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