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 <iostream>
using namespace std;
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
const int N = 1e5 + 5, K = 205;
const int64_t inf = 1e18;
int64_t f[2][N], a[N];
int opt[K][N], st[N];
void solve(int k, int l, int r, int optl, int optr) {
if (r < l) return;
int i = l + r >> 1;
f[k & 1][i] = -inf;
for (int j = optl; j <= min(i, optr); ++j)
if (ckmax(f[k & 1][i], f[k & 1 ^ 1][j] + a[j] * (a[i] - a[j])))
opt[k][i] = j;
solve(k, l, i - 1, optl, opt[k][i]);
solve(k, i + 1, r, opt[k][i], optr);
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
f[0][0] = -inf;
int n, k; cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i], a[i] += a[i - 1];
for (int l = 1; l <= k; ++l) solve(l, l, n, l, n);
cout << f[k & 1][n] << '\n';
for (int v = n, l = k; v = opt[l--][v]; ) st[l] = v;
for (int l = 0; l < k; ++l) cout << st[l] << ' ';
}
Compilation message (stderr)
sequence.cpp: In function 'void solve(int, int, int, int, int)':
sequence.cpp:13:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int i = l + r >> 1;
~~^~~
sequence.cpp:16:30: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
if (ckmax(f[k & 1][i], f[k & 1 ^ 1][j] + a[j] * (a[i] - a[j])))
~~^~~
sequence.cpp: In function 'int main()':
sequence.cpp:31:27: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
for (int v = n, l = k; v = opt[l--][v]; ) st[l] = v;
~~^~~~~~~~~~~~~
# | 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... |