#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n, k;
cin >> n >> k;
vector<ll> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
if (n <= 2000 && k <= 2000) {
vector<vector<ll>> dp(n, vector<ll>(k+1));
vector<vector<ll>> dpSeg(n, vector<ll>(k+1));
for (int j = 1; j <= k; j++) {
dpSeg[0][j] = max(0ll, a[0]);
}
for (int i = 1; i < n; i++) {
for (int j = 1; j <= k; j++) {
dp[i][j] = max(dp[i-1][j], dpSeg[i-1][j]);
dpSeg[i][j] = max(dp[i-1][j-1], dpSeg[i-1][j]) + a[i];
}
}
cout << max(dp.back().back(), dpSeg.back().back());
return 0;
}
ll sum = 0;
ll pre = 0, post = 0;
bool neg = false;
for (auto &e : a) {
if (neg) post += e;
if (e < 0) { neg = true; pre = sum; }
sum += e;
}
if (!neg) {
cout << sum;
return 0;
}
if (k == 1) {
cout << max({pre, post, sum});
}
else {
cout << (pre + post);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
2652 KB |
Output is correct |
2 |
Correct |
66 ms |
2736 KB |
Output is correct |
3 |
Correct |
68 ms |
2652 KB |
Output is correct |
4 |
Correct |
68 ms |
2652 KB |
Output is correct |
5 |
Correct |
69 ms |
2652 KB |
Output is correct |
6 |
Correct |
66 ms |
2652 KB |
Output is correct |
7 |
Correct |
67 ms |
2652 KB |
Output is correct |
8 |
Correct |
68 ms |
2652 KB |
Output is correct |
9 |
Correct |
68 ms |
2652 KB |
Output is correct |
10 |
Correct |
69 ms |
2648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
2652 KB |
Output is correct |
2 |
Correct |
43 ms |
2648 KB |
Output is correct |
3 |
Correct |
34 ms |
2652 KB |
Output is correct |
4 |
Correct |
49 ms |
2652 KB |
Output is correct |
5 |
Correct |
69 ms |
5460 KB |
Output is correct |
6 |
Correct |
36 ms |
3604 KB |
Output is correct |
7 |
Correct |
35 ms |
3676 KB |
Output is correct |
8 |
Correct |
72 ms |
5456 KB |
Output is correct |
9 |
Correct |
73 ms |
5456 KB |
Output is correct |
10 |
Correct |
35 ms |
3776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
77 ms |
2752 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
4 ms |
6488 KB |
Output is correct |
22 |
Correct |
29 ms |
51604 KB |
Output is correct |
23 |
Correct |
6 ms |
12124 KB |
Output is correct |
24 |
Correct |
3 ms |
7004 KB |
Output is correct |
25 |
Correct |
5 ms |
9564 KB |
Output is correct |
26 |
Correct |
3 ms |
5980 KB |
Output is correct |
27 |
Correct |
4 ms |
9052 KB |
Output is correct |
28 |
Correct |
1 ms |
1372 KB |
Output is correct |
29 |
Correct |
1 ms |
1372 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
2652 KB |
Output is correct |
2 |
Correct |
66 ms |
2736 KB |
Output is correct |
3 |
Correct |
68 ms |
2652 KB |
Output is correct |
4 |
Correct |
68 ms |
2652 KB |
Output is correct |
5 |
Correct |
69 ms |
2652 KB |
Output is correct |
6 |
Correct |
66 ms |
2652 KB |
Output is correct |
7 |
Correct |
67 ms |
2652 KB |
Output is correct |
8 |
Correct |
68 ms |
2652 KB |
Output is correct |
9 |
Correct |
68 ms |
2652 KB |
Output is correct |
10 |
Correct |
69 ms |
2648 KB |
Output is correct |
11 |
Correct |
34 ms |
2652 KB |
Output is correct |
12 |
Correct |
43 ms |
2648 KB |
Output is correct |
13 |
Correct |
34 ms |
2652 KB |
Output is correct |
14 |
Correct |
49 ms |
2652 KB |
Output is correct |
15 |
Correct |
69 ms |
5460 KB |
Output is correct |
16 |
Correct |
36 ms |
3604 KB |
Output is correct |
17 |
Correct |
35 ms |
3676 KB |
Output is correct |
18 |
Correct |
72 ms |
5456 KB |
Output is correct |
19 |
Correct |
73 ms |
5456 KB |
Output is correct |
20 |
Correct |
35 ms |
3776 KB |
Output is correct |
21 |
Incorrect |
77 ms |
2752 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |