#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';
}
# |
결과 |
실행 시간 |
메모리 |
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 |
- |