#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
bool maximize (T1 &a, T2 b) {
if(a < b) {a = b; return true;} return false;
}
template <class T1, class T2>
bool minimize (T1 &a, T2 b) {
if(a > b) {a = b; return true;} return false;
}
#define ll long long
#define el '\n'
#define fi first
#define se second
#define MASK(i) (1LL << (i))
#define BIT(mask, i) (((mask) >> (i)) & 1)
const int oo = (int)1e9 + 7;
const ll INF = (ll)1e18 + 7LL;
const int N = 555;
int n, k;
ll a[N];
void init (void) {
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
}
long long pre[N], cost[N][N], f[N][N];
void solve (void) {
for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + a[i];
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
long long mx_sum = -INF;
long long minPre = pre[i - 1];
for (int k = i; k <= j; k++){
maximize(mx_sum, pre[k] - minPre);
minimize(minPre, pre[k]);
}
cost[i][j] = mx_sum;
// cout << i << ' ' << j << ' ' << cost[i][j] << el;
}
}
memset(f, 0, sizeof f);
f[0][0] = 0;
for (int i = 1; i <= n; i++) {
for (int t = 0; t <= k; t++) {
for (int j = 0; j < i; j++) {
maximize(f[i][t], f[j][t]);
if(t >= 1) maximize(f[i][t], f[j][t - 1] + cost[j + 1][i]);
}
}
}
long long ans = -INF;
for (int i = 1; i <= n; i++) maximize(ans, f[i][k]);
cout << ans;
}
int main (void) {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen ("test.inp", "r", stdin);
// freopen ("test.out", "w", stdout);
init();
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4688 KB |
Output is correct |
2 |
Correct |
1 ms |
4688 KB |
Output is correct |
3 |
Correct |
1 ms |
4688 KB |
Output is correct |
4 |
Correct |
1 ms |
4688 KB |
Output is correct |
5 |
Correct |
1 ms |
4688 KB |
Output is correct |
6 |
Correct |
1 ms |
4688 KB |
Output is correct |
7 |
Correct |
1 ms |
4688 KB |
Output is correct |
8 |
Correct |
1 ms |
4688 KB |
Output is correct |
9 |
Correct |
1 ms |
4688 KB |
Output is correct |
10 |
Correct |
1 ms |
4688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4688 KB |
Output is correct |
2 |
Correct |
1 ms |
4688 KB |
Output is correct |
3 |
Correct |
1 ms |
4688 KB |
Output is correct |
4 |
Correct |
1 ms |
4688 KB |
Output is correct |
5 |
Correct |
1 ms |
4688 KB |
Output is correct |
6 |
Correct |
1 ms |
4688 KB |
Output is correct |
7 |
Correct |
1 ms |
4688 KB |
Output is correct |
8 |
Correct |
1 ms |
4688 KB |
Output is correct |
9 |
Correct |
1 ms |
4688 KB |
Output is correct |
10 |
Correct |
1 ms |
4688 KB |
Output is correct |
11 |
Correct |
7 ms |
4688 KB |
Output is correct |
12 |
Correct |
7 ms |
4576 KB |
Output is correct |
13 |
Correct |
5 ms |
4688 KB |
Output is correct |
14 |
Correct |
7 ms |
4764 KB |
Output is correct |
15 |
Correct |
6 ms |
4764 KB |
Output is correct |
16 |
Correct |
4 ms |
4688 KB |
Output is correct |
17 |
Correct |
7 ms |
4688 KB |
Output is correct |
18 |
Correct |
4 ms |
4688 KB |
Output is correct |
19 |
Correct |
4 ms |
4688 KB |
Output is correct |
20 |
Correct |
5 ms |
4688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4688 KB |
Output is correct |
2 |
Correct |
1 ms |
4688 KB |
Output is correct |
3 |
Correct |
1 ms |
4688 KB |
Output is correct |
4 |
Correct |
1 ms |
4688 KB |
Output is correct |
5 |
Correct |
1 ms |
4688 KB |
Output is correct |
6 |
Correct |
1 ms |
4688 KB |
Output is correct |
7 |
Correct |
1 ms |
4688 KB |
Output is correct |
8 |
Correct |
1 ms |
4688 KB |
Output is correct |
9 |
Correct |
1 ms |
4688 KB |
Output is correct |
10 |
Correct |
1 ms |
4688 KB |
Output is correct |
11 |
Correct |
7 ms |
4688 KB |
Output is correct |
12 |
Correct |
7 ms |
4576 KB |
Output is correct |
13 |
Correct |
5 ms |
4688 KB |
Output is correct |
14 |
Correct |
7 ms |
4764 KB |
Output is correct |
15 |
Correct |
6 ms |
4764 KB |
Output is correct |
16 |
Correct |
4 ms |
4688 KB |
Output is correct |
17 |
Correct |
7 ms |
4688 KB |
Output is correct |
18 |
Correct |
4 ms |
4688 KB |
Output is correct |
19 |
Correct |
4 ms |
4688 KB |
Output is correct |
20 |
Correct |
5 ms |
4688 KB |
Output is correct |
21 |
Incorrect |
2 ms |
4688 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |