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 <iostream>
using namespace std;
const int N = 1e5 + 5, K = 205;
int64_t f[2][N], a[N];
int opt[K][N], s[N];
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k; cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i], a[i] += a[i - 1];
for (int l = 1; l <= k; ++l) {
auto c = [&](int j) { return f[(l & 1) ^ 1][j] - a[j] * a[j]; };
auto val = [&](int j, int x) { return a[j] * x + c(j); };
auto cross = [&](int i, int j, int k) {
return (c(k) - c(i)) * (a[j] - a[i]) - (c(j) - c(i)) * (a[k] - a[i]);
};
for (int i = l, j = 1, t = 0; i <= n; ++i) {
while (i > l && j > t) --j;
while (j < t && val(s[j], a[i]) < val(s[j + 1], a[i])) ++j;
opt[l][i] = s[j];
f[l & 1][i] = val(s[j], a[i]);
while (t > 1 && 0 <= cross(s[t - 1], s[t], i)) --t;
s[++t] = i;
}
}
cout << f[k & 1][n] << '\n';
for (int v = n, l = k; v = opt[l--][v]; ) s[l] = v;
for (int l = 0; l < k; ++l) cout << s[l] << ' ';
}
Compilation message (stderr)
beads.cpp: In function 'int main()':
beads.cpp:30:27: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
for (int v = n, l = k; v = opt[l--][v]; ) s[l] = v;
~~^~~~~~~~~~~~~
# | 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... |