//gm --- akezhon
#include <bits/stdc++.h>
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define pb push_back
#define pf push_front
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define pii pair<int,int>
#define tm (tl+tr)/2
#define TL v+v, tl, tm
#define TR v+v+1, tm+1, tr
#define DA l <= tl && tr <= r
#define NE r < tl || tr < l
#define double long double
#define int long long
using namespace std;
const int N=5e5+7;
const int mod=998244353;
const int inf=2e9;
const int eps=1e-18;
struct line{
int b, k;
double l, r;
int i;
};
vector<line>v;
double xint(line a, line b){
if(a.k==b.k)return inf;
else return (b.b-a.b)/(a.k-b.k);
}
void add(int b, int k, int i){
line x = {b, k, -inf, inf, i};
while(v.size() && (eps < v.back().l-xint(v.back(), x) || (v.back().k==x.k && x.b <= v.back().b)))v.pop_back();
// while(1 < v.size() && xint(v.back(), v[v.size()-2]) > xint(v.back(), x))v.pop_back();
// v.pb(x);
// return;
if(v.empty()){
v.pb(x);
}
else{
double inter = xint(v.back(), x);
v.back().r = inter;
v.pb(x);
v.back().l = inter;
}
}
line get(int x){
int l=0, r=v.size()-1, ans=0;
while(l <= r){
int m = (l+r)/2;
// int val = (m == 0 ? -inf : xint(v[m], v[m - 1]));
// if(val < x)l=m+1, ans=m;
if(v[m].l <= x+eps)l=m+1, ans=m;
else r=m-1;
}
return v[ans];
}
int n, k;
int a[N];
int p[N];
int dp[205][100005];
int pe[205][100005];
void AlemAmenov(){
cin >> n >> k;
for(int i=1; i <= n; i++){
cin >> a[i];
p[i]=p[i-1]+a[i];
}
int mx=0, pos=0;
for(int kk=1; kk <= k; kk++){
v.clear();
for(int i=1; i < n; i++){
add(dp[kk-1][i-1] - p[n]*p[i-1], p[i-1], i-1);
line x = get(p[i]);
dp[kk][i] = p[n]*p[i]-p[i]*p[i] + x.b + p[i]*x.k;
pe[kk][i] = x.i;
}
}
for(int i=1; i < n; i++){
if(mx <= dp[k][i]){
mx = dp[k][i];
pos = i;
}
}
cout << mx << '\n';
while(k){
cout << pos << ' ';
pos = pe[k--][pos];
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int RealName=1;
// cin >> RealName;
// srand(time(0));
while(RealName--)
AlemAmenov();
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... |