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;
ll a[N+2], sum[N+2];
ll dp[N+2][K+2], pre[N+2][K+2];
ll cost(int l, int r) {
return (sum[r]-sum[l-1])*sum[l-1];
}
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++) {
for (int j = 1; j <= n; j++) {
dp[j][i] = -INF;
for (int l = j-1; l >= 0; l--) {
if (dp[j][i] < dp[l][i-1]+cost(l+1,j)) {
dp[j][i] = dp[l][i-1]+cost(l+1,j);
pre[j][i] = l;
}
}
}
}
// for (int i = 1; i <= n; i++) {
// for (int j = 0; j < k; j++) {
// ch[j].insert(sum[i-1],dp[i-1][j]-sum[i-1]*sum[i-1],i-1);
// }
// for (int j = 1; j <= k; j++) {
// ii x = ch[j-1].query(sum[i]);
// dp[i][j] = x.fi;
// pre[i][j] = x.se;
// }
// }
vector<int> res;
for (int i = n, j = k; i >= 1; i = pre[i][j], j--) {
res.push_back(pre[i][j]);
}
sort(res.begin(),res.end());
cout << dp[n][k] << '\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 'int main()':
sequence.cpp:55:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | 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... |