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 N 100005
#define K 205
using namespace std;
long long dp[N];
long long ndp[N];
int par[N][K];
long long pre[N];
int n;
struct CHT{
vector<pair<int,int>> lines;
void clear(){
lines.clear();
}
long long get(pair<int,long long> a,pair<int,long long> b){
if(a.first == b.first)
return 0;
return (b.second - a.second + (a.first - b.first -1)) / (a.first - b.first);
}
long long val(int c){
return dp[c] - pre[c] * pre[n] - pre[c] * pre[c];
}
void add(int c){
int a = 2*pre[c];
long long b = val(c);
if(lines.size() && 2*pre[lines.back().second] == a && val(lines.back().second) >= b)
return;
while(lines.size() && get({2*pre[lines.back().second],val(lines.back().second)},{a,b}) <= lines.back().first){
lines.pop_back();
}
if(lines.empty()){
lines.push_back({0,c});
}
else lines.push_back({get({2*pre[lines.back().second],val(lines.back().second)},{a,b}),c});
}
pair<int, long long> get(int x){
pair<int,int> tmp = {x+1,0};
int pos = lower_bound(lines.begin(),lines.end(),tmp) - lines.begin() - 1;
return {lines[pos].second,1ll * 2*pre[lines[pos].second] * x + val(lines[pos].second)};
}
}maxi;
void solve(){
int k;
cin >> n >> k;
k++;
for(int i = 1;i<=n;i++){
cin >> pre[i];
pre[i] += pre[i-1];
}
for(int j = 1;j<=k;j++){
maxi.add(j-1);
for(int i=j;i<=n;i++){
auto x = maxi.get(pre[i]);
par[i][j] = x.first;
ndp[i] = x.second - pre[i] * pre[i] + pre[i] * pre[n];
maxi.add(i);
}
for(int i=1;i<=n;i++){
dp[i] = ndp[i];
}
maxi.clear();
}
cout << dp[n] / 2 << endl;
while(par[n][k]){
cout << par[n][k];
n = par[n][k];
k--;
if(k)cout << " ";
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t=1;
//cin>>t;
while(t--){
solve();
}
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
# | 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... |