#include<bits/stdc++.h>
using namespace std;
const int N = 300005;
int n, k;
int a[N];
long long dp[2003][2003][2];
void sub3()
{
long long ans = 0, mx = 0, mn = 0, s = 0;
for(int i = 1; i <= n; i++)
{
s += a[i];
mx = max(mx, s);
mn = min(mn, s);
ans = max(ans, mx - mn);
}
cout << ans;
}
void sub6()
{
long long ans=0;
dp[0][0][1] = 0;
dp[0][0][0] = 0;
for(int i = 1; i <= n; i++)
{
dp[i][0][0] = 0;
for(int j = 1; j <= k; j++)
{
dp[i][j][0] = max(dp[i - 1][j][1], dp[i - 1][j][0]);
dp[i][j][1] = max(dp[i - 1][j][1], dp[i - 1][j - 1][0]) + a[i];
ans = max({ans, dp[i][j][0], dp[i][j][1]});
}
}
cout << ans;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> k;
int cntam = 0;
long long s = 0;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
cntam += (a[i] < 0);
}
if(k == 1)
{
sub3();
return 0;
}
if(cntam <= 1)
{
long long ans = 0;
for(int i = 1; i <= n; i++) if(a[i] > 0) ans += a[i];
cout << ans;
return 0;
}
sub6();
return 0;
}
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:47:15: warning: unused variable 's' [-Wunused-variable]
47 | long long s = 0;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
2652 KB |
Output is correct |
2 |
Correct |
22 ms |
2512 KB |
Output is correct |
3 |
Correct |
22 ms |
2648 KB |
Output is correct |
4 |
Correct |
23 ms |
2648 KB |
Output is correct |
5 |
Correct |
23 ms |
2652 KB |
Output is correct |
6 |
Correct |
21 ms |
2644 KB |
Output is correct |
7 |
Correct |
24 ms |
2516 KB |
Output is correct |
8 |
Correct |
26 ms |
2512 KB |
Output is correct |
9 |
Correct |
21 ms |
2648 KB |
Output is correct |
10 |
Correct |
22 ms |
2512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
2648 KB |
Output is correct |
2 |
Correct |
15 ms |
3672 KB |
Output is correct |
3 |
Correct |
15 ms |
3760 KB |
Output is correct |
4 |
Correct |
15 ms |
3672 KB |
Output is correct |
5 |
Correct |
28 ms |
5436 KB |
Output is correct |
6 |
Correct |
16 ms |
3712 KB |
Output is correct |
7 |
Correct |
14 ms |
3672 KB |
Output is correct |
8 |
Correct |
23 ms |
5436 KB |
Output is correct |
9 |
Correct |
23 ms |
5604 KB |
Output is correct |
10 |
Correct |
15 ms |
3780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
26 ms |
2504 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
2652 KB |
Output is correct |
2 |
Correct |
22 ms |
2512 KB |
Output is correct |
3 |
Correct |
22 ms |
2648 KB |
Output is correct |
4 |
Correct |
23 ms |
2648 KB |
Output is correct |
5 |
Correct |
23 ms |
2652 KB |
Output is correct |
6 |
Correct |
21 ms |
2644 KB |
Output is correct |
7 |
Correct |
24 ms |
2516 KB |
Output is correct |
8 |
Correct |
26 ms |
2512 KB |
Output is correct |
9 |
Correct |
21 ms |
2648 KB |
Output is correct |
10 |
Correct |
22 ms |
2512 KB |
Output is correct |
11 |
Correct |
14 ms |
2648 KB |
Output is correct |
12 |
Correct |
15 ms |
3672 KB |
Output is correct |
13 |
Correct |
15 ms |
3760 KB |
Output is correct |
14 |
Correct |
15 ms |
3672 KB |
Output is correct |
15 |
Correct |
28 ms |
5436 KB |
Output is correct |
16 |
Correct |
16 ms |
3712 KB |
Output is correct |
17 |
Correct |
14 ms |
3672 KB |
Output is correct |
18 |
Correct |
23 ms |
5436 KB |
Output is correct |
19 |
Correct |
23 ms |
5604 KB |
Output is correct |
20 |
Correct |
15 ms |
3780 KB |
Output is correct |
21 |
Incorrect |
26 ms |
2504 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |