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>
#define ll long long
#define pb push_back
#define s second
#define f first
#define left L[id][h],l,(l + r)/2
#define right R[id][h],(l + r)/2 + 1,r
#define pii pair <int,int>
#define int ll
using namespace std;
const int N = 1e4 + 3,inf = 2e9;
int p[N],lst[N][203];
ll dp[N][203];
pii t[203][4*N];
int vis[203][4*N],cnt[203],L[203][4*N],R[203][4*N];
ll f(pii line,int x){
return x * line.f + line.s;
}
void upd(int id,int h,int l,int r,pii line,int idx){
if (!vis[id][h]){
vis[id][h] = idx;
t[id][h] = line;
return;
}
int mid = (l + r)/2;
if (f(t[id][h],mid) < f(line,mid))
swap(t[id][h],line),vis[id][h] = idx;
if (l == r) return;
if (!L[id][h]) L[id][h] = ++cnt[id];
if (!R[id][h]) R[id][h] = ++cnt[id];
if (f(t[id][h],l) > f(line,l)){
upd(id,right,line,idx);
}else{
upd(id,left,line,idx);
}
}
pii get(int id,int h,int l,int r,int x){
pii cur = {0,0};
if (vis[id][h]) cur = {f(t[id][h],x - 1),vis[id][h]};
if (l == r) return cur;
pair <ll,int> res = {-1e18,0};
if (x > (l + r)/2) {
if (R[id][h]) res = get(id,right,x);
}else{
if (L[id][h]) res = get(id,left,x);
}
if (res.f > cur.f) return res;
return cur;
}
signed main() {
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n,k;
cin>>n>>k;
for (int i = 1; i <= n; i++){
cin >> p[i];
p[i] += p[i - 1];
}
for (int r = 0; r <= n + 1; r++)
for (int kk = 1; kk <= k; kk++)
dp[r][kk] = -1;
for (int i = 1; i <= k; i++)
cnt[i] = 1;
for (int i = 2; i <= n; i++){
for (int j = 1; j <= min(k,i - 1); j++){
if (j == 1){
int s1 = p[n] - p[i - 1],s2 = p[i - 1];
int cur = s1*s2;
if (cur > dp[i][j]){
dp[i][j] = cur;
lst[i][j] = i;
}
continue;
}
int s1 = p[n] - p[i - 1];
pii res = get(j - 1,1,1,2e9,s1+1);
dp[i][j] = res.f + s1*p[i - 1];
lst[i][j] = res.s;
}
for (int j = 1; j <= min(k,i - 1); j++){
if (dp[i][j] != -1) upd(j,1,1,2e9,{-p[i - 1],dp[i][j]},i);
}
}
int id=n;
for (int i = 1; i <= n; i++){
if (dp[i][k] > dp[id][k]) id = i;
}
cout<<dp[id][k]<<endl;
vector <int> ans;
for (int i = k; i >= 1; i--){
ans.pb(id);
id = lst[id][i];
}
for (int i = k - 1; i >= 0; i--)
cout<<ans[i] - 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... |