This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include <bits/stdc++.h>
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define eb emplace_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {cerr<<#a<<": ";for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define cer(a) {cerr<<#a<<": ";for(ll x_ : a) cerr<<x_<< " ";cerr<<endl;}
#define si(a) (ll)(a.size())
using namespace std;
using ld = double;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pld = pair<ld,ld>;
#define mod 1
ll add(ll x,ll y){
x+=y;
if(x<0){
x%=mod;
x+=mod;
}else{
if(x>=mod) x%=mod;
}
return x;
}
ll mul(ll a,ll b){
a = add(a,0);
b = add(b,0);
ll ans = (a*b)%mod;
if(ans<0) ans+=mod;
return ans;
}
const ll maxn = 100005;
const ll maxk = 205;
const ll maxx = 1000005;
const ll mx = 1000000000;
ll n,k;
ll a[maxn];
ll ps[maxn];
ll dp[maxk][maxn];
int opt[maxk][maxn];
struct lin {
ll n,k;
lin(){n = -llinf,k = 0;}
lin(ll n_,ll k_){n = n_,k = k_;}
ll operator()(ll x){return k*x+n;}
};
pair<ll,lin> t[maxx];
ll ls[maxx],rs[maxx],tsz = 0,root = 0;
void upd(ll &v,ll tl,ll tr,pair<ll,lin> p) {
if(!v) v = ++tsz;
if(t[v].sc.n==-llinf){t[v] = p;return;}
ll mid = tl+(tr-tl)/2;
if(t[v].sc(mid)<p.sc(mid)) {
swap(t[v],p);
}
lin g = t[v].sc;
if(g(tl)>=p.sc(tl)) {
upd(rs[v],mid+1,tr,p);
}else {
upd(ls[v],tl,mid,p);
}
}
pair<ll,ll> get(ll &v,ll tl,ll tr,ll i) {
if(!v) return {-llinf,0};
cerr<<t[v].sc(i)<<endl;
if(tl==tr){return {t[v].sc(i),t[v].fi};}
ll mid = tl+(tr-tl)/2;
pll val = {t[v].sc(i),t[v].fi};
if(i<=mid) return max(val,get(ls[v],tl,mid,i));
return max(val,get(rs[v],mid+1,tr,i));
}
void tc(){
pair<ll,lin> pp;
pp.fi = 0;
pp.sc = lin();
for(ll i = 0;i<maxx;i++) t[i] = pp;
cin >> n >> k;
for(ll i = 1;i<=n;i++) cin >> a[i];
for(ll i = 1;i<=n;i++) ps[i] = ps[i-1] + a[i];
for(ll j = 1;j<=k;j++) {
for(ll i = 1;i<=n;i++){
lin f(-ps[i]*ps[i]+dp[j-1][i],ps[i]);
upd(root,-mx,mx,{i,f});
}
for(ll i = 1;i<=n;i++) {
auto it = get(root,-mx,mx,ps[i]);
dp[j][i] = it.fi;
opt[j][i] = it.sc;
}
for(ll i = 0;i<=tsz;i++) {
t[i] = pp;
ls[i] = rs[i] = 0;
}
root = tsz = 0;
}
cout<<dp[k][n]<<endl;
vector<ll> v;
for(ll i = k;i>=1;i--) {
n = opt[i][n];
v.pb(n);
}
reverse(all(v));
for(ll x : v) cout<<x<< " ";
cout<<endl;
}
int main(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
int t; t = 1;
while(t--){
tc();
}
return (0-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... |