#include <bits/stdc++.h>
using namespace std;
#define int long long
int N,K;
int A[500000], v[500000], l[500000], r[500000], a[500000];
priority_queue<pair<int,int>> pq;
// priority_queue<pair<int,int>> pq;
main() {
cin >> N >> K;
int cur = 0, t = 0, pos = 0;
for (int i = 0; i <= N; i++) {
if (i < N) cin >> A[i];
if (i < N && (cur >= 0) == (A[i] >= 0)) cur += A[i];
else {
if (cur == 0) {
cur = A[i];
continue;
}
// cout << i << " " << cur << " " << t << "\n";
v[t] = cur;
l[t] = t-1;
r[t] = t+1;
pq.push(make_pair(-abs(cur),t));
a[t] = 1;
t++;
if (cur > 0) pos++;
cur = A[i];
}
}
int x = 0, ans = 0;
//cout << "\n";
while (!pq.empty()) {
int val, i;
tie(val, i) = pq.top();
pq.pop();
if (!a[i]) continue;
if (pos > K) {
//cout << "a " << v[i] << " " << i << "\n";
if (v[i] > 0) pos--;
if (l[i] != -1) {
if (v[l[i]] > 0) pos--;
v[i] += v[l[i]];
r[l[l[i]]] = i;
a[l[i]] = 0;
l[i] = l[l[i]];
}
if (r[i] != t) {
if (v[r[i]] > 0) pos--;
v[i] += v[r[i]];
l[r[r[i]]] = i;
a[r[i]] = 0;
r[i] = r[r[i]];
}
pq.push(make_pair(-abs(v[i]),i));
// cout << "add " << v[i] << " " << i << " " << a[i] << "\n";
if (v[i] > 0) pos++;
}
else {
// cout << "b " << v[i] << " " << i << " " << a[i] << "\n";
if (v[i] > 0) {
ans += v[i];
// cout << "! " << v[i] << "\n";
}
}
//cout << v[i] << " " << i << "\n";
//if (++x == 3) break;
}
cout << ans << "\n";
//cout << pos << "\n";
}
Compilation message
feast.cpp:10:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
feast.cpp: In function 'int main()':
feast.cpp:35:9: warning: unused variable 'x' [-Wunused-variable]
int x = 0, ans = 0;
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
178 ms |
5496 KB |
Output is correct |
2 |
Correct |
181 ms |
5472 KB |
Output is correct |
3 |
Correct |
187 ms |
5624 KB |
Output is correct |
4 |
Correct |
185 ms |
5496 KB |
Output is correct |
5 |
Correct |
186 ms |
5496 KB |
Output is correct |
6 |
Correct |
180 ms |
5496 KB |
Output is correct |
7 |
Correct |
182 ms |
5368 KB |
Output is correct |
8 |
Correct |
184 ms |
5584 KB |
Output is correct |
9 |
Correct |
179 ms |
5368 KB |
Output is correct |
10 |
Correct |
182 ms |
5496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
3704 KB |
Output is correct |
2 |
Correct |
103 ms |
3776 KB |
Output is correct |
3 |
Correct |
97 ms |
3704 KB |
Output is correct |
4 |
Correct |
97 ms |
3832 KB |
Output is correct |
5 |
Correct |
178 ms |
5368 KB |
Output is correct |
6 |
Correct |
96 ms |
3704 KB |
Output is correct |
7 |
Correct |
99 ms |
3832 KB |
Output is correct |
8 |
Correct |
185 ms |
5500 KB |
Output is correct |
9 |
Correct |
179 ms |
5368 KB |
Output is correct |
10 |
Correct |
99 ms |
3960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
251 ms |
13672 KB |
Output is correct |
2 |
Correct |
244 ms |
13668 KB |
Output is correct |
3 |
Correct |
250 ms |
13668 KB |
Output is correct |
4 |
Correct |
248 ms |
13668 KB |
Output is correct |
5 |
Correct |
246 ms |
13668 KB |
Output is correct |
6 |
Correct |
254 ms |
13668 KB |
Output is correct |
7 |
Correct |
249 ms |
13668 KB |
Output is correct |
8 |
Correct |
246 ms |
13672 KB |
Output is correct |
9 |
Correct |
252 ms |
13668 KB |
Output is correct |
10 |
Correct |
250 ms |
13540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
178 ms |
5496 KB |
Output is correct |
2 |
Correct |
181 ms |
5472 KB |
Output is correct |
3 |
Correct |
187 ms |
5624 KB |
Output is correct |
4 |
Correct |
185 ms |
5496 KB |
Output is correct |
5 |
Correct |
186 ms |
5496 KB |
Output is correct |
6 |
Correct |
180 ms |
5496 KB |
Output is correct |
7 |
Correct |
182 ms |
5368 KB |
Output is correct |
8 |
Correct |
184 ms |
5584 KB |
Output is correct |
9 |
Correct |
179 ms |
5368 KB |
Output is correct |
10 |
Correct |
182 ms |
5496 KB |
Output is correct |
11 |
Correct |
98 ms |
3704 KB |
Output is correct |
12 |
Correct |
103 ms |
3776 KB |
Output is correct |
13 |
Correct |
97 ms |
3704 KB |
Output is correct |
14 |
Correct |
97 ms |
3832 KB |
Output is correct |
15 |
Correct |
178 ms |
5368 KB |
Output is correct |
16 |
Correct |
96 ms |
3704 KB |
Output is correct |
17 |
Correct |
99 ms |
3832 KB |
Output is correct |
18 |
Correct |
185 ms |
5500 KB |
Output is correct |
19 |
Correct |
179 ms |
5368 KB |
Output is correct |
20 |
Correct |
99 ms |
3960 KB |
Output is correct |
21 |
Correct |
251 ms |
13672 KB |
Output is correct |
22 |
Correct |
244 ms |
13668 KB |
Output is correct |
23 |
Correct |
250 ms |
13668 KB |
Output is correct |
24 |
Correct |
248 ms |
13668 KB |
Output is correct |
25 |
Correct |
246 ms |
13668 KB |
Output is correct |
26 |
Correct |
254 ms |
13668 KB |
Output is correct |
27 |
Correct |
249 ms |
13668 KB |
Output is correct |
28 |
Correct |
246 ms |
13672 KB |
Output is correct |
29 |
Correct |
252 ms |
13668 KB |
Output is correct |
30 |
Correct |
250 ms |
13540 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
4 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
5 ms |
384 KB |
Output is correct |
35 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
36 |
Halted |
0 ms |
0 KB |
- |