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][K];
int par[N][K];
long long pre[N];
struct CHT{
vector<pair<pair<int,int>,pair<int,long long>>> lines;
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);
}
void add(int a,long long b,int c){
if(lines.size() && lines.back().second.first == a && lines.back().second.second >= b)
return;
while(lines.size() && get(lines.back().second,{a,b}) <= lines.back().first.first){
lines.pop_back();
}
if(lines.empty()){
lines.push_back({{0,c},{a,b}});
}
else lines.push_back({{get(lines.back().second,{a,b}),c},{a,b}});
}
pair<int, long long> get(int x){
pair<pair<int,int>,pair<int,long long>> tmp = {{x+1,0},{0,0}};
int pos = lower_bound(lines.begin(),lines.end(),tmp) - lines.begin() - 1;
return {lines[pos].first.second,1ll * lines[pos].second.first * x + lines[pos].second.second};
}
}maxi[K];
void solve(){
int n,k;
cin >> n >> k;
k++;
for(int i = 1;i<=n;i++){
cin >> pre[i];
pre[i] += pre[i-1];
}
maxi[0].add(0,0,0);
for(int i=1;i<=k;i++){
maxi[i].add(0,-1e9,-1e18);
}
for(int i=1;i<=n;i++){
for(int j = k;j>=1;j--){
auto x = maxi[j-1].get(pre[i]);
par[i][j] = x.first;
dp[i][j] = x.second - pre[i] * pre[i] + pre[i] * pre[n];
maxi[j].add(2*pre[i], dp[i][j] - pre[i] * pre[n] - pre[i] * pre[i],i);
}
}
cout << dp[n][k] / 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
}
Compilation message (stderr)
sequence.cpp: In function 'void solve()':
sequence.cpp:42:28: warning: overflow in conversion from 'double' to 'int' changes value from '-1.0e+18' to '-2147483648' [-Woverflow]
42 | maxi[i].add(0,-1e9,-1e18);
| ^~~~~
# | 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... |