#include <cstdio>
#include <numeric>
#include <cstring>
#include <cassert>
#include <string>
#include <deque>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
using ll=long long;
#define N 300005
#define ALL(x) x.begin(), x.end()
int n, k, a[N];
long long p[N];
int main()
{
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i], p[i] = p[i-1] + a[i];
if (count_if(a+1, a+1+n,[](int x){ return x<0; }) == 0)
return cout << accumulate(a+1,a+1+n, 0ll), 0;
if (count_if(a+1, a+1+n,[](int x){ return x<0; }) == 1)
{
int x;
for (int i = 1; i <= n; ++i) if (a[i] < 0) x = i;
if (k == 1) return cout << max({p[x-1], p[n] - p[x], p[n]}), 0;
else return cout << p[n] - a[x], 0;
}
if (k == 1)
{
long long dp[N]{0};
memset(dp, -0xbf, sizeof dp);
dp[0] = 0;
for (int i = 1; i <= n; ++i) dp[i] = max(1ll*a[i], dp[i-1] + a[i]);
return cout <<*max_element(dp, dp+N), 0;
}
if (n <= 300)
{
long long dp[305][305]{0};
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= k; ++j)
{
dp[i][j] = max({dp[i-1][j], dp[i][j], dp[i][j-1]});
for (int l = 0; l < i; ++l)
{
dp[i][j] = max(dp[i][j], dp[l][j-1] + p[i] - p[l]);
}
} }
cout << dp[n][k];
}
return 0;
}
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:35:39: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
35 | else return cout << p[n] - a[x], 0;
| ~~~^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
5980 KB |
Output is correct |
2 |
Correct |
28 ms |
6228 KB |
Output is correct |
3 |
Correct |
24 ms |
6232 KB |
Output is correct |
4 |
Correct |
24 ms |
6228 KB |
Output is correct |
5 |
Correct |
24 ms |
6228 KB |
Output is correct |
6 |
Correct |
22 ms |
6036 KB |
Output is correct |
7 |
Correct |
23 ms |
5980 KB |
Output is correct |
8 |
Correct |
23 ms |
6252 KB |
Output is correct |
9 |
Correct |
25 ms |
5980 KB |
Output is correct |
10 |
Correct |
24 ms |
6260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
6236 KB |
Output is correct |
2 |
Correct |
18 ms |
6232 KB |
Output is correct |
3 |
Correct |
16 ms |
6232 KB |
Output is correct |
4 |
Correct |
16 ms |
6236 KB |
Output is correct |
5 |
Correct |
24 ms |
8956 KB |
Output is correct |
6 |
Correct |
16 ms |
7260 KB |
Output is correct |
7 |
Correct |
17 ms |
7260 KB |
Output is correct |
8 |
Correct |
25 ms |
9052 KB |
Output is correct |
9 |
Correct |
24 ms |
8824 KB |
Output is correct |
10 |
Correct |
17 ms |
7260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
28 ms |
6236 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
5980 KB |
Output is correct |
2 |
Correct |
28 ms |
6228 KB |
Output is correct |
3 |
Correct |
24 ms |
6232 KB |
Output is correct |
4 |
Correct |
24 ms |
6228 KB |
Output is correct |
5 |
Correct |
24 ms |
6228 KB |
Output is correct |
6 |
Correct |
22 ms |
6036 KB |
Output is correct |
7 |
Correct |
23 ms |
5980 KB |
Output is correct |
8 |
Correct |
23 ms |
6252 KB |
Output is correct |
9 |
Correct |
25 ms |
5980 KB |
Output is correct |
10 |
Correct |
24 ms |
6260 KB |
Output is correct |
11 |
Correct |
16 ms |
6236 KB |
Output is correct |
12 |
Correct |
18 ms |
6232 KB |
Output is correct |
13 |
Correct |
16 ms |
6232 KB |
Output is correct |
14 |
Correct |
16 ms |
6236 KB |
Output is correct |
15 |
Correct |
24 ms |
8956 KB |
Output is correct |
16 |
Correct |
16 ms |
7260 KB |
Output is correct |
17 |
Correct |
17 ms |
7260 KB |
Output is correct |
18 |
Correct |
25 ms |
9052 KB |
Output is correct |
19 |
Correct |
24 ms |
8824 KB |
Output is correct |
20 |
Correct |
17 ms |
7260 KB |
Output is correct |
21 |
Incorrect |
28 ms |
6236 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |