#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
vector<long long> arr (N);
vector<long long> comp;
vector< vector<long long> > g (N);
bool is_rem [N];
template<typename T>
struct Kadane {
long long best;
long long cur;
int s, start, end;
Kadane (const vector<T> &a) {
best = cur = s = start = end = 0LL;
for (int i = 0; i < (int) a.size(); i++) {
cur += a[i];
if (cur < 0) {
cur = 0;
s = i + 1;
}
if (cur > best) {
best = cur;
start = s;
end = i + 1;
}
}
}
};
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> arr[i];
for (int i = 0; i < n; i++) {
if (comp.empty() || ( (arr[i] >= 0) ^ (comp.back() >= 0) ) == true ) {
comp.emplace_back(arr[i]);
continue;
}
long long cur = arr[i];
while (!comp.empty() && ( (arr[i] >= 0) ^ (comp.back() >= 0) ) == false ) {
cur += comp.back();
comp.pop_back();
}
comp.emplace_back(cur);
}
int tot = 0;
for (int i = 0; i < (int) comp.size() ; i++) if (comp[i] > 0) ++tot;
if (tot <= k) {
long long s = 0;
for (auto to : comp) s += (to >= 0 ? to : 0);
cout << s << '\n';
return 0;
}
int primeiro = -1;
int ultimo = -1;
for (int i = 0; i < (int) comp.size(); i++) {
if (comp[i] > 0) {
if (primeiro == -1) primeiro = i;
else ultimo = i + 1;
}
}
vector<int> neg;
for (int i = primeiro; i < ultimo; i++) {
if (comp[i] < 0) {
neg.emplace_back(i);
}
}
sort (neg.begin(), neg.end(), [&] (int x, int y) {
return comp[x] < comp[y];
});
int blocos = 1;
for (auto to : neg) {
if (blocos < k) {
++blocos;
is_rem[to] = true;
}
}
assert (blocos == k);
int idx = 0;
for (int i = primeiro; i < ultimo; i++) {
if (is_rem[i]) {
++idx;
continue;
}
g[idx].emplace_back(comp[i]);
}
long long res = 0;
for (auto vec : g) {
if (!vec.empty()) {
Kadane <long long> solve (vec);
res += solve.best;
}
}
cout << res << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
9728 KB |
Output is correct |
2 |
Correct |
55 ms |
9848 KB |
Output is correct |
3 |
Correct |
52 ms |
9788 KB |
Output is correct |
4 |
Correct |
51 ms |
9728 KB |
Output is correct |
5 |
Correct |
50 ms |
9728 KB |
Output is correct |
6 |
Correct |
51 ms |
9792 KB |
Output is correct |
7 |
Correct |
51 ms |
9728 KB |
Output is correct |
8 |
Correct |
52 ms |
9728 KB |
Output is correct |
9 |
Correct |
53 ms |
9728 KB |
Output is correct |
10 |
Correct |
51 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
9728 KB |
Output is correct |
2 |
Correct |
35 ms |
9728 KB |
Output is correct |
3 |
Correct |
33 ms |
9728 KB |
Output is correct |
4 |
Correct |
33 ms |
9728 KB |
Output is correct |
5 |
Correct |
56 ms |
9652 KB |
Output is correct |
6 |
Correct |
34 ms |
9728 KB |
Output is correct |
7 |
Correct |
33 ms |
9728 KB |
Output is correct |
8 |
Correct |
52 ms |
9848 KB |
Output is correct |
9 |
Correct |
49 ms |
9728 KB |
Output is correct |
10 |
Correct |
36 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
14384 KB |
Output is correct |
2 |
Correct |
68 ms |
14444 KB |
Output is correct |
3 |
Correct |
69 ms |
14444 KB |
Output is correct |
4 |
Correct |
72 ms |
14316 KB |
Output is correct |
5 |
Correct |
72 ms |
14384 KB |
Output is correct |
6 |
Correct |
67 ms |
14444 KB |
Output is correct |
7 |
Correct |
70 ms |
14444 KB |
Output is correct |
8 |
Correct |
69 ms |
14444 KB |
Output is correct |
9 |
Correct |
70 ms |
14444 KB |
Output is correct |
10 |
Correct |
71 ms |
14444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Incorrect |
11 ms |
9728 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Incorrect |
11 ms |
9728 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Incorrect |
11 ms |
9728 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
9728 KB |
Output is correct |
2 |
Correct |
55 ms |
9848 KB |
Output is correct |
3 |
Correct |
52 ms |
9788 KB |
Output is correct |
4 |
Correct |
51 ms |
9728 KB |
Output is correct |
5 |
Correct |
50 ms |
9728 KB |
Output is correct |
6 |
Correct |
51 ms |
9792 KB |
Output is correct |
7 |
Correct |
51 ms |
9728 KB |
Output is correct |
8 |
Correct |
52 ms |
9728 KB |
Output is correct |
9 |
Correct |
53 ms |
9728 KB |
Output is correct |
10 |
Correct |
51 ms |
9728 KB |
Output is correct |
11 |
Correct |
33 ms |
9728 KB |
Output is correct |
12 |
Correct |
35 ms |
9728 KB |
Output is correct |
13 |
Correct |
33 ms |
9728 KB |
Output is correct |
14 |
Correct |
33 ms |
9728 KB |
Output is correct |
15 |
Correct |
56 ms |
9652 KB |
Output is correct |
16 |
Correct |
34 ms |
9728 KB |
Output is correct |
17 |
Correct |
33 ms |
9728 KB |
Output is correct |
18 |
Correct |
52 ms |
9848 KB |
Output is correct |
19 |
Correct |
49 ms |
9728 KB |
Output is correct |
20 |
Correct |
36 ms |
9728 KB |
Output is correct |
21 |
Correct |
69 ms |
14384 KB |
Output is correct |
22 |
Correct |
68 ms |
14444 KB |
Output is correct |
23 |
Correct |
69 ms |
14444 KB |
Output is correct |
24 |
Correct |
72 ms |
14316 KB |
Output is correct |
25 |
Correct |
72 ms |
14384 KB |
Output is correct |
26 |
Correct |
67 ms |
14444 KB |
Output is correct |
27 |
Correct |
70 ms |
14444 KB |
Output is correct |
28 |
Correct |
69 ms |
14444 KB |
Output is correct |
29 |
Correct |
70 ms |
14444 KB |
Output is correct |
30 |
Correct |
71 ms |
14444 KB |
Output is correct |
31 |
Correct |
11 ms |
9728 KB |
Output is correct |
32 |
Correct |
10 ms |
9728 KB |
Output is correct |
33 |
Incorrect |
11 ms |
9728 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |