#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;
for (int i = 0; i < n; i++) {
if (arr[i] < 0) good = 0;
}
if (good) {
int cur = 0;
for (int i = 0; i < n; i++) {
cur += arr[i];
}
cout << cur << 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 |
Incorrect |
45 ms |
5324 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
18612 KB |
Output is correct |
2 |
Correct |
36 ms |
18932 KB |
Output is correct |
3 |
Correct |
36 ms |
18540 KB |
Output is correct |
4 |
Runtime error |
133 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
18780 KB |
Output is correct |
2 |
Correct |
52 ms |
18608 KB |
Output is correct |
3 |
Correct |
70 ms |
18768 KB |
Output is correct |
4 |
Correct |
53 ms |
18616 KB |
Output is correct |
5 |
Correct |
52 ms |
18828 KB |
Output is correct |
6 |
Correct |
51 ms |
18936 KB |
Output is correct |
7 |
Correct |
52 ms |
18996 KB |
Output is correct |
8 |
Correct |
52 ms |
18716 KB |
Output is correct |
9 |
Correct |
53 ms |
19052 KB |
Output is correct |
10 |
Correct |
64 ms |
18912 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 |
1 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 |
0 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
4 ms |
428 KB |
Output is correct |
12 |
Correct |
5 ms |
464 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
4 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
3 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 |
2 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
4 ms |
428 KB |
Output is correct |
12 |
Correct |
5 ms |
464 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
4 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
3 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 |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
797 ms |
3436 KB |
Output is correct |
22 |
Execution timed out |
1092 ms |
25972 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
45 ms |
5324 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |