43332023-03-25 11:03:05thetuberbeeSípálya (55 pont)cpp17Accepted 55/55115ms21212 KiB
#include <bits/stdc++.h>

using namespace std;

int main ()
{
    int n, k;
    cin >> n >> k;
    vector<long long> vec(n);
    for (int i = 0; i < n; i++) {
        cin >> vec[i];
        vec[i] += i;
    }
    multiset<long long> maxset;
    long long sum = 0;
    for (int i = 0; i < k; i++) {
        sum += vec[i];
        maxset.insert(vec[i]);
    }
    //for (auto x : maxset) cout << x << " ";
    //cout << "\n";
    long long mincost = INT64_MAX;
    for (int i = k - 1; i < n; i++) {
        if (i >= k) {
            sum -= vec[i - k];
            sum += vec[i];
            maxset.erase(maxset.find(vec[i - k]));
            maxset.insert(vec[i]);
        }
        auto it = maxset.end();
        it--;
        long long localmax = *it;
        long long cost = k * localmax - sum;
        if (cost < mincost) {
            mincost = cost;
        }
        /*cout << localmax << " " << sum << " " << cost << "\nset: ";
        for (long long x : maxset) {
            cout << x << " ";
        }
        cout << "\n";*/
    }
    cout << mincost;
}
SubtaskSumTestVerdictTimeMemory
base55/55
1Accepted0/03ms1688 KiB
2Accepted0/03ms1844 KiB
3Accepted2/23ms2100 KiB
4Accepted2/23ms2312 KiB
5Accepted2/23ms2528 KiB
6Accepted2/23ms2664 KiB
7Accepted3/33ms2632 KiB
8Accepted1/18ms2964 KiB
9Accepted1/18ms2992 KiB
10Accepted1/18ms3380 KiB
11Accepted1/18ms3428 KiB
12Accepted1/18ms3596 KiB
13Accepted1/18ms4040 KiB
14Accepted2/28ms4168 KiB
15Accepted2/27ms3964 KiB
16Accepted2/2108ms7660 KiB
17Accepted2/2112ms16456 KiB
18Accepted2/2114ms17448 KiB
19Accepted3/3112ms21212 KiB
20Accepted2/2112ms8864 KiB
21Accepted2/2109ms8812 KiB
22Accepted2/2111ms8800 KiB
23Accepted2/2111ms8276 KiB
24Accepted2/2115ms8408 KiB
25Accepted2/2112ms9292 KiB
26Accepted2/2112ms9716 KiB
27Accepted2/2112ms10016 KiB
28Accepted3/3112ms9972 KiB
29Accepted3/3112ms10488 KiB
30Accepted3/3114ms11008 KiB