#include <bits/stdc++.h>
using namespace std;
const int mx = 1e6 + 5;
int n, k, A[mx], ans; vector<int> range[mx]; stack<int> stk;
int main(){
cin >> n >> k;
for (int i = 1; i <= n; i++){
cin >> A[i];
while (!stk.empty() and A[stk.top()] <= A[i]) stk.pop();
stk.push(i);
}
for (int i = 1; i <= n; i++){
int mxTop = 0;
while (!stk.empty()){
int L = stk.top();
int dst = (L < i) ? (i - L - 1) : (n - (L - i + 1));
if (dst) range[dst].push_back(min(A[L], A[i]) - mxTop);
if (A[L] > A[i]) break;
mxTop = max(mxTop, A[L]); stk.pop();
}
stk.push(i);
}
for (long long len = 1; len <= n; len++){
for (int lim : range[len]){
if (len * lim >= k) ans += 2 * (k / len), k = 0;
else ans += 2 * lim, k -= len * lim;
}
}
cout<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
Output is correct |
2 |
Correct |
12 ms |
23756 KB |
Output is correct |
3 |
Correct |
12 ms |
23756 KB |
Output is correct |
4 |
Correct |
12 ms |
23780 KB |
Output is correct |
5 |
Correct |
17 ms |
23776 KB |
Output is correct |
6 |
Correct |
14 ms |
23756 KB |
Output is correct |
7 |
Correct |
13 ms |
23756 KB |
Output is correct |
8 |
Correct |
14 ms |
23788 KB |
Output is correct |
9 |
Correct |
13 ms |
23792 KB |
Output is correct |
10 |
Correct |
52 ms |
27284 KB |
Output is correct |
11 |
Correct |
52 ms |
24696 KB |
Output is correct |
12 |
Correct |
52 ms |
27272 KB |
Output is correct |
13 |
Correct |
450 ms |
62932 KB |
Output is correct |
14 |
Correct |
428 ms |
63216 KB |
Output is correct |
15 |
Correct |
417 ms |
61356 KB |
Output is correct |
16 |
Correct |
413 ms |
62472 KB |
Output is correct |
17 |
Correct |
414 ms |
62140 KB |
Output is correct |
18 |
Correct |
450 ms |
63120 KB |
Output is correct |
19 |
Correct |
453 ms |
63300 KB |
Output is correct |
20 |
Correct |
332 ms |
43296 KB |
Output is correct |