#include <iostream>
#include <algorithm>
#include <cstring>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ll long long
#define inf 1e18
using namespace std;
struct dt{
ll t, c;
bool operator < (const dt &x) const{
return (t > x.t);
}
};
const int N = 3e5 + 9;
int n, k, t;
ll dp[2009][2009], a[N], pre[N];
void solve(){
int p;
ll s = 0;
FOR(i, 1, n) {
s += a[i];
if (a[i] < 0) p = i;
}
if (k == 1) cout << max(max(pre[p - 1], pre[n] - pre[p]), s);
if (k == 2) cout << s - a[p];
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen("nhap.inp", "r", stdin);
// freopen("xuat.out", "w", stdout);
cin >> n >> k;
FOR(i, 1, n) {
cin >> a[i];
t += (a[i] < 0);
pre[i] = pre[i-1] + a[i];
}
if (t == 1) {
solve();
return 0;
}
FOR(i, 1, k) {
ll cur = 0;
FOR(j, 1, n) {
cur = max(cur, dp[i-1][j] - pre[j]);
dp[i][j] = max(dp[i][j-1], pre[j] + cur);
}
}
cout <<dp[k][n];
return 0;
}
Compilation message
feast.cpp: In function 'void solve()':
feast.cpp:30:32: warning: 'p' may be used uninitialized in this function [-Wmaybe-uninitialized]
30 | if (k == 2) cout << s - a[p];
| ~~~^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1055 ms |
30488 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
5972 KB |
Output is correct |
2 |
Correct |
16 ms |
6100 KB |
Output is correct |
3 |
Correct |
17 ms |
5952 KB |
Output is correct |
4 |
Incorrect |
18 ms |
5956 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
27 ms |
10280 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
328 KB |
Output is correct |
21 |
Correct |
2 ms |
3412 KB |
Output is correct |
22 |
Correct |
13 ms |
26836 KB |
Output is correct |
23 |
Correct |
3 ms |
6356 KB |
Output is correct |
24 |
Correct |
2 ms |
3668 KB |
Output is correct |
25 |
Correct |
3 ms |
4944 KB |
Output is correct |
26 |
Correct |
2 ms |
3028 KB |
Output is correct |
27 |
Correct |
4 ms |
4864 KB |
Output is correct |
28 |
Correct |
1 ms |
852 KB |
Output is correct |
29 |
Correct |
1 ms |
852 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1055 ms |
30488 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |