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;
int N, M;
int A[100005];
long long pre[100005];
int opt[205][100005];
long long dp[2][100005];
int ans[105];
long long cost(int l, int r) {
return (pre[r] - pre[l - 1]) * pre[l - 1];
}
void dnc(int K, int l, int r, int kl, int kr) {
if(l > r) return;
int mid = (l + r) >> 1;
long long best = -1e18, tmp;
int id = 0;
for(int i = min(mid, kr); i >= kl; i--) {
tmp = dp[!(K & 1)][i - 1] + cost(i, mid);
if(tmp > best) {
best = tmp, id = i;
}
}
dp[K & 1][mid] = best;
opt[K][mid] = id;
dnc(K, l, mid - 1, kl, id);
dnc(K, mid + 1, r, id, kr);
}
void backtrack(int K, int now) {
ans[K] = now;
if(K == 0) return;
backtrack(K - 1, opt[K][now] - 1);
}
int main() {
// freopen("inp", "r", stdin);
scanf("%d %d", &N, &M);
for(int i = 1; i <= N; i++) {
scanf("%d", &A[i]);
pre[i] = A[i] + pre[i - 1];
}
for(int k = 1; k <= M; k++) {
dnc(k, k + 1, N, k + 1, N);
}
long long best = 0, id = 0;
backtrack(M, N);
printf("%lld\n", dp[M & 1][N]);
for(int i = 0; i < M; i++) {
printf("%d ", ans[i]);
}
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:49:13: warning: unused variable 'best' [-Wunused-variable]
long long best = 0, id = 0;
^~~~
sequence.cpp:49:23: warning: unused variable 'id' [-Wunused-variable]
long long best = 0, id = 0;
^~
sequence.cpp:41:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &M);
~~~~~^~~~~~~~~~~~~~~~~
sequence.cpp:43:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &A[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... |