82112024-01-12 19:42:31gergomiszoriSípálya (55 pont)cpp17Wrong answer 0/55470ms9324 KiB
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define endl "\n"
#define pll pair<ll,ll>
#define fs first
#define sc second

const ll MOD = 1e9+7;

void solve()
{
    ll n, k;
    cin >> n >> k;
    vector<ll> a(n);
    
    for(ll i = 0; i < n; i++) cin >> a[i];
    
    if(k == 1)
    {
        cout << 0 << endl;
        return;
    }

    vector<ll> pref(n, 0), suf(n, 0);
    ll ans = 0;

    ll maxe = -1;
    ll maxl = 0;
    
    for(ll i = 0; i < n; i++)
    {
        if(a[i] >= maxe || (maxe != -1 && a[i] > maxe - (i-maxl)))
        {
            maxe = a[i];
            maxl = i;
        }
    }

    vector<ll> dist(n, 0);

    for(ll i = 0; i < n; i++)
    {
        ll magas = maxl-i + maxe;
        dist[i] = magas - a[i];
        ans += dist[i];
        pref[i] += ans;
    }
    suf = pref;
    reverse(suf.begin(), suf.end());

    ll fans = ans;

    for(ll i = 0; i < n-k+1; i++)
    {
        ll min_dist = 1e17;
        for(ll j = 0; j < k; j++)
        {
            min_dist = min(min_dist, dist[i+j]);
        }
        ll ans2 = fans - (min_dist)*k;
        if(i > 0) ans2 -= pref[i-1];
        if(i < n-k) ans2 -= suf[i+k];

        ans = min(ans, ans2);
    }

    cout << ans << endl;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    ll t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base0/55
1Accepted0/03ms1832 KiB
2Wrong answer0/03ms2052 KiB
3Wrong answer0/23ms2424 KiB
4Wrong answer0/23ms2640 KiB
5Wrong answer0/23ms2692 KiB
6Wrong answer0/23ms2660 KiB
7Wrong answer0/33ms2920 KiB
8Wrong answer0/18ms3668 KiB
9Wrong answer0/18ms3704 KiB
10Wrong answer0/18ms3796 KiB
11Wrong answer0/113ms3664 KiB
12Wrong answer0/112ms3856 KiB
13Wrong answer0/124ms3812 KiB
14Wrong answer0/227ms3848 KiB
15Wrong answer0/213ms3848 KiB
16Time limit exceeded0/2463ms8804 KiB
17Time limit exceeded0/2462ms8936 KiB
18Time limit exceeded0/2458ms8852 KiB
19Time limit exceeded0/3470ms8736 KiB
20Time limit exceeded0/2467ms8884 KiB
21Time limit exceeded0/2465ms8868 KiB
22Time limit exceeded0/2462ms9028 KiB
23Time limit exceeded0/2458ms9236 KiB
24Time limit exceeded0/2467ms9284 KiB
25Time limit exceeded0/2470ms9124 KiB
26Time limit exceeded0/2462ms9228 KiB
27Time limit exceeded0/2439ms9324 KiB
28Time limit exceeded0/3462ms9228 KiB
29Time limit exceeded0/3462ms9276 KiB
30Time limit exceeded0/3462ms9304 KiB