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 int long long
#define MOD 1000000007
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
int32_t main(){
int n,k;
cin>>n>>k;
vector<vector<int>>dp(n+1,vector<int>(k+1,-1e15)),pre2(n+1,vector<int>(k+1));
vector<int>arr(n+1),pre(n+1);
pre[0]=0;
for(int i=1;i<=n;i++){
cin>>arr[i];
pre[i]=pre[i-1]+arr[i];
}
dp[0][0]=0;
for(int j=1;j<=k;j++){
for(int i=j;i<=n-1;i++){
for(int k=i-1;k>=j-1;k--){
dp[i][j]=max(dp[i][j],dp[k][j-1]+pre[k]*(pre[i]-pre[k]));
if(dp[i][j]==dp[k][j-1]+pre[k]*(pre[i]-pre[k])){
pre2[i][j]=k;
}
}
}
}
int mx=-1e15,ind;
for(int i=k;i<=n-1;i++){
mx=max(mx,dp[i][k]+pre[i]*(pre[n]-pre[i]));
if(mx==dp[i][k]+pre[i]*(pre[n]-pre[i])){
ind=i;
}
}
vector<int>ans;
while(ind!=0){
ans.pb(ind);
ind=pre2[ind][k--];
}
cout<<mx<<endl;
while(ans.size()){
cout<<ans.back()<<" ";
ans.pop_back();
}
cout<<endl;
}
# | 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... |