#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = b; i >= a; i --)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPD(i, n) for (int i = n - 1; i >= 0; --i)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void setupIO(){
#define name "Whisper"
//Phu Trong from Nguyen Tat Thanh High School for gifted student
srand(time(NULL));
cin.tie(nullptr)->sync_with_stdio(false); cout.tie(nullptr);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
cout << fixed << setprecision(10);
}
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
const int MAX = 3e5 + 5;
int nArr, numSeg;
int a[MAX], val[MAX];
void Whisper(){
cin >> nArr >> numSeg;
for (int i = 1; i <= nArr; ++i) cin >> a[i];
a[0] = a[nArr + 1] = -1e18;
int cnt = 0;
vector<int> comp;
for (int i = 0; i <= nArr + 1; ){
int sum = 0; int r = i;
while(r <= nArr + 1 && (a[r] >= 0) == (a[i] >= 0)){
sum += a[r];
++r;
}
comp.push_back(sum);
if(sum >= 0){
++cnt;
}
i = r;
}
for (int i = 0; i < (int)comp.size(); ++i) val[i] = comp[i];
set<pair<int, int>> s; set<int> pos;
for (int i = 0; i < (int)comp.size(); ++i){
pos.insert(i);
s.insert(make_pair(abs(comp[i]), i));
}
for (int i = cnt; i > numSeg; --i){
int v = (*s.begin()).first;
int id = (*s.begin()).second;
auto new_pos = pos.lower_bound(id);
auto l = prev(new_pos), r = next(new_pos);
s.erase(make_pair(abs(val[*l]), *l));
s.erase(make_pair(abs(val[*r]), *r));
s.erase(make_pair(abs(val[id]), id));
val[id] += val[*l]; val[id] += val[*r];
pos.erase(*l); pos.erase(*r);
s.insert(make_pair(abs(val[id]), id));
}
int ans = 0;
for (auto&v : pos){
if(val[v] >= 0) ans += val[v];
}
cout << ans;
}
signed main(){
setupIO();
int Test = 1;
// cin >> Test;
for ( int i = 1 ; i <= Test ; i++ ){
Whisper();
if (i < Test) cout << '\n';
}
}
Compilation message
feast.cpp: In function 'void Whisper()':
feast.cpp:71:13: warning: unused variable 'v' [-Wunused-variable]
71 | int v = (*s.begin()).first;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
5460 KB |
Output is correct |
2 |
Correct |
23 ms |
5720 KB |
Output is correct |
3 |
Correct |
30 ms |
5604 KB |
Output is correct |
4 |
Correct |
39 ms |
5712 KB |
Output is correct |
5 |
Correct |
24 ms |
5724 KB |
Output is correct |
6 |
Correct |
24 ms |
5436 KB |
Output is correct |
7 |
Correct |
29 ms |
5460 KB |
Output is correct |
8 |
Correct |
29 ms |
5696 KB |
Output is correct |
9 |
Correct |
23 ms |
5464 KB |
Output is correct |
10 |
Correct |
24 ms |
5456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
4184 KB |
Output is correct |
2 |
Correct |
15 ms |
4012 KB |
Output is correct |
3 |
Correct |
15 ms |
3928 KB |
Output is correct |
4 |
Correct |
15 ms |
3932 KB |
Output is correct |
5 |
Correct |
23 ms |
5436 KB |
Output is correct |
6 |
Correct |
15 ms |
3932 KB |
Output is correct |
7 |
Correct |
15 ms |
4036 KB |
Output is correct |
8 |
Correct |
24 ms |
5684 KB |
Output is correct |
9 |
Correct |
22 ms |
5420 KB |
Output is correct |
10 |
Correct |
15 ms |
3912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
237 ms |
25396 KB |
Output is correct |
2 |
Correct |
301 ms |
25220 KB |
Output is correct |
3 |
Correct |
321 ms |
25260 KB |
Output is correct |
4 |
Correct |
267 ms |
25168 KB |
Output is correct |
5 |
Correct |
230 ms |
25276 KB |
Output is correct |
6 |
Correct |
267 ms |
25440 KB |
Output is correct |
7 |
Correct |
232 ms |
25416 KB |
Output is correct |
8 |
Correct |
252 ms |
25288 KB |
Output is correct |
9 |
Correct |
319 ms |
25804 KB |
Output is correct |
10 |
Correct |
274 ms |
25540 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 |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2516 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 |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2516 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 |
2392 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 |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 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 |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2516 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 |
2392 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 |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 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 |
2648 KB |
Output is correct |
25 |
Correct |
1 ms |
2648 KB |
Output is correct |
26 |
Correct |
2 ms |
2652 KB |
Output is correct |
27 |
Correct |
2 ms |
2536 KB |
Output is correct |
28 |
Correct |
1 ms |
2648 KB |
Output is correct |
29 |
Correct |
2 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
5460 KB |
Output is correct |
2 |
Correct |
23 ms |
5720 KB |
Output is correct |
3 |
Correct |
30 ms |
5604 KB |
Output is correct |
4 |
Correct |
39 ms |
5712 KB |
Output is correct |
5 |
Correct |
24 ms |
5724 KB |
Output is correct |
6 |
Correct |
24 ms |
5436 KB |
Output is correct |
7 |
Correct |
29 ms |
5460 KB |
Output is correct |
8 |
Correct |
29 ms |
5696 KB |
Output is correct |
9 |
Correct |
23 ms |
5464 KB |
Output is correct |
10 |
Correct |
24 ms |
5456 KB |
Output is correct |
11 |
Correct |
15 ms |
4184 KB |
Output is correct |
12 |
Correct |
15 ms |
4012 KB |
Output is correct |
13 |
Correct |
15 ms |
3928 KB |
Output is correct |
14 |
Correct |
15 ms |
3932 KB |
Output is correct |
15 |
Correct |
23 ms |
5436 KB |
Output is correct |
16 |
Correct |
15 ms |
3932 KB |
Output is correct |
17 |
Correct |
15 ms |
4036 KB |
Output is correct |
18 |
Correct |
24 ms |
5684 KB |
Output is correct |
19 |
Correct |
22 ms |
5420 KB |
Output is correct |
20 |
Correct |
15 ms |
3912 KB |
Output is correct |
21 |
Correct |
237 ms |
25396 KB |
Output is correct |
22 |
Correct |
301 ms |
25220 KB |
Output is correct |
23 |
Correct |
321 ms |
25260 KB |
Output is correct |
24 |
Correct |
267 ms |
25168 KB |
Output is correct |
25 |
Correct |
230 ms |
25276 KB |
Output is correct |
26 |
Correct |
267 ms |
25440 KB |
Output is correct |
27 |
Correct |
232 ms |
25416 KB |
Output is correct |
28 |
Correct |
252 ms |
25288 KB |
Output is correct |
29 |
Correct |
319 ms |
25804 KB |
Output is correct |
30 |
Correct |
274 ms |
25540 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 |
2648 KB |
Output is correct |
39 |
Correct |
1 ms |
2396 KB |
Output is correct |
40 |
Correct |
1 ms |
2516 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 |
2392 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 |
2392 KB |
Output is correct |
48 |
Correct |
1 ms |
2396 KB |
Output is correct |
49 |
Correct |
1 ms |
2392 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 |
2648 KB |
Output is correct |
55 |
Correct |
1 ms |
2648 KB |
Output is correct |
56 |
Correct |
2 ms |
2652 KB |
Output is correct |
57 |
Correct |
2 ms |
2536 KB |
Output is correct |
58 |
Correct |
1 ms |
2648 KB |
Output is correct |
59 |
Correct |
2 ms |
2652 KB |
Output is correct |
60 |
Correct |
2 ms |
2652 KB |
Output is correct |
61 |
Correct |
149 ms |
24944 KB |
Output is correct |
62 |
Correct |
113 ms |
25632 KB |
Output is correct |
63 |
Correct |
247 ms |
25060 KB |
Output is correct |
64 |
Correct |
171 ms |
25504 KB |
Output is correct |
65 |
Correct |
209 ms |
25432 KB |
Output is correct |
66 |
Correct |
255 ms |
25296 KB |
Output is correct |
67 |
Correct |
209 ms |
25036 KB |
Output is correct |
68 |
Correct |
240 ms |
25484 KB |
Output is correct |
69 |
Correct |
263 ms |
24924 KB |
Output is correct |
70 |
Correct |
261 ms |
24840 KB |
Output is correct |