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];
long long dp[205][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 = 0, tmp;
int id = 0;
for(int i = kl; i <= mid && i <= kr; i++) {
tmp = dp[K - 1][i - 1] + cost(i, mid);
if(tmp > best) {
best = tmp, id = i;
}
}
dp[K][mid] = best;
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;
for(int i = now; i >= K; i--) {
if(dp[K][now] == cost(i, now) + dp[K - 1][i - 1]) {
backtrack(K - 1, i - 1);
break;
}
}
}
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][N]);
for(int i = 0; i < M; i++) {
printf("%d ", ans[i]);
}
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:52:13: warning: unused variable 'best' [-Wunused-variable]
long long best = 0, id = 0;
^~~~
sequence.cpp:52:23: warning: unused variable 'id' [-Wunused-variable]
long long best = 0, id = 0;
^~
sequence.cpp:44: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:46: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... |