82132024-01-12 20:05:26gergomiszoriSípálya (55 pont)cpp17Wrong answer 4/55481ms6948 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(maxl < 0 || a[i+k-1] > maxe)
            {
                for(ll j = 0; j < k; j++)
                {
                    if(a[i+j] >= maxe)
                    {
                        maxe = a[i+j];
                        maxl = j;
                    }
                }
                ans2 += (fsize - maxl) * k;
            }
            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
base4/55
1Accepted0/03ms1832 KiB
2Wrong answer0/03ms2052 KiB
3Wrong answer0/23ms2276 KiB
4Wrong answer0/23ms2484 KiB
5Wrong answer0/23ms2708 KiB
6Wrong answer0/23ms2916 KiB
7Accepted3/33ms3000 KiB
8Wrong answer0/14ms3320 KiB
9Wrong answer0/14ms3836 KiB
10Wrong answer0/14ms3724 KiB
11Wrong answer0/117ms3860 KiB
12Wrong answer0/117ms3796 KiB
13Accepted1/132ms4096 KiB
14Wrong answer0/221ms4032 KiB
15Wrong answer0/216ms4024 KiB
16Time limit exceeded0/2474ms4880 KiB
17Wrong answer0/228ms6948 KiB
18Time limit exceeded0/2462ms4904 KiB
19Time limit exceeded0/3479ms4852 KiB
20Time limit exceeded0/2467ms4968 KiB
21Time limit exceeded0/2470ms4908 KiB
22Time limit exceeded0/2455ms4848 KiB
23Time limit exceeded0/2462ms4856 KiB
24Time limit exceeded0/2465ms4840 KiB
25Time limit exceeded0/2439ms4908 KiB
26Time limit exceeded0/2458ms4896 KiB
27Time limit exceeded0/2481ms5112 KiB
28Time limit exceeded0/3441ms5116 KiB
29Time limit exceeded0/3453ms4920 KiB
30Time limit exceeded0/3462ms5156 KiB