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;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long double ld;
typedef pair<ll, ll> pll;
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define ROF(i, a, b) for(int i = a; i >= b; i--)
#define ms memset
#define pb push_back
#define fi first
#define se second
const ll INF = 1e18;
void solve(){
int n,k; cin>>n>>k;
vi a(n+1); FOR(i,1,n+1) cin>>a[i];
vector<ll> s(n+1); FOR(i,1,n+1) s[i]=s[i-1]+a[i];
ll dp[n+1][k+1]; ms(dp,0,sizeof(dp));
ll f[n+1][k+1]; ms(f,0,sizeof(f));
FOR(i,1,n+1){
f[i][0]=-s[i];
f[i][0]=max(f[i][0],f[i-1][0]);
FOR(j,1,k+1){
dp[i][j]=max(dp[i][j],dp[i][j-1]);
dp[i][j]=max(dp[i][j],dp[i-1][j]);
dp[i][j]=max(dp[i][j],s[i]+f[i-1][j-1]);
f[i][j]=dp[i][j]-s[i];
if (i>1) f[i][j]=max(f[i][j],f[i-1][j]);
//cout<<dp[i][j]<<" ";
//cout<<f[i][j]<<" ";
}
//cout<<"\n";
}
cout<<dp[n][k]<<"\n";
}
int main(){
//MOD=MOD1;
ios::sync_with_stdio(false);
if (fopen("input.txt", "r")) {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
int TC = 1;
//cin >> TC;
FOR(i, 1, TC+1){
//cout << "Case #" << i << ": ";
solve();
}
return 0;
}
Compilation message (stderr)
feast.cpp: In function 'int main()':
feast.cpp:45:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:46:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |