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>
using namespace std;
using ll = long long;
int n, k, a[100100], bt[202][100100];
ll s[100100], dp[100100], pdp[100100];
array<ll, 3> t[100100];
int front, back;
bool check(int a, int b, int c) {
return (t[b][0]-t[a][0]) * (t[c][1]-t[a][1]) >= (t[c][0]-t[a][0]) * (t[b][1]-t[a][1]);
}
void f(int k, int u) {
if(k == 0) return;
f(k-1, bt[k][u]);
cout << u << " ";
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
for(int i=1;i<=n;i++) cin >> a[i];
for(int i=1;i<=n;i++) s[i] = s[i-1] + a[i];
int mx = 0;
for(int j=1;j<=k;j++) {
front = back = 0;
t[0] = {s[n]+s[j-1], dp[j-1] - s[n]*s[j-1], j-1};
for(int i=j;i<n;i++) {
auto &f = front, &b = back;
while(f < b && t[f][0]*s[i]+t[f][1] <= t[f+1][0]*s[i]+t[f+1][1]) f++;
auto [X, Y, id] = t[f];
dp[i] = X*s[i]+Y - s[i]*s[i], bt[j][i] = id;
t[++b] = {s[n]+s[i], pdp[i] - s[n]*s[i], i};
while(f+1 < b && check(b-2, b-1, b)) b--, t[b] = t[b+1];
if(j == k && dp[i] >= dp[mx]) mx = i;
}
for(int i=1;i<=n;i++) pdp[i] = dp[i];
}
cout << dp[mx] << "\n";
f(k, mx);
}
# | 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... |