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;
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fs first
#define sc second
#define pb push_back
const int maxn=1e5,maxk=201;
ll dp[maxn+1][maxk+1],a[maxn+1],pre[maxn+1];
int bt[maxn+1][maxk+1],c;
void dnc(int l,int r,int opl,int opr){
if(l>r)return;
int m=(l+r)/2;
pll best={-1e18,0};
for(int i=opl;i<=opr&&i<m;i++){
best=max(best,{dp[i][c-1]+pre[i]*(pre[m]-pre[i]),i});
}
dp[m][c]=best.fs;
bt[m][c]=best.sc;
dnc(l,m-1,opl,best.sc);
dnc(m+1,r,best.sc,opr);
}
void solve() {
int n,k;
cin>>n>>k;
k++;
pre[0]=0;
for(int i=1;i<=n;i++){
cin>>a[i];
pre[i]=pre[i-1]+a[i];
}
for(int i=0;i<=n;i++){
for(int j=i+1;j<=k;j++)dp[i][j]=-1e18;
if(i)dp[i][0]=-1e18;
}
for(c=1;c<=k;c++){
dnc(1,n,0,n);
}
cout<<dp[n][k]<<'\n';
int x=n;
vector<int> v;
for(int i=k;i>1;i--){
x=bt[x][i];
v.pb(x);
}
reverse(v.begin(),v.end());
for(int i:v)cout<<i<<' ';
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
solve();
}
# | 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... |