#include<bits/stdc++.h>
#include<fstream>
using namespace std;
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
const ld PI = 3.14159265359;
using u128 = __uint128_t;
const int x[4] = {2, -2, 2, -2};
const int y[4] = {2, 2, -2, -2};
const ll mod = 1e9 + 7;
const int mxn = 1e6 + 69, mxq = 1e5 + 5, sq = 500, mxv = 2e7 + 5;
//const int base = (1 <<18);
const ll inf = 1e18 + 5, neg = -69420;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
//const int x[9] = {0, 1, 1, -1, -1, 2, -2, 2, -2};
//const inty[9] = {0, 2, -2, 2, -2, 1, 1, -1, -1};
int n, k;
ll a[mxn + 1], val[mxn + 1];
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("COLLEGE.INP", "r", stdin);
//freopen("COLLEGE.OUT", "w", stdout);
cin >> n >> k;
vt<ll>comp;
a[0] = -inf; a[n + 1] = -inf;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
int cnt = 0;
for(int i = 0; i <= n + 1;){
ll sm = 0;
int r = i;
while(r <= n + 1 && (a[r] >= 0) == (a[i] >= 0)){
sm += a[r++];
}
comp.pb(sm);
if(sm >= 0){
cnt++;
}
i = r;
}
for(int i = 0; i < sz(comp); i++)val[i] = comp[i];
set<pair<ll, ll>>lar; set<int>pos;
for(int i = 0; i < sz(comp); i++){
lar.insert(mpp(abs(comp[i]), i)); pos.insert(i);
}
for(int i = cnt; i > k; i--){
auto [va, id] = *lar.begin();
auto it = pos.lower_bound(id), lw = prev(it), hg = next(it);
lar.erase(mpp(abs(val[*hg]), *hg)); lar.erase(mpp(abs(val[*lw]), *lw));lar.erase(mpp(abs(val[id]), id));
val[id] += val[*lw]; val[id] += val[*hg];
pos.erase(*lw); pos.erase(*hg); lar.insert(mpp(abs(val[id]), id));
}
ll ans = 0;
for(auto it: lar){
if(val[it.se] >= 0)ans += val[it.se];
}
cout << ans;
return(0);
}
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:62:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
62 | auto [va, id] = *lar.begin();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
7228 KB |
Output is correct |
2 |
Correct |
31 ms |
7256 KB |
Output is correct |
3 |
Correct |
27 ms |
7260 KB |
Output is correct |
4 |
Correct |
32 ms |
7364 KB |
Output is correct |
5 |
Correct |
26 ms |
7256 KB |
Output is correct |
6 |
Correct |
26 ms |
7248 KB |
Output is correct |
7 |
Correct |
27 ms |
7248 KB |
Output is correct |
8 |
Correct |
30 ms |
7256 KB |
Output is correct |
9 |
Correct |
27 ms |
7356 KB |
Output is correct |
10 |
Correct |
27 ms |
7512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
5464 KB |
Output is correct |
2 |
Correct |
17 ms |
5468 KB |
Output is correct |
3 |
Correct |
17 ms |
5456 KB |
Output is correct |
4 |
Correct |
19 ms |
5468 KB |
Output is correct |
5 |
Correct |
26 ms |
7356 KB |
Output is correct |
6 |
Correct |
17 ms |
5448 KB |
Output is correct |
7 |
Correct |
19 ms |
5464 KB |
Output is correct |
8 |
Correct |
31 ms |
7260 KB |
Output is correct |
9 |
Correct |
25 ms |
7248 KB |
Output is correct |
10 |
Correct |
17 ms |
5708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
227 ms |
26892 KB |
Output is correct |
2 |
Correct |
234 ms |
26820 KB |
Output is correct |
3 |
Correct |
250 ms |
27080 KB |
Output is correct |
4 |
Correct |
220 ms |
26824 KB |
Output is correct |
5 |
Correct |
222 ms |
26948 KB |
Output is correct |
6 |
Correct |
231 ms |
27184 KB |
Output is correct |
7 |
Correct |
263 ms |
27172 KB |
Output is correct |
8 |
Correct |
210 ms |
26824 KB |
Output is correct |
9 |
Correct |
240 ms |
27332 KB |
Output is correct |
10 |
Correct |
248 ms |
27588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2512 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2512 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
3 ms |
2812 KB |
Output is correct |
26 |
Correct |
1 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
1 ms |
2652 KB |
Output is correct |
29 |
Correct |
1 ms |
2652 KB |
Output is correct |
30 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
7228 KB |
Output is correct |
2 |
Correct |
31 ms |
7256 KB |
Output is correct |
3 |
Correct |
27 ms |
7260 KB |
Output is correct |
4 |
Correct |
32 ms |
7364 KB |
Output is correct |
5 |
Correct |
26 ms |
7256 KB |
Output is correct |
6 |
Correct |
26 ms |
7248 KB |
Output is correct |
7 |
Correct |
27 ms |
7248 KB |
Output is correct |
8 |
Correct |
30 ms |
7256 KB |
Output is correct |
9 |
Correct |
27 ms |
7356 KB |
Output is correct |
10 |
Correct |
27 ms |
7512 KB |
Output is correct |
11 |
Correct |
17 ms |
5464 KB |
Output is correct |
12 |
Correct |
17 ms |
5468 KB |
Output is correct |
13 |
Correct |
17 ms |
5456 KB |
Output is correct |
14 |
Correct |
19 ms |
5468 KB |
Output is correct |
15 |
Correct |
26 ms |
7356 KB |
Output is correct |
16 |
Correct |
17 ms |
5448 KB |
Output is correct |
17 |
Correct |
19 ms |
5464 KB |
Output is correct |
18 |
Correct |
31 ms |
7260 KB |
Output is correct |
19 |
Correct |
25 ms |
7248 KB |
Output is correct |
20 |
Correct |
17 ms |
5708 KB |
Output is correct |
21 |
Correct |
227 ms |
26892 KB |
Output is correct |
22 |
Correct |
234 ms |
26820 KB |
Output is correct |
23 |
Correct |
250 ms |
27080 KB |
Output is correct |
24 |
Correct |
220 ms |
26824 KB |
Output is correct |
25 |
Correct |
222 ms |
26948 KB |
Output is correct |
26 |
Correct |
231 ms |
27184 KB |
Output is correct |
27 |
Correct |
263 ms |
27172 KB |
Output is correct |
28 |
Correct |
210 ms |
26824 KB |
Output is correct |
29 |
Correct |
240 ms |
27332 KB |
Output is correct |
30 |
Correct |
248 ms |
27588 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2392 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
1 ms |
2396 KB |
Output is correct |
40 |
Correct |
1 ms |
2512 KB |
Output is correct |
41 |
Correct |
1 ms |
2396 KB |
Output is correct |
42 |
Correct |
1 ms |
2396 KB |
Output is correct |
43 |
Correct |
1 ms |
2396 KB |
Output is correct |
44 |
Correct |
1 ms |
2396 KB |
Output is correct |
45 |
Correct |
1 ms |
2396 KB |
Output is correct |
46 |
Correct |
1 ms |
2396 KB |
Output is correct |
47 |
Correct |
1 ms |
2396 KB |
Output is correct |
48 |
Correct |
1 ms |
2392 KB |
Output is correct |
49 |
Correct |
1 ms |
2396 KB |
Output is correct |
50 |
Correct |
1 ms |
2396 KB |
Output is correct |
51 |
Correct |
1 ms |
2652 KB |
Output is correct |
52 |
Correct |
1 ms |
2652 KB |
Output is correct |
53 |
Correct |
1 ms |
2652 KB |
Output is correct |
54 |
Correct |
1 ms |
2652 KB |
Output is correct |
55 |
Correct |
3 ms |
2812 KB |
Output is correct |
56 |
Correct |
1 ms |
2652 KB |
Output is correct |
57 |
Correct |
1 ms |
2652 KB |
Output is correct |
58 |
Correct |
1 ms |
2652 KB |
Output is correct |
59 |
Correct |
1 ms |
2652 KB |
Output is correct |
60 |
Correct |
1 ms |
2652 KB |
Output is correct |
61 |
Correct |
178 ms |
26604 KB |
Output is correct |
62 |
Correct |
119 ms |
27336 KB |
Output is correct |
63 |
Correct |
226 ms |
26712 KB |
Output is correct |
64 |
Correct |
150 ms |
27016 KB |
Output is correct |
65 |
Correct |
183 ms |
27160 KB |
Output is correct |
66 |
Correct |
221 ms |
27080 KB |
Output is correct |
67 |
Correct |
192 ms |
26684 KB |
Output is correct |
68 |
Correct |
247 ms |
27076 KB |
Output is correct |
69 |
Correct |
205 ms |
26580 KB |
Output is correct |
70 |
Correct |
259 ms |
26564 KB |
Output is correct |