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>
#pragma GCC target( "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")
using namespace std;
#define int long long
#define y1 cheza
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N=2e5+100;
const int B=700;
const int M=1e7+10;
const int mod=1e9+7;
const int INF=1e18;
const long double eps=1e-8;
const int dx[]={1,-1,0,0};
const int dy[]={0,0,-1,1};
const int debug=0;
int n,k;
int a[N];
int dp[N][220];
int pr[N][220];
int pref[N];
struct point{
int k,b,id;
};
int calc(point x,point y){
if(y.k==x.k){
return -x.b;
}
return (x.b-y.b)/(y.k-x.k);
}
int prec(point x,int y){
return x.k*y+x.b;
}
vector<point>v;
pair<int,int> get(int x){
int l=0;
int r=v.size()-2;
int ans=v.size()-1;
while(l<=r){
int mid=(l+r)>>1ll;
if(prec(v[mid],x)>prec(v[mid+1],x)){
ans=mid;
r=mid-1;
}
else{
l=mid+1;
}
}
pair<int,int>res;
res.first=prec(v[ans],x);
res.second=v[ans].id;
return res;
}
void add(point x){
while(v.size()>1&&calc(x,v.back())>calc(x,v[v.size()-2])){
v.pop_back();
}
v.push_back(x);
}
void test(){
cin>>n>>k;
add({0,0,0});
for(int i=1;i<=n;i++){
cin>>a[i];
pref[i]=pref[i-1]+a[i];
add({-pref[i],0,i});
}
k++;
for(int kk=1;kk<=k;kk++){
// cout<<kk<<" <<<<<<<\n";
for(int i=1;i<=n;i++){
// for(int j=0;j<i;j++){
// int y=dp[j][kk-1]+(pref[i]-pref[j])*(pref[n]-pref[i]);
// if(y>=dp[i][kk]){
// dp[i][kk]=y;
// pr[i][kk]=j;
// }
// }
dp[i][kk]=pref[i]*pref[n]-pref[i]*pref[i];
auto res=get(pref[n]-pref[i]);
dp[i][kk]+=res.first;
pr[i][kk]=res.second;
// cout<<"y="<<-pref[i]<<"*x"<<'+'<<dp[i][kk-1]<<'\n';
}
v.clear();
for(int i=1;i<=n;i++){
add({-pref[i],dp[i][kk],i});
}
// cout<<'\n';
}
// A B C
// k1=AC+BC
// k1=AB+BC
// k1=AB+AC
// k2=AC+BC+AB
// k2=AB+AC+BC
// for(int i=1;i<=k;i++){
// for(int j=1;j<=n;j++){
// cout<<dp[j][i]<<' ';
// }
// cout<<'\n';
// }
// for(int i=1;i<=k;i++){
// for(int j=1;j<=n;j++){
// cout<<pr[j][i]<<' ';
// }
// cout<<'\n';
// }
// return;
int mx=dp[n][k];
int cur=pr[n][k];
vector<int>ans;
ans.push_back(cur);
k--;
while(k!=1){
cur=pr[cur][k];
k--;
ans.push_back(cur);
}
cout<<mx<<'\n';
// reverse(ans.begin(),ans.end());
sort(ans.begin(),ans.end());
for(auto i:ans){
cout<<i<<' ';
}
cout<<'\n';
}
/*
int y=dp[j][kk-1]+(pref[i]-pref[j])*(pref[n]-pref[i]);
i ,j
dp[i][kk]=min dp[j][kk-1] + (pref[i]-pref[j])*(pref[n]-pref[i]);
dp[i][kk]=pref[i]*pref[n]-pref[i]*pref[i];
-pref[j]*(pref[n]-pref[i])+dp[j][kk-1];
*/
signed main(){
// cout<<"HEllo\n";
// freopen("INPUT.TXT","r",stdin);
// freopen("OUTPUT.TXT","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// cout.tie(nullptr);
long long t2=1;
// cin>>t2;
// scanf("%lld",&t2);
for(int i=1;i<=t2;i++){
// cout<<"Case "<<i<<": ";
test();
}
return 0;
}
/*
temmie luck
*/
# | 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... |