#include <cstdio>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
typedef pair<int,int> pi;
int n,k,a[1000005];
priority_queue<pi, vector<pi>, greater<pi> > pq;
stack<int> s,t;
int main(){
scanf("%d %d",&n,&k);
for (int i=0; i<n; i++) {
scanf("%d",&a[i]);
}
rotate(a, max_element(a, a+n), a+n);
a[n] = a[0];
for (int i=0; i<=n; i++) {
if(s.empty() || s.top() >= a[i]){
s.push(a[i]);
t.push(i);
}
else{
while (!s.empty() && s.top() < a[i]) {
int ptop = s.top();
s.pop();
t.pop();
pq.push(pi(i - t.top() - 1, min(s.top(), a[i]) - ptop));
// first cost로 t만큼 뽕뽑
}
s.push(a[i]);
t.push(i);
}
}
int ret = 0;
while(!pq.empty()){
pi t = pq.top();
pq.pop();
int change = min(t.second, k / t.first);
ret += change * 2;
k -= change * t.first;
}
printf("%d",ret);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
5488 KB |
Output is correct |
2 |
Correct |
0 ms |
5488 KB |
Output is correct |
3 |
Correct |
0 ms |
5488 KB |
Output is correct |
4 |
Correct |
0 ms |
5488 KB |
Output is correct |
5 |
Correct |
0 ms |
5488 KB |
Output is correct |
6 |
Correct |
0 ms |
5488 KB |
Output is correct |
7 |
Correct |
0 ms |
5488 KB |
Output is correct |
8 |
Correct |
0 ms |
5488 KB |
Output is correct |
9 |
Correct |
0 ms |
5488 KB |
Output is correct |
10 |
Correct |
30 ms |
7032 KB |
Output is correct |
11 |
Correct |
29 ms |
7032 KB |
Output is correct |
12 |
Correct |
35 ms |
7312 KB |
Output is correct |
13 |
Correct |
362 ms |
17784 KB |
Output is correct |
14 |
Correct |
340 ms |
17784 KB |
Output is correct |
15 |
Correct |
307 ms |
17784 KB |
Output is correct |
16 |
Correct |
358 ms |
17784 KB |
Output is correct |
17 |
Correct |
325 ms |
17784 KB |
Output is correct |
18 |
Correct |
396 ms |
17784 KB |
Output is correct |
19 |
Correct |
305 ms |
17784 KB |
Output is correct |
20 |
Correct |
317 ms |
21940 KB |
Output is correct |