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,avx2")
#pragma GCC optimize("unroll-loops,O2")
#define rep(i,l,r) for (int i = l; i < r; i++)
#define repr(i,r,l) for (int i = r; i >= l; i--)
#define X first
#define Y second
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << " : " << x << endl;
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
constexpr int N = 1e5+10,mod = 1e9+7,maxm = 210;
constexpr ll inf = 1e18+10;
inline int mkay(int a,int b){
if (a+b >= mod) return a+b-mod;
// if (a+b < 0) return a+b+mod;
return a+b;
}
inline int poww(int a,int k){
if (k < 0) return 0;
int z = 1;
while (k){
if (k&1) z = 1ll*z*a%mod;
a = 1ll*a*a%mod;
k >>= 1;
}
return z;
}
ll dp[2][N],ps[N];
int opt[maxm][N],a[N];
vector<int> ve;
vector<pll> baz;
ll bar(int i,int j,int k){
return (dp[k][i]-ps[i]*ps[i]-dp[k][j]+ps[j]*ps[j]+ps[j]-ps[i]-1)/(ps[j]-ps[i]);
}
int main(){
ios :: sync_with_stdio(0); cin.tie(0);
int n,k;
cin >> n >> k;
rep(i,1,n+1){
cin >> a[i];
ps[i] = ps[i-1]+a[i];
dp[1][i] = 0;
}
k++;
rep(i,2,k+1){
bool d = i&1;
ve.clear();
baz.clear();
ve.pb(0);
baz.pb({0,inf});
rep(j,1,n+1){
int ind = lower_bound(all(baz),make_pair(ps[j]+1,0ll))-1-baz.begin();
int x = ve[ind];
opt[i][j] = x;
dp[d][j] = dp[1-d][x]+ps[x]*(ps[j]-ps[x]);
while (!ve.empty()){
x = ve.back();
if (ps[x] == ps[j]){
if (dp[1-d][x]-ps[x]*ps[x] <= dp[1-d][j]-ps[j]*ps[j]){
baz.pop_back();
ve.pop_back();
continue;
}
break;
}
ll tmp = bar(x,j,1-d);
if (tmp <= baz.back().X){
baz.pop_back();
ve.pop_back();
continue;
}
break;
}
if (ve.empty()){
ve.pb(j);
baz.pb({0,inf});
continue;
}
x = ve.back();
if (ps[x] == ps[j]) continue;
ll tmp = bar(x,j,1-d);
baz.back().Y = tmp;
ve.pb(j);
baz.pb({baz.back().Y,inf});
}
}
cout << dp[k&1][n] << endl;
vector<int> ans;
int cur = n,cnt = k;
while(cnt > 1){
ans.pb(opt[cnt][cur]);
cur = opt[cnt][cur];
cnt--;
}
sort(all(ans));
for (int u : ans) cout << u << ' ';
cout << endl;
}
# | 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... |