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 fi first
#define se second
typedef long long ll;
typedef pair<int,int> ii;
const int N = 1e5;
const int K = 2e2;
const ll INF = 1e18;
int n, k;
int a[N+2];
int bt[K+2][N+2];
ll sum[N+2];
ll dp[2][N+2];
ll cost(int l, int r) {
return (sum[r]-sum[l-1])*sum[l-1];
}
void compute(int id, int l, int r, int x, int y) {
if (l > r) return;
int mid = (l+r)/2, opt = -1;
dp[id&1][mid] = -INF;
for (int i = x; i <= min(y,mid); i++) {
if (dp[id&1][mid] < dp[id&1^1][i-1]+cost(i,mid)) {
dp[id&1][mid] = dp[id&1^1][i-1]+cost(i,mid);
bt[id][mid] = i-1;
opt = i;
}
}
compute(id,l,mid-1,x,opt); compute(id,mid+1,r,opt,y);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k; k++;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum[i] = sum[i-1]+a[i];
}
for (int i = 2; i <= k; i++) compute(i,i,n,i,n);
vector<int> res;
for (int i = n, j = k; i >= 1; i = bt[j][i], j--) {
res.push_back(bt[j][i]);
}
sort(res.begin(),res.end());
cout << dp[k&1][n] << '\n';
for (int i = 1; i < res.size(); i++) {
if (i != 1) cout << ' ';
cout << res[i];
}
cout << '\n';
return 0;
}
/*
7 3
4 1 3 4 0 2 3
*/
Compilation message (stderr)
sequence.cpp: In function 'void compute(int, int, int, int, int)':
sequence.cpp:28:34: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
28 | if (dp[id&1][mid] < dp[id&1^1][i-1]+cost(i,mid)) {
| ~~^~
sequence.cpp:29:34: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
29 | dp[id&1][mid] = dp[id&1^1][i-1]+cost(i,mid);
| ~~^~
sequence.cpp: In function 'int main()':
sequence.cpp:51:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for (int i = 1; i < res.size(); i++) {
| ~~^~~~~~~~~~~~
# | 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... |