#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
#define endl '\n'
#define pl(var) " [" << #var << ": " << (var) << "] "
#define ll long long
void solve() {
// dp[i][j] = the max sum that can be obtained if j subsegments are chosen and last subsegment ends at i
// dp[i][j] = dp[0..i][j-1]
int n, k; cin >> n >> k;
vector<ll int> arr(n); for (int i = 0; i < n; i++) {
cin >> arr[i];
}
bool good = 1;
bool one = 0;
for (int i = 0; i < n; i++) {
if (arr[i] < 0 && !one) {
good = 0;
one = 1;
} else if (arr[i] < 0 && one) {
one = 0;
break;
}
}
if (good) {
ll int cur = 0;
for (int i = 0; i < n; i++) {
cur += arr[i];
}
cout << cur << endl;
return;
}
if (one) {
// depends on the value of k;
ll int start = 0;
ll int end = 0;
ll int all = 0;
bool instart = 1;
for (int i = 0; i < n; i++) {
all += arr[i];
if (arr[i] < 0) {
instart = 0;
continue;
}
if (instart) {
start += arr[i];
} else {
end += arr[i];
}
}
if (k == 1) {
cout << max({start, end, all}) << endl;
} else {
cout << start + end << endl;
}
return;
}
vector<vector<ll int>> dp(n + 1, vector<ll int>(k + 1, 0));
ll int ans = 0;
ll int mn = 0;
ll int pref = 0;
for (int i = 0; i < n; i++) {
pref += arr[i];
mn = min(pref, mn);
dp[i][1] = max(pref - mn, pref);
ans = max(ans, dp[i][1]);
if (arr[i] < 0) good = 0;
}
for (int i = 2; i <= k; i++) {
for (int j = i - 1; j < n; j++) {
ll int cur = arr[j];
ll int best = arr[j];
for (int k = j - 1; k >= 0; k--) {
dp[j][i] = max(dp[k][i - 1] + best, dp[j][i]);
cur += arr[k];
best = max(best, cur);
}
ans = max(ans, dp[j][i]);
}
}
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
while (T--)
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
2516 KB |
Output is correct |
2 |
Correct |
30 ms |
2644 KB |
Output is correct |
3 |
Correct |
33 ms |
2644 KB |
Output is correct |
4 |
Correct |
31 ms |
2644 KB |
Output is correct |
5 |
Correct |
32 ms |
2644 KB |
Output is correct |
6 |
Correct |
28 ms |
2516 KB |
Output is correct |
7 |
Correct |
28 ms |
2516 KB |
Output is correct |
8 |
Correct |
27 ms |
2644 KB |
Output is correct |
9 |
Correct |
27 ms |
2516 KB |
Output is correct |
10 |
Correct |
29 ms |
2516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
2516 KB |
Output is correct |
2 |
Correct |
29 ms |
2644 KB |
Output is correct |
3 |
Correct |
23 ms |
2516 KB |
Output is correct |
4 |
Correct |
21 ms |
3668 KB |
Output is correct |
5 |
Correct |
34 ms |
5396 KB |
Output is correct |
6 |
Correct |
20 ms |
3624 KB |
Output is correct |
7 |
Correct |
21 ms |
3788 KB |
Output is correct |
8 |
Correct |
30 ms |
5456 KB |
Output is correct |
9 |
Correct |
39 ms |
5264 KB |
Output is correct |
10 |
Correct |
23 ms |
3660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
18764 KB |
Output is correct |
2 |
Correct |
47 ms |
18600 KB |
Output is correct |
3 |
Correct |
51 ms |
18836 KB |
Output is correct |
4 |
Correct |
46 ms |
18656 KB |
Output is correct |
5 |
Correct |
50 ms |
18760 KB |
Output is correct |
6 |
Correct |
65 ms |
19036 KB |
Output is correct |
7 |
Correct |
50 ms |
18920 KB |
Output is correct |
8 |
Correct |
47 ms |
18768 KB |
Output is correct |
9 |
Correct |
47 ms |
18952 KB |
Output is correct |
10 |
Correct |
46 ms |
19016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
4 ms |
456 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
404 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
4 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
4 ms |
456 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
404 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
4 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
683 ms |
3412 KB |
Output is correct |
22 |
Execution timed out |
1063 ms |
25940 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
2516 KB |
Output is correct |
2 |
Correct |
30 ms |
2644 KB |
Output is correct |
3 |
Correct |
33 ms |
2644 KB |
Output is correct |
4 |
Correct |
31 ms |
2644 KB |
Output is correct |
5 |
Correct |
32 ms |
2644 KB |
Output is correct |
6 |
Correct |
28 ms |
2516 KB |
Output is correct |
7 |
Correct |
28 ms |
2516 KB |
Output is correct |
8 |
Correct |
27 ms |
2644 KB |
Output is correct |
9 |
Correct |
27 ms |
2516 KB |
Output is correct |
10 |
Correct |
29 ms |
2516 KB |
Output is correct |
11 |
Correct |
21 ms |
2516 KB |
Output is correct |
12 |
Correct |
29 ms |
2644 KB |
Output is correct |
13 |
Correct |
23 ms |
2516 KB |
Output is correct |
14 |
Correct |
21 ms |
3668 KB |
Output is correct |
15 |
Correct |
34 ms |
5396 KB |
Output is correct |
16 |
Correct |
20 ms |
3624 KB |
Output is correct |
17 |
Correct |
21 ms |
3788 KB |
Output is correct |
18 |
Correct |
30 ms |
5456 KB |
Output is correct |
19 |
Correct |
39 ms |
5264 KB |
Output is correct |
20 |
Correct |
23 ms |
3660 KB |
Output is correct |
21 |
Correct |
48 ms |
18764 KB |
Output is correct |
22 |
Correct |
47 ms |
18600 KB |
Output is correct |
23 |
Correct |
51 ms |
18836 KB |
Output is correct |
24 |
Correct |
46 ms |
18656 KB |
Output is correct |
25 |
Correct |
50 ms |
18760 KB |
Output is correct |
26 |
Correct |
65 ms |
19036 KB |
Output is correct |
27 |
Correct |
50 ms |
18920 KB |
Output is correct |
28 |
Correct |
47 ms |
18768 KB |
Output is correct |
29 |
Correct |
47 ms |
18952 KB |
Output is correct |
30 |
Correct |
46 ms |
19016 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
3 ms |
340 KB |
Output is correct |
42 |
Correct |
4 ms |
456 KB |
Output is correct |
43 |
Correct |
2 ms |
340 KB |
Output is correct |
44 |
Correct |
3 ms |
340 KB |
Output is correct |
45 |
Correct |
2 ms |
404 KB |
Output is correct |
46 |
Correct |
2 ms |
340 KB |
Output is correct |
47 |
Correct |
4 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
683 ms |
3412 KB |
Output is correct |
52 |
Execution timed out |
1063 ms |
25940 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |