#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define dbg(x) cerr << #x << " " << x << "\n"
using ll = long long;
const int MAX_N = 1e6;
int a[MAX_N], h[1 + MAX_N];
int stk[1 + MAX_N + 1];
int main () {
ios::sync_with_stdio (false);
cin.tie (0); cout.tie (0);
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++)
cin >> a[i];
int mx = 0;
for (int i = 0; i < n; i++)
if (a[mx] < a[i])
mx = i;
int nr = 0;
for (int i = mx; i < n; i++)
h[nr++] = a[i];
for (int i = 0; i < mx; i++)
h[nr++] = a[i];
h[nr] = a[mx]; /// [0...n] h[0] = h[n] = a[mx]
int vf = 0;
priority_queue <pair <int, int>> gaps;
for (int i = 0; i <= n; i++) {
while (vf && h[stk[vf]] < h[i]) {
vf--;
gaps.push ({-(i - stk[vf] - 1), h[stk[vf + 1]] - min (h[i], h[stk[vf]])});
}
stk[++vf] = i;
}
int ans = 0;
///dbg (n);
while (gaps.size ()) {
int length = -gaps.top ().first;
int heigth = -gaps.top ().second;
/// dbg (length);
gaps.pop ();
int taken = min (k / length, heigth);
k -= taken * length;
ans += taken * 2;
}
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
23 ms |
3180 KB |
Output is correct |
11 |
Correct |
31 ms |
3308 KB |
Output is correct |
12 |
Correct |
23 ms |
3436 KB |
Output is correct |
13 |
Correct |
240 ms |
26080 KB |
Output is correct |
14 |
Correct |
228 ms |
26080 KB |
Output is correct |
15 |
Correct |
231 ms |
24160 KB |
Output is correct |
16 |
Correct |
220 ms |
25052 KB |
Output is correct |
17 |
Correct |
216 ms |
25056 KB |
Output is correct |
18 |
Correct |
223 ms |
25976 KB |
Output is correct |
19 |
Correct |
226 ms |
26080 KB |
Output is correct |
20 |
Correct |
216 ms |
24288 KB |
Output is correct |