#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MXN = 1e6+5;
ll A[MXN];
int L[MXN], R[MXN];
void era(int x){
int l = L[x], r = R[x];
R[l] = r; L[r] = l;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, k; cin >> n >> k;
for(int i = 1;i<=n;i++) {
cin >> A[i];
if(i>1 && A[i]*A[i-1]>=0) A[i-1] += A[i], i--, n--;
else if(i==1 && A[i]<=0) i--,n--;
}
if(n && A[n]<=0) n--;
for(int i = 1;i<=n;i++) L[i] = i-1, R[i] = i+1;
L[1] = -1, R[n] = -1;
ll ans = 0;
int cnt = 0;
for(int i = 1;i<=n;i++) {
if(A[i]>0) cnt++;
ans += max(0LL,A[i]);
A[i] = -abs(A[i]);
}
k = cnt-k;
priority_queue<pair<ll,int>> pq, rem;
for(int i = 1;i<=n;i++) pq.push({A[i],i});
for(int i = 0;i<k;i++){
while(pq.size() && rem.size() && pq.top()==rem.top()) pq.pop(), rem.pop();
int p = pq.top().second; pq.pop();
ans += A[p];
int l = L[p], r = R[p];
if(l!=-1 && r!=-1) {
A[p] = A[l]+A[r]-A[p];
pq.push({A[p],p});
}
else era(p);
if(l!=-1) era(l), rem.push({A[l],l});
if(r!=-1) era(r), rem.push({A[r],r});
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
7760 KB |
Output is correct |
2 |
Correct |
26 ms |
7748 KB |
Output is correct |
3 |
Correct |
26 ms |
7868 KB |
Output is correct |
4 |
Correct |
24 ms |
7772 KB |
Output is correct |
5 |
Correct |
25 ms |
7772 KB |
Output is correct |
6 |
Correct |
25 ms |
7732 KB |
Output is correct |
7 |
Correct |
24 ms |
7772 KB |
Output is correct |
8 |
Incorrect |
29 ms |
7764 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
5712 KB |
Output is correct |
2 |
Correct |
17 ms |
5468 KB |
Output is correct |
3 |
Correct |
15 ms |
5468 KB |
Output is correct |
4 |
Correct |
15 ms |
5568 KB |
Output is correct |
5 |
Incorrect |
26 ms |
7772 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
86 ms |
20932 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4696 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4696 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
2 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
2 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4696 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
2 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
2 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
2 ms |
4440 KB |
Output is correct |
22 |
Correct |
2 ms |
4444 KB |
Output is correct |
23 |
Correct |
2 ms |
4444 KB |
Output is correct |
24 |
Correct |
2 ms |
4444 KB |
Output is correct |
25 |
Correct |
2 ms |
4444 KB |
Output is correct |
26 |
Correct |
2 ms |
4444 KB |
Output is correct |
27 |
Correct |
2 ms |
4440 KB |
Output is correct |
28 |
Correct |
2 ms |
4444 KB |
Output is correct |
29 |
Correct |
2 ms |
4444 KB |
Output is correct |
30 |
Correct |
2 ms |
4440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
7760 KB |
Output is correct |
2 |
Correct |
26 ms |
7748 KB |
Output is correct |
3 |
Correct |
26 ms |
7868 KB |
Output is correct |
4 |
Correct |
24 ms |
7772 KB |
Output is correct |
5 |
Correct |
25 ms |
7772 KB |
Output is correct |
6 |
Correct |
25 ms |
7732 KB |
Output is correct |
7 |
Correct |
24 ms |
7772 KB |
Output is correct |
8 |
Incorrect |
29 ms |
7764 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |