이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |