Submission #1081638

# Submission time Handle Problem Language Result Execution time Memory
1081638 2024-08-30T08:40:55 Z coolboy19521 Mountain Trek Route (IZhO12_route) C++17
0 / 100
1 ms 348 KB
#include "bits/stdc++.h"
#define ll long long

using namespace std;

const int sz = 1e6 + 6;

int a[sz];

int main() {
    int n, k; cin >> n >> k;
    for (int i = 0; i < n; i ++)
        cin >> a[i];
    int pr = 0;
    for (int i = 0; i < n; i ++) {
        int j = (i - 1 + n) % n;
        pr += abs(a[i] - a[j]);
    }
    int lo = *min_element(a, a + n), hi = *max_element(a, a + n);
    while (lo != hi) {
        int mi = (lo + hi + 1) / 2;
        int r = 0;
        for (int i = 0; i < n; i ++)
            r += max(mi - a[i], 0);
        if (r <= k) lo = mi;
        else hi = mi - 1;
    }
    for (int i = 0; i < n; i ++)
        a[i] = max(a[i], lo);
    int r = 0;
    for (int i = 0; i < n; i ++) {
        int j = (i - 1 + n) % n;
        r += abs(a[i] - a[j]);
    }
    cout << pr - r << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Incorrect 0 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -