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>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const ll inf = 1e10;
const int maxn = 1100;
ll arr[maxn], pref[maxn];
ll dp[maxn][210], n, k;
ll parent[maxn][210];
ll getsum(int l, int r) {
if(r < l) return 0;
return pref[r] - pref[l-1];
}
int main() {
cin>>n>>k;
for(int i=1;i<=n;i++) {
cin>>arr[i];
pref[i] = pref[i-1] + arr[i];
}
for(int i=1;i<n;i++) {
dp[i][1] = pref[i] * getsum(i+1, n);
}
ll result = -inf;
int resultind = -1;
for(int d=2;d<=k;d++) {
for(int i=d;i<=n;i++) {
dp[i][d] = -inf;
for(int j=d-1;j<i;j++) {
if(dp[j][d-1] + getsum(j+1, i) * getsum(i+1, n) >= dp[i][d]) {
dp[i][d] = dp[j][d-1] + getsum(j+1, i) * getsum(i+1, n);
parent[i][d] = j;
}
}
if(dp[i][d] >= result) {
result = dp[i][d];
resultind = i;
}
}
}
vector<int>v;
cout<<result<<"\n";
for(int i=k;i>=1;i--) {
v.pb(resultind);
resultind = parent[resultind][i];
}
reverse(v.begin(), v.end());
for(int i:v)
cout<<i<<" ";
cout<<"\n";
}
# | 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... |