40032023-03-08 08:32:431478Sípálya (55 pont)cpp17Accepted 55/55120ms24168 KiB
#include <bits/stdc++.h>

using namespace std;

int main()
{
    //ifstream cin("in.txt");
    long long n;
    cin >> n;
    long long k;
    cin >> k;
    vector<long long> heights(n + 2);
    vector<long long> ps(n + 1);
    for(int i = 1; i <= n; i++){
        cin >> heights[i];
        ps[i] = ps[i - 1] + heights[i];
    }

    multiset<long long> s;
    for(int i = 1; i <= k; i++){
        s.insert(heights[i] + i);
    }

    long long ans = LONG_MAX;
    for(long long i = k; i <= n; i++){
        long long actualsum = ps[i] - ps[i - k];
        long long neededsum = (*s.rbegin()- i) * k + (k - 1)*k/2;
        //cout << i << " " << heights[i] << " " << *s.rbegin() - i << " " << neededsum - actualsum << '\n';
        ans = min(ans, neededsum - actualsum);
        s.erase(s.lower_bound(heights[i - k + 1] + i - k + 1));
        s.insert(heights[i + 1] + i + 1);
    }
    cout << ans;



    return 0;
}
SubtaskSumTestVerdictTimeMemory
base55/55
1Accepted0/03ms1812 KiB
2Accepted0/03ms2184 KiB
3Accepted2/23ms2292 KiB
4Accepted2/23ms2452 KiB
5Accepted2/24ms2676 KiB
6Accepted2/23ms2892 KiB
7Accepted3/33ms3096 KiB
8Accepted1/18ms3856 KiB
9Accepted1/18ms3892 KiB
10Accepted1/18ms3856 KiB
11Accepted1/18ms4212 KiB
12Accepted1/18ms3928 KiB
13Accepted1/18ms4156 KiB
14Accepted2/28ms4264 KiB
15Accepted2/28ms4400 KiB
16Accepted2/2109ms10576 KiB
17Accepted2/2114ms19440 KiB
18Accepted2/2114ms20480 KiB
19Accepted3/3116ms24168 KiB
20Accepted2/2115ms11816 KiB
21Accepted2/2111ms11828 KiB
22Accepted2/2112ms11968 KiB
23Accepted2/2114ms11628 KiB
24Accepted2/2120ms11844 KiB
25Accepted2/2115ms12596 KiB
26Accepted2/2114ms12904 KiB
27Accepted2/2112ms13392 KiB
28Accepted3/3114ms13396 KiB
29Accepted3/3118ms13960 KiB
30Accepted3/3115ms14496 KiB