82162024-01-12 20:35:36gergomiszoriSípálya (55 pont)cpp17Accepted 55/5568ms21100 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];
        a[i] += i;
    }
    
    if(k == 1)
    {
        cout << 0 << endl;
        return;
    }

    ll ans = LLONG_MAX;
    multiset<ll> elements;
    ll vsum = 0;

    for(ll i = 0; i < k; i++)
    {
        vsum += a[i];
        elements.insert(a[i]);
    }

    for(ll i = k-1; i < n; i++)
    {
        if(i >= k)
        {
            vsum -= a[i-k];
            vsum += a[i];
            elements.erase(elements.find(a[i-k]));
            elements.insert(a[i]);
        }
        auto it = elements.end();
        it--;
        ans = min(ans, k * *it - vsum);
    }
    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
base55/55
1Accepted0/03ms1824 KiB
2Accepted0/03ms2064 KiB
3Accepted2/23ms2416 KiB
4Accepted2/23ms2580 KiB
5Accepted2/23ms2612 KiB
6Accepted2/23ms2748 KiB
7Accepted3/33ms2832 KiB
8Accepted1/16ms3180 KiB
9Accepted1/16ms3388 KiB
10Accepted1/16ms3276 KiB
11Accepted1/16ms3432 KiB
12Accepted1/16ms3420 KiB
13Accepted1/16ms3772 KiB
14Accepted2/26ms4084 KiB
15Accepted2/26ms3852 KiB
16Accepted2/263ms7396 KiB
17Accepted2/268ms16036 KiB
18Accepted2/265ms17172 KiB
19Accepted3/368ms21100 KiB
20Accepted2/265ms8900 KiB
21Accepted2/264ms8352 KiB
22Accepted2/264ms8356 KiB
23Accepted2/264ms7844 KiB
24Accepted2/264ms8100 KiB
25Accepted2/267ms9112 KiB
26Accepted2/267ms9596 KiB
27Accepted2/264ms10012 KiB
28Accepted3/365ms10072 KiB
29Accepted3/365ms10328 KiB
30Accepted3/368ms10868 KiB