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 <iostream>
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5,M=205;
int n,k,a[N],q[N],pre[N][M];
long long pref[N],f[N],g[N];
double slope(int i,int j) {
if(pref[i]==pref[j]) return -1e18;
return 1.0*((g[i]-pref[i]*pref[i])-(g[j]-pref[j]*pref[j]))/(pref[j]-pref[i]);
}
int main() {
cin>>n>>k;
for(int i=1;i<=n;++i){
cin>>a[i];
pref[i] = pref[i-1]+a[i];
}
for(int j=1;j<=k;++j) {
int l=1,r=1;
q[r]=0;
r++;
for(int i=1;i<=n;++i) {
while(l<r-1 && slope(q[l],q[l+1])<=pref[i]){
l++;
}
f[i]=g[q[l]]+pref[q[l]]*(pref[i]-pref[q[l]]);
pre[i][j]=q[l];
while(l<r-1 &&slope(q[r-2],q[r-1])>=slope(q[r-1],i)) --r;
q[r]=i;
r++;
}
for(int i=1;i<=n;i++){
g[i] = f[i];
}
}
cout<<f[n]<<endl;
int x=n;
for(int i=k;i>=1;--i){
x=pre[x][i];
cout<<x<<endl;
}
return 0;
}
# | 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... |