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 pair<ll,ll>pii;
typedef pair<pii,pii>piii;
const int mxn = 1e4+5;
#define inf 1e16
#define all(v) v.begin(), v.end()
#define FAST ios_base::sync_with_stdio(0);cout.tie(0)
ll arr[mxn];
ll dp[mxn][205];
ll pre[mxn];
int dir[mxn][205];
int n, k;
ll solve(int pos, int x)
{
if(x==k) return 0;
if(pos>n) {
return -inf;
}
if(dp[pos][x]!=-1) return dp[pos][x];
ll sum = 0, ret = 0;
for(int i = pos; i <= n; i++) {
sum+=arr[i];
ll tmp = max(ret, solve(i+1, x+1)+sum*pre[i+1]);
if(tmp>ret) {
ret = tmp;
dir[pos][x] = i+1;
}
}
return dp[pos][x] = ret;
}
void pathprint(int pos, int x)
{
if(x==k) return;
cout << dir[pos][x]-1 << ' ';
pathprint(dir[pos][x], x+1);
}
int main()
{
FAST;
cin >> n >> k;
for(int i = 1; i <= n; i++) {
cin >> arr[i];
}
pre[n] = arr[n];
for(int i = n-1; i >= 1; i--) {
pre[i] = pre[i+1]+arr[i];
}
memset(dp, -1, sizeof dp);
ll ans = solve(1, 0);
cout << ans << '\n';
pathprint(1, 0);
return 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... |