This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 100100;
int n, k;
ll a[N];
ll dp[N][2], p[N][201], opt[N];
int cur;
ll cost(int i,int j) {
assert(j < i);
return dp[j][0] + a[j] * (a[i] - a[j]);
}
void solve(int l,int r,int tl,int tr) {
if(l > r) return;
int m = l + r >> 1;
opt[m] = -1;
for(int j = tl; j <= min(m - 1, tr); ++j) {
if(opt[m] == -1 || cost(m, opt[m]) < cost(m, j)) {
opt[m] = j;
}
}
if(opt[m] == -1) {
dp[m][1] = -1e17;
} else {
dp[m][1] = cost(m, opt[m]);
}
solve(l, m - 1, tl, opt[m]);
solve(m + 1, r, opt[m], tr);
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
a[i] += a[i - 1];
}
for(int j = 1; j <= k; j++) {
for(int i = 1; i <= n; i++) {
dp[i][0] = dp[i][1];
}
solve(1, n, 1, n);
for(int i = 1; i <= n; ++i) {
p[i][j] = opt[i];
}
}
cout << dp[n][1] << "\n";
for(int j = k, cur = n; j; --j) {
if(p[cur][j])cout << p[cur][j] << ' ';
cur = p[cur][j];
}
}
Compilation message (stderr)
sequence.cpp: In function 'void solve(int, int, int, int)':
sequence.cpp:17:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
17 | int m = l + r >> 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... |