This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Be Name KHODA
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define pp pop_back
#define lb lower_bound
#define ub upper_bound
#define cl clear
#define bg begin
#define arr(x) array<ll , x>
#define endl '\n'
struct line{
ll ar , sh;
};
int n , k;
ll ps[100000] , dp[100000][2];
int opt[100000][201];
vector<int> cnv;
vector<arr(2)> pts;
line ls[100000];
inline ll form(int i , int j , int t){
return (ps[i] - ps[j]) * ps[j] + dp[j][(t - 1) & 1];
}
inline bool isDel(line a , line b , line c){
__int128_t d1 = (a.ar - b.ar) , d2 = (b.sh - a.sh);
__int128_t d3 = (b.ar - c.ar) , d4 = (c.sh - b.sh);
if(d2 < 0ll) d1 *= (-1ll) , d2 *= (-1ll);
if(d4 < 0ll) d3 *= (-1ll) , d4 *= (-1ll);
return ((d1 * d4) <= (d2 * d3));
}
inline ll getX(line a , line b){
ll d1 = a.ar - b.ar , d2 = b.sh - a.sh;
if(d2 < 0ll) d1 *= (-1ll) , d2 *= (-1ll);
return ((d1 + d2 - 1) / d2);
}
inline void addPt(arr(2) d){
if(pts.empty()){
pts.pb(d);
return;
}
int len = (int)pts.size();
while(len >= 1 and d[0] < pts[len - 1][0]) pts.pp() , len--;
pts.pb(d);
}
inline void addLine(int e){
if(cnv.empty()){
cnv.pb(e) , addPt({ps[e] , e});
return;
}
int len = (int)cnv.size();
while(len >= 2 and isDel(ls[e] , ls[cnv[len - 1]] , ls[cnv[len - 2]])){
cnv.pp() , len--;
}
if(ls[e].sh == ls[cnv[len - 1]].sh){
if(ls[e].ar >= ls[cnv[len - 1]].ar){
cnv.pp() , cnv.pb(e);
len--;
if(len == 0) addPt({ps[e] , e});
else{
arr(2) d = {max(getX(ls[e] , ls[cnv[len - 1]]) , ps[e]) , e};
addPt(d);
}
}
return;
}
// if(e == 1) cout << getX(ls[e] , ls[cnv[len - 1]]) << "**\n";
arr(2) d = {max(getX(ls[e] , ls[cnv[len - 1]]) , ps[e]) , e};
addPt(d) , cnv.pb(e);
}
inline void f(){
for(int i = 0 ; i < n ; i++){
opt[i][0] = -1;
ls[i].ar = dp[i][0] - (ps[i] * ps[i]);
ls[i].sh = ps[i];
addLine(i);
}
for(int t = 1 ; t <= k ; t++){
int it = -1;
for(int i = 0 ; i < n ; i++){
while(it < (int)pts.size() - 1 and ps[i] >= pts[it + 1][0] and i > pts[it + 1][1]) it++;
if(it == -1){
dp[i][t & 1] = -1 , opt[i][t] = -1;
continue;
}
int j = pts[it][1];
if(j == i){
cout << i << " " << t << "%\n";
exit(0);
}
// if(t == 1 and i == 2) cout << pts[0][1] << "^^\n";
dp[i][t & 1] = form(i , j , t) , opt[i][t] = j;
}
cnv.cl() , pts.cl();
for(int i = 0 ; i < n ; i++){
if(dp[i][t & 1] == -1) continue;
ls[i].ar = dp[i][t & 1] - (ps[i] * ps[i]);
ls[i].sh = ps[i];
addLine(i);
}
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for(int i = 0 ; i < n ; i++) cin >> ps[i];
for(int i = 1 ; i < n ; i++) ps[i] += ps[i - 1];
f();
cout << dp[n - 1][k & 1] << endl;
vector<int> o;
int v = n - 1 , i = k;
while(opt[v][i] != -1) o.pb(opt[v][i]) , v = opt[v][i] , i--;
reverse(o.bg() , o.end());
for(int el : o) cout << el + 1 << " ";
}
# | 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... |