#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 |
24 ms |
5712 KB |
Output is correct |
3 |
Correct |
23 ms |
5560 KB |
Output is correct |
4 |
Correct |
23 ms |
5720 KB |
Output is correct |
5 |
Correct |
26 ms |
5724 KB |
Output is correct |
6 |
Correct |
23 ms |
5496 KB |
Output is correct |
7 |
Correct |
24 ms |
5720 KB |
Output is correct |
8 |
Correct |
24 ms |
5724 KB |
Output is correct |
9 |
Correct |
27 ms |
5576 KB |
Output is correct |
10 |
Correct |
24 ms |
5724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
3928 KB |
Output is correct |
2 |
Correct |
15 ms |
3936 KB |
Output is correct |
3 |
Correct |
17 ms |
3932 KB |
Output is correct |
4 |
Correct |
15 ms |
3916 KB |
Output is correct |
5 |
Correct |
22 ms |
5456 KB |
Output is correct |
6 |
Correct |
15 ms |
3932 KB |
Output is correct |
7 |
Correct |
17 ms |
3928 KB |
Output is correct |
8 |
Correct |
23 ms |
5728 KB |
Output is correct |
9 |
Correct |
26 ms |
5568 KB |
Output is correct |
10 |
Correct |
15 ms |
3940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
248 ms |
25288 KB |
Output is correct |
2 |
Correct |
239 ms |
24992 KB |
Output is correct |
3 |
Correct |
242 ms |
25384 KB |
Output is correct |
4 |
Correct |
227 ms |
25072 KB |
Output is correct |
5 |
Correct |
223 ms |
25240 KB |
Output is correct |
6 |
Correct |
238 ms |
25408 KB |
Output is correct |
7 |
Correct |
286 ms |
25520 KB |
Output is correct |
8 |
Correct |
224 ms |
25172 KB |
Output is correct |
9 |
Correct |
239 ms |
25652 KB |
Output is correct |
10 |
Correct |
235 ms |
25860 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 |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 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 |
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 |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 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 |
2396 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 |
2400 KB |
Output is correct |
15 |
Correct |
1 ms |
2404 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 |
2396 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 |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 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 |
2396 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 |
2400 KB |
Output is correct |
15 |
Correct |
1 ms |
2404 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 |
2396 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 |
4 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2676 KB |
Output is correct |
28 |
Correct |
1 ms |
2648 KB |
Output is correct |
29 |
Correct |
1 ms |
2648 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 |
24 ms |
5712 KB |
Output is correct |
3 |
Correct |
23 ms |
5560 KB |
Output is correct |
4 |
Correct |
23 ms |
5720 KB |
Output is correct |
5 |
Correct |
26 ms |
5724 KB |
Output is correct |
6 |
Correct |
23 ms |
5496 KB |
Output is correct |
7 |
Correct |
24 ms |
5720 KB |
Output is correct |
8 |
Correct |
24 ms |
5724 KB |
Output is correct |
9 |
Correct |
27 ms |
5576 KB |
Output is correct |
10 |
Correct |
24 ms |
5724 KB |
Output is correct |
11 |
Correct |
15 ms |
3928 KB |
Output is correct |
12 |
Correct |
15 ms |
3936 KB |
Output is correct |
13 |
Correct |
17 ms |
3932 KB |
Output is correct |
14 |
Correct |
15 ms |
3916 KB |
Output is correct |
15 |
Correct |
22 ms |
5456 KB |
Output is correct |
16 |
Correct |
15 ms |
3932 KB |
Output is correct |
17 |
Correct |
17 ms |
3928 KB |
Output is correct |
18 |
Correct |
23 ms |
5728 KB |
Output is correct |
19 |
Correct |
26 ms |
5568 KB |
Output is correct |
20 |
Correct |
15 ms |
3940 KB |
Output is correct |
21 |
Correct |
248 ms |
25288 KB |
Output is correct |
22 |
Correct |
239 ms |
24992 KB |
Output is correct |
23 |
Correct |
242 ms |
25384 KB |
Output is correct |
24 |
Correct |
227 ms |
25072 KB |
Output is correct |
25 |
Correct |
223 ms |
25240 KB |
Output is correct |
26 |
Correct |
238 ms |
25408 KB |
Output is correct |
27 |
Correct |
286 ms |
25520 KB |
Output is correct |
28 |
Correct |
224 ms |
25172 KB |
Output is correct |
29 |
Correct |
239 ms |
25652 KB |
Output is correct |
30 |
Correct |
235 ms |
25860 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 |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
2392 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 |
2396 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 |
2400 KB |
Output is correct |
45 |
Correct |
1 ms |
2404 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 |
2396 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 |
4 ms |
2652 KB |
Output is correct |
54 |
Correct |
1 ms |
2652 KB |
Output is correct |
55 |
Correct |
1 ms |
2652 KB |
Output is correct |
56 |
Correct |
1 ms |
2652 KB |
Output is correct |
57 |
Correct |
1 ms |
2676 KB |
Output is correct |
58 |
Correct |
1 ms |
2648 KB |
Output is correct |
59 |
Correct |
1 ms |
2648 KB |
Output is correct |
60 |
Correct |
2 ms |
2652 KB |
Output is correct |
61 |
Correct |
186 ms |
25052 KB |
Output is correct |
62 |
Correct |
133 ms |
25692 KB |
Output is correct |
63 |
Correct |
263 ms |
25004 KB |
Output is correct |
64 |
Correct |
155 ms |
25544 KB |
Output is correct |
65 |
Correct |
248 ms |
25320 KB |
Output is correct |
66 |
Correct |
219 ms |
25372 KB |
Output is correct |
67 |
Correct |
190 ms |
24940 KB |
Output is correct |
68 |
Correct |
264 ms |
25540 KB |
Output is correct |
69 |
Correct |
236 ms |
24908 KB |
Output is correct |
70 |
Correct |
252 ms |
24888 KB |
Output is correct |