This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
deque<pair<pair<long long,long long>,long long>> v1;
double in(long double m1,long double c1,long double m2,long double c2){
if(m1==m2)return (c1>=c2);
return (c2-c1)/(m1-m2);
}
void insert_line(long long m,long long c,long long idx){
while(v1.size()>1&&in(v1[v1.size()-2].first.first,v1[v1.size()-2].first.second,v1[v1.size()-1].first.first,v1[v1.size()-1].first.second)>=in(v1[v1.size()-2].first.first,v1[v1.size()-2].first.second,m,c)){
v1.pop_back();
}
v1.push_back({{m,c},idx});
}
pair<long long,long long> eval(long long x){
while(v1.size()>1&&in(v1[0].first.first,v1[0].first.second,v1[1].first.first,v1[1].first.second)<x)v1.pop_front();
return {v1[0].first.first*x+v1[0].first.second,v1[0].second};
}
long long dp[100001][2];
int lol[100001][204];
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
long long n,k;
cin>>n>>k;
long long arr[n+1] = {0};
for(int i = 1;i<=n;i++){
cin>>arr[i];
arr[i]+=arr[i-1];
}
if(n==1){
cout<<0<<endl;
return 0;
}
for(long long j = 1;j<=k+1;j++){
v1.clear();
insert_line(arr[j-1],(dp[j-1][!(j&1)]-arr[j-1]*arr[n]),j-1);
for(int i = j;i<=n;i++){
dp[i][j&1] = arr[i]*arr[n]-arr[i]*arr[i]+eval(arr[i]).first;
lol[i][j] = eval(arr[i]).second;
insert_line(arr[i],(dp[i][!(j&1)]-arr[i]*arr[n]),i);
}
}
k++;
cout<<dp[n][k&1]<<endl;
int x = n;
vector<long long> ha;
while(k>1){
ha.push_back(max(1,lol[x][k]));
x = lol[x][k];k--;
}
for(int i = ha.size()-1;i>=0;i--){
cout<<ha[i]<<" ";
}
}
# | 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... |