82152024-01-12 20:21:04gergomiszoriSípálya (55 pont)cpp17Wrong answer 6/55500ms7276 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;
    }

    ll ans = LLONG_MAX-1;

    ll maxe = -1;
    ll maxl = 0;

    ll ans2 = 0;

    for(ll i = 0; i < n-k+1; i++)
    {
        if(i == 0)
        {
            for(ll j = 0; j < k; j++)
            {
                if(a[i+j] >= maxe)
                {
                    maxe = a[i+j];
                    maxl = j;
                }
            }
            for(ll j = 0; j < k; j++)
            {
                ll magas = maxl-j + maxe;
                ans2 += magas - a[i+j];
            }
        }
        else
        {
            ans2 -= (maxl + maxe) - a[i-1];
            ll fsize = (maxl + maxe) -1;
            maxl--;
            if(a[i+k-1] >= maxe)
            {
                maxe = a[i+k-1];
                maxl = k-1;
                ans2 += (maxe - (fsize - maxl)) * (k-1);
            }
            if(maxl < 0)
            {
                for(ll j = 0; j < k; j++)
                {
                    if(a[i+j] >= maxe)
                    {
                        maxe = a[i+j];
                        maxl = j;
                    }
                }
                ans2 += (maxe - (fsize - maxl)) * (k-1);
            }
            ans2 += (maxl-(k-1)) + maxe - a[i+k-1];
        }
        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
base6/55
1Accepted0/03ms1832 KiB
2Wrong answer0/03ms2048 KiB
3Wrong answer0/23ms2244 KiB
4Wrong answer0/23ms2340 KiB
5Wrong answer0/23ms2556 KiB
6Wrong answer0/23ms2768 KiB
7Accepted3/33ms2976 KiB
8Wrong answer0/14ms3132 KiB
9Wrong answer0/14ms3404 KiB
10Wrong answer0/14ms3456 KiB
11Wrong answer0/114ms3732 KiB
12Wrong answer0/114ms3624 KiB
13Accepted1/127ms3908 KiB
14Wrong answer0/217ms4084 KiB
15Wrong answer0/214ms4292 KiB
16Time limit exceeded0/2500ms4864 KiB
17Accepted2/227ms7276 KiB
18Time limit exceeded0/2469ms5264 KiB
19Time limit exceeded0/3465ms5260 KiB
20Time limit exceeded0/2477ms5260 KiB
21Time limit exceeded0/2462ms5312 KiB
22Time limit exceeded0/2469ms5196 KiB
23Time limit exceeded0/2465ms5444 KiB
24Time limit exceeded0/2474ms5564 KiB
25Time limit exceeded0/2465ms5696 KiB
26Time limit exceeded0/2469ms5764 KiB
27Time limit exceeded0/2465ms5836 KiB
28Time limit exceeded0/3465ms5884 KiB
29Time limit exceeded0/3426ms5632 KiB
30Time limit exceeded0/3455ms5680 KiB