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>
#define FOR(x, a, b) for (int x = a; x <= b; ++x)
#define FOD(x, a, b) for (int x = a; x >= b; --x)
#define REP(x, a, b) for (int x = a; x < b; ++x)
#define DEBUG(X) { cout << #X << " = " << X << endl; }
#define PR(A, n) { cout << #A << " = "; FOR(_, 1, n) cout << A[_] << " "; cout << endl; }
#define PR0(A, n) { cout << #A << " = "; REP(_, 0, n) cout << A[_] << " "; cout << endl; }
#define BitCount(x) __builtin_popcount(x)
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
const int M = 2e2 + 10;
int n, m, ansSize = 0;
int a[N], ans[N];
int S[N];
int dp[M][N];
void Compute(int p, int l, int r, int u, int v) {
if (l > r) return;
int m = (l + r) >> 1;
int x = u, y = min(m - 1, v), best = -1;
FOR(i, x, y)
if (dp[p][m] < dp[p - 1][i] + S[i] * (S[m] - S[i])) {
dp[p][m] = dp[p - 1][i] + S[i] * (S[m] - S[i]);
best = i;
}
Compute(p, l, m - 1, u, best);
Compute(p, m + 1, r, best, v);
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // LOCAL
scanf("%d%d", &n, &m);
FOR(i, 1, n) scanf("%d", &a[i]);
FOR(i, 1, n) S[i] = S[i - 1] + a[i];
FOR(i, 1, m) {
Compute(i, 1, n, 1, n);
}
printf("%d\n", dp[m][n]);
int j = n;
FOD(i, m, 1)
FOR(k, 1, j)
if (dp[i - 1][k] + S[k] * (S[j] - S[k]) == dp[i][j]) {
ans[++ansSize] = k;
j = k;
break;
}
FOD(i, ansSize, 1) printf("%d ", ans[i]);
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:42:26: 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:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
FOR(i, 1, n) 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... |