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;
#define ll long long
#define mp make_pair
#define pb push_back
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, n) for (int i = 0; i < (int)(n); ++ i)
struct func {
int k; ll b;
func(): k(), b() {}
func(int k, ll b): k(k), b(b) {}
ll get(int x) { return 1LL * k * x + b; }
double inter(func o) {
return -(double)(b - o.b) / (k - o.k);
}
};
int n, k, a[100005], s[100005];
ll dp[100005], ndp[100005];
int pr[100005][205];
func f[100005];
int que[100005], ql, qr;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k;
rep(i, n) cin >> a[i], s[i + 1] = s[i] + a[i];
rep(j, k) {
ql = qr = 0;
rep(i, n) {
f[i] = func(s[i], dp[i] - 1LL * s[i] * s[i]);
while (qr - ql >= 2 && f[que[qr - 2]].inter(f[i]) <= f[que[qr - 2]].inter(f[que[qr - 1]])) -- qr;
que[qr ++] = i;
while (qr - ql >= 2 && f[que[ql]].get(s[i + 1]) <= f[que[ql + 1]].get(s[i + 1])) ++ ql;
ndp[i + 1] = f[que[ql]].get(s[i + 1]);
pr[i + 1][j + 1] = que[ql];
}
memcpy(dp, ndp, sizeof(dp));
}
cout << dp[n] << endl;
vector <int> ans;
int i = n, j = k;
while (j) i = pr[i][j], -- j, ans.pb(i);
reverse(ans.begin(), ans.end());
rep(i, ans.size()) cout << ans[i] << " ";
cout << endl;
return 0;
}
/*
7 3
4 1 3 4 0 2 3
*/
# | 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... |