This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
feast.cpp: In function 'int main()':
feast.cpp:47:15: warning: unused variable 's' [-Wunused-variable]
47 | long long s = 0;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |