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
const int ar = 1e5 + 5;
int n, k;
ll sum[ar], pos[205][ar], dp[205][ar];
void sub1234() {
for(int i = 1; i <= k; ++i) {
for(int j = i; j <= n; ++j) {
for(int h = i - 1; h < j; ++h) {
dp[i][j] = max(dp[i][j], dp[i - 1][h] + (sum[j] - sum[h]) * (sum[n] - sum[j]));
if(dp[i][j] == dp[i - 1][h] + (sum[j] - sum[h]) * (sum[n] - sum[j])) pos[i][j] = h;
}
}
}
ll mx = -1, ind = 0;
for(int i = k; i <= n; ++i) {
if(dp[k][i] > mx) mx = dp[k][i], ind = i;
}
cout << mx << '\n';
for(int i = k; i; --i) {
cout << ind << ' ';
ind = pos[i][ind];
}
exit(0);
}
void cal(int l, int r, int nl, int nr, int cur) {
if(l > r) return;
int m = l + r >> 1;
for(int i = max(nl, cur - 1); i <= min(m, nr); ++i) {
if(dp[cur][m] < dp[cur - 1][i] + (sum[m] - sum[i]) * (sum[n] - sum[m])) {
dp[cur][m] = dp[cur - 1][i] + (sum[m] - sum[i]) * (sum[n] - sum[m]);
pos[cur][m] = i;
}
}
cal(l, m - 1, nl, pos[cur][m], cur);
cal(m + 1, r, pos[cur][m], nr, cur);
}
void sub5() {
for(int i = 1; i <= k; ++i) cal(1, n, 0, n, i);
ll mx = -1, ind = 0;
for(int i = k; i <= n; ++i) {
if(dp[k][i] > mx) mx = dp[k][i], ind = i;
}
cout << mx << '\n';
for(int i = k; i; --i) {
cout << ind << ' ';
ind = pos[i][ind];
}
exit(0);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; ++i) {
int x; cin >> x;
sum[i] = sum[i - 1] + x;
}
if(n <= 1000) sub1234();
sub5();
}
Compilation message (stderr)
sequence.cpp: In function 'void cal(int, int, int, int, int)':
sequence.cpp:32:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
32 | 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... |