#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) || k == 1) {
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;
}
cout << (neg ? pre + post : sum);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
2652 KB |
Output is correct |
2 |
Correct |
69 ms |
2652 KB |
Output is correct |
3 |
Correct |
110 ms |
2652 KB |
Output is correct |
4 |
Correct |
69 ms |
2652 KB |
Output is correct |
5 |
Correct |
68 ms |
2652 KB |
Output is correct |
6 |
Correct |
85 ms |
2652 KB |
Output is correct |
7 |
Correct |
67 ms |
2648 KB |
Output is correct |
8 |
Correct |
81 ms |
2652 KB |
Output is correct |
9 |
Correct |
71 ms |
2652 KB |
Output is correct |
10 |
Correct |
77 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
34596 KB |
Output is correct |
2 |
Correct |
60 ms |
35548 KB |
Output is correct |
3 |
Correct |
58 ms |
34640 KB |
Output is correct |
4 |
Correct |
36 ms |
2648 KB |
Output is correct |
5 |
Correct |
92 ms |
34388 KB |
Output is correct |
6 |
Correct |
56 ms |
34640 KB |
Output is correct |
7 |
Correct |
32 ms |
2648 KB |
Output is correct |
8 |
Correct |
96 ms |
35160 KB |
Output is correct |
9 |
Correct |
105 ms |
34384 KB |
Output is correct |
10 |
Correct |
34 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
35004 KB |
Output is correct |
2 |
Correct |
97 ms |
34640 KB |
Output is correct |
3 |
Correct |
106 ms |
34900 KB |
Output is correct |
4 |
Correct |
104 ms |
34896 KB |
Output is correct |
5 |
Correct |
101 ms |
35152 KB |
Output is correct |
6 |
Correct |
101 ms |
35156 KB |
Output is correct |
7 |
Correct |
101 ms |
35412 KB |
Output is correct |
8 |
Correct |
110 ms |
35500 KB |
Output is correct |
9 |
Correct |
103 ms |
35408 KB |
Output is correct |
10 |
Correct |
100 ms |
35280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
348 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 |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
348 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 |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 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 |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
348 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 |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
6452 KB |
Output is correct |
22 |
Correct |
27 ms |
51660 KB |
Output is correct |
23 |
Correct |
6 ms |
12200 KB |
Output is correct |
24 |
Correct |
4 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 |
5 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 |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
2652 KB |
Output is correct |
2 |
Correct |
69 ms |
2652 KB |
Output is correct |
3 |
Correct |
110 ms |
2652 KB |
Output is correct |
4 |
Correct |
69 ms |
2652 KB |
Output is correct |
5 |
Correct |
68 ms |
2652 KB |
Output is correct |
6 |
Correct |
85 ms |
2652 KB |
Output is correct |
7 |
Correct |
67 ms |
2648 KB |
Output is correct |
8 |
Correct |
81 ms |
2652 KB |
Output is correct |
9 |
Correct |
71 ms |
2652 KB |
Output is correct |
10 |
Correct |
77 ms |
2652 KB |
Output is correct |
11 |
Correct |
60 ms |
34596 KB |
Output is correct |
12 |
Correct |
60 ms |
35548 KB |
Output is correct |
13 |
Correct |
58 ms |
34640 KB |
Output is correct |
14 |
Correct |
36 ms |
2648 KB |
Output is correct |
15 |
Correct |
92 ms |
34388 KB |
Output is correct |
16 |
Correct |
56 ms |
34640 KB |
Output is correct |
17 |
Correct |
32 ms |
2648 KB |
Output is correct |
18 |
Correct |
96 ms |
35160 KB |
Output is correct |
19 |
Correct |
105 ms |
34384 KB |
Output is correct |
20 |
Correct |
34 ms |
2652 KB |
Output is correct |
21 |
Correct |
104 ms |
35004 KB |
Output is correct |
22 |
Correct |
97 ms |
34640 KB |
Output is correct |
23 |
Correct |
106 ms |
34900 KB |
Output is correct |
24 |
Correct |
104 ms |
34896 KB |
Output is correct |
25 |
Correct |
101 ms |
35152 KB |
Output is correct |
26 |
Correct |
101 ms |
35156 KB |
Output is correct |
27 |
Correct |
101 ms |
35412 KB |
Output is correct |
28 |
Correct |
110 ms |
35500 KB |
Output is correct |
29 |
Correct |
103 ms |
35408 KB |
Output is correct |
30 |
Correct |
100 ms |
35280 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
0 ms |
604 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
604 KB |
Output is correct |
45 |
Correct |
0 ms |
604 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
604 KB |
Output is correct |
48 |
Correct |
0 ms |
344 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
3 ms |
6452 KB |
Output is correct |
52 |
Correct |
27 ms |
51660 KB |
Output is correct |
53 |
Correct |
6 ms |
12200 KB |
Output is correct |
54 |
Correct |
4 ms |
7004 KB |
Output is correct |
55 |
Correct |
5 ms |
9564 KB |
Output is correct |
56 |
Correct |
3 ms |
5980 KB |
Output is correct |
57 |
Correct |
5 ms |
9052 KB |
Output is correct |
58 |
Correct |
1 ms |
1372 KB |
Output is correct |
59 |
Correct |
1 ms |
1372 KB |
Output is correct |
60 |
Correct |
1 ms |
600 KB |
Output is correct |
61 |
Incorrect |
75 ms |
5656 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |