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
using namespace std;
const int N = (int)100007;
int n, k, arr[N], pr[N], par[N][207];
int dp1[N][3];
int calc(int l,int r){
return pr[l] * (pr[r] - pr[l]);
}
void solve(int k,int l,int r,int optl,int optr){
if(l > r){
return;
}
int mid = (l + r) >> 1;
int opt = optl;
dp1[mid][k % 2] = 0;
for(int i = optl;i <= min(mid,optr); i++){
int cur = dp1[i][((k - 1) % 2)] + calc(i,mid);
if(cur >= dp1[mid][k % 2]){
dp1[mid][k % 2] = cur;
opt = i;
}
}
par[mid][k] = opt;
solve(k,l,mid - 1,optl,opt);
solve(k,mid + 1,r,opt,optr);
}
main(){
cin >> n >> k;
for(int i = 1;i <= n; i++){
cin >> arr[i];
pr[i] = pr[i - 1] + arr[i];
}
for(int i = 1;i <= k; i++){
solve(i,1,n,1,n);
}
cout << dp1[n][k % 2] << '\n';
vector<int > ans;
while(par[n][k]){
ans.push_back(par[n][k]);
n = par[n][k];
k--;
}
for(int i = ans.size() - 1; i >= 0; i--){
cout << ans[i] << " ";
}
}
/*
7 3
4 1 3 4 0 2 3
*/
Compilation message (stderr)
sequence.cpp:28:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){
^
# | 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... |