#include<bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define ii pair<int, int>
#define iii pair<ii, int>
#define ll long long
#define pb push_back
#define pli pair<ll, int>
#define all(v) (v).begin(), (v).end()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define cntbit(mask) __builtin_popcount(mask)
#define getbit(x, i) ((x >> i) & 1)
#define MASK(i) (1 << i)
#define pli pair<ll, int>
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return 1;} return 0;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return 1;} return 0;}
const int N = 2005;
const int V = 3e5 + 5;
const int MOD = 1e9 + 7;
int n, k, a[V], pos;
pli sum = mp(0, 1);
set<pli> Q;
set<int> s;
ll cost[V], ans;
void solve() {
cin >> n >> k;
for(int i = 1 ; i <= n ; i++) {
cin >> a[i];
if (i == 1 || (sum.fi < 0 && a[i] < 0) || (sum.fi >= 0 && a[i] >= 0)) sum.fi += a[i];
else {
if (sum.se > 1 || sum.fi >= 0) {
cost[sum.se] = sum.fi;
if (sum.fi >= 0) pos++;
sum.fi = abs(sum.fi);
Q.insert(sum);
s.insert(sum.se);
}
sum = mp(a[i], i);
}
}
if (sum.fi >= 0) {
pos++;
cost[sum.se] = sum.fi;
Q.insert(sum);
s.insert(sum.se);
}
while((int) pos > k) {
pli x = *Q.begin();
auto u = s.lower_bound(x.se), v = u;
++v;
if (cost[x.se] < 0 && (u == s.begin() || v == s.end())) {
s.erase(u);
Q.erase(Q.begin());
continue;
}
int re[2] = {0, 0};
if (cost[x.se] >= 0) --pos;
if (u != s.begin()) {
--u;
cost[x.se] += cost[*u];
re[0] = *u;
}
if (v != s.end()) {
cost[x.se] += cost[*v];
re[1] = *v;
}
Q.erase(Q.begin());
for(int j = 0 ; j < 2 ; j++) if (re[j]) {
if (cost[re[j]] >= 0) --pos;
s.erase(re[j]);
auto it = Q.find(pli(abs(cost[re[j]]), re[j]));
if (it != Q.end()) Q.erase(it);
}
Q.insert(mp(abs(cost[x.se]), x.se));
if (cost[x.se] >= 0) ++pos;
}
while((int) Q.size()) {
pli x = *Q.begin();
Q.erase(Q.begin());
if (cost[x.se] >= 0) ans += cost[x.se];
} cout << ans;
}
int main(void) {
//freopen("task.inp", "r", stdin);
//freopen("task.out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1; while(t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
4176 KB |
Output is correct |
2 |
Correct |
21 ms |
4444 KB |
Output is correct |
3 |
Correct |
21 ms |
4432 KB |
Output is correct |
4 |
Correct |
21 ms |
4468 KB |
Output is correct |
5 |
Correct |
21 ms |
4432 KB |
Output is correct |
6 |
Correct |
31 ms |
4444 KB |
Output is correct |
7 |
Correct |
20 ms |
4224 KB |
Output is correct |
8 |
Correct |
22 ms |
4424 KB |
Output is correct |
9 |
Correct |
22 ms |
4168 KB |
Output is correct |
10 |
Correct |
23 ms |
4424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
4688 KB |
Output is correct |
2 |
Correct |
19 ms |
4688 KB |
Output is correct |
3 |
Correct |
15 ms |
4580 KB |
Output is correct |
4 |
Correct |
14 ms |
4688 KB |
Output is correct |
5 |
Correct |
29 ms |
6260 KB |
Output is correct |
6 |
Correct |
15 ms |
4668 KB |
Output is correct |
7 |
Correct |
14 ms |
4688 KB |
Output is correct |
8 |
Correct |
23 ms |
6472 KB |
Output is correct |
9 |
Correct |
23 ms |
6388 KB |
Output is correct |
10 |
Correct |
15 ms |
4688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
227 ms |
23112 KB |
Output is correct |
2 |
Correct |
270 ms |
22600 KB |
Output is correct |
3 |
Correct |
240 ms |
23112 KB |
Output is correct |
4 |
Correct |
235 ms |
22852 KB |
Output is correct |
5 |
Correct |
230 ms |
22856 KB |
Output is correct |
6 |
Correct |
243 ms |
23112 KB |
Output is correct |
7 |
Correct |
252 ms |
23112 KB |
Output is correct |
8 |
Correct |
225 ms |
23048 KB |
Output is correct |
9 |
Correct |
252 ms |
23200 KB |
Output is correct |
10 |
Correct |
233 ms |
23112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
508 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
476 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
508 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
476 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
472 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
508 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
476 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
472 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
592 KB |
Output is correct |
22 |
Correct |
1 ms |
592 KB |
Output is correct |
23 |
Correct |
1 ms |
592 KB |
Output is correct |
24 |
Correct |
1 ms |
488 KB |
Output is correct |
25 |
Correct |
2 ms |
592 KB |
Output is correct |
26 |
Correct |
1 ms |
592 KB |
Output is correct |
27 |
Correct |
1 ms |
592 KB |
Output is correct |
28 |
Correct |
2 ms |
592 KB |
Output is correct |
29 |
Correct |
2 ms |
592 KB |
Output is correct |
30 |
Correct |
1 ms |
848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
4176 KB |
Output is correct |
2 |
Correct |
21 ms |
4444 KB |
Output is correct |
3 |
Correct |
21 ms |
4432 KB |
Output is correct |
4 |
Correct |
21 ms |
4468 KB |
Output is correct |
5 |
Correct |
21 ms |
4432 KB |
Output is correct |
6 |
Correct |
31 ms |
4444 KB |
Output is correct |
7 |
Correct |
20 ms |
4224 KB |
Output is correct |
8 |
Correct |
22 ms |
4424 KB |
Output is correct |
9 |
Correct |
22 ms |
4168 KB |
Output is correct |
10 |
Correct |
23 ms |
4424 KB |
Output is correct |
11 |
Correct |
15 ms |
4688 KB |
Output is correct |
12 |
Correct |
19 ms |
4688 KB |
Output is correct |
13 |
Correct |
15 ms |
4580 KB |
Output is correct |
14 |
Correct |
14 ms |
4688 KB |
Output is correct |
15 |
Correct |
29 ms |
6260 KB |
Output is correct |
16 |
Correct |
15 ms |
4668 KB |
Output is correct |
17 |
Correct |
14 ms |
4688 KB |
Output is correct |
18 |
Correct |
23 ms |
6472 KB |
Output is correct |
19 |
Correct |
23 ms |
6388 KB |
Output is correct |
20 |
Correct |
15 ms |
4688 KB |
Output is correct |
21 |
Correct |
227 ms |
23112 KB |
Output is correct |
22 |
Correct |
270 ms |
22600 KB |
Output is correct |
23 |
Correct |
240 ms |
23112 KB |
Output is correct |
24 |
Correct |
235 ms |
22852 KB |
Output is correct |
25 |
Correct |
230 ms |
22856 KB |
Output is correct |
26 |
Correct |
243 ms |
23112 KB |
Output is correct |
27 |
Correct |
252 ms |
23112 KB |
Output is correct |
28 |
Correct |
225 ms |
23048 KB |
Output is correct |
29 |
Correct |
252 ms |
23200 KB |
Output is correct |
30 |
Correct |
233 ms |
23112 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
508 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
38 |
Correct |
1 ms |
476 KB |
Output is correct |
39 |
Correct |
1 ms |
504 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
1 ms |
472 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
43 |
Correct |
1 ms |
336 KB |
Output is correct |
44 |
Correct |
1 ms |
336 KB |
Output is correct |
45 |
Correct |
1 ms |
336 KB |
Output is correct |
46 |
Correct |
1 ms |
336 KB |
Output is correct |
47 |
Correct |
1 ms |
336 KB |
Output is correct |
48 |
Correct |
1 ms |
336 KB |
Output is correct |
49 |
Correct |
1 ms |
336 KB |
Output is correct |
50 |
Correct |
1 ms |
336 KB |
Output is correct |
51 |
Correct |
1 ms |
592 KB |
Output is correct |
52 |
Correct |
1 ms |
592 KB |
Output is correct |
53 |
Correct |
1 ms |
592 KB |
Output is correct |
54 |
Correct |
1 ms |
488 KB |
Output is correct |
55 |
Correct |
2 ms |
592 KB |
Output is correct |
56 |
Correct |
1 ms |
592 KB |
Output is correct |
57 |
Correct |
1 ms |
592 KB |
Output is correct |
58 |
Correct |
2 ms |
592 KB |
Output is correct |
59 |
Correct |
2 ms |
592 KB |
Output is correct |
60 |
Correct |
1 ms |
848 KB |
Output is correct |
61 |
Correct |
150 ms |
22600 KB |
Output is correct |
62 |
Correct |
115 ms |
23112 KB |
Output is correct |
63 |
Correct |
212 ms |
22600 KB |
Output is correct |
64 |
Correct |
148 ms |
23156 KB |
Output is correct |
65 |
Correct |
199 ms |
23112 KB |
Output is correct |
66 |
Correct |
178 ms |
22916 KB |
Output is correct |
67 |
Correct |
166 ms |
22600 KB |
Output is correct |
68 |
Correct |
238 ms |
23112 KB |
Output is correct |
69 |
Correct |
235 ms |
22600 KB |
Output is correct |
70 |
Correct |
225 ms |
22600 KB |
Output is correct |