# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
416224 | DEQK | Split the sequence (APIO14_sequence) | C++17 | 0 ms | 0 KiB |
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>
#define ll long long
using namespace std;
const int N = 100100;
const ll INF = numeric_limits<ll>::max();
int n, k;
ll a[N];
ll dp[N][2], p[N][205], opt[N];
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] = -INF;
} 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";
int cnt = 1;
vector<int> ans;
for(int i = k, cur = n; i; i--) {
ans.push_back(p[cur][i]);
cur = p[cur][i];
}
for(int i = (int)ans.size() - 1; i >= 0; i--) if(ans[i] > 0) cout << ans[i] << " ", ans++;
if(cnt != k + 1) {
cout << "1";
return 0;
}
}