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;
typedef int ll;
typedef long long lol;
#define cl const ll
#define mid ((l + r) >> 1)
cl N = 1e5 + 3, K = 201;
ll par [K][N], n, k, cur, t;
lol dp [2][N], ps [N];
void solve (cl &l = 1, cl &r = n, cl &optl = 1, cl &optr = n) {
ll m = mid, till = min(m, optl);
ll bst = -1; lol mx = -1;
for (ll i = m; i >= till; i--) {
lol p = dp[t][i - 1] + ps[i - 1] * (ps[m] - ps[i - 1]);
if (p > mx) mx = p, bst = i;
}
dp[1 - t][m] = mx;
par[cur][m] = bst - 1;
if (l == r) return;
solve(l, m, optl, bst);
solve(m + 1, r, bst, optr);
}
int main ()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>> n >> k;
for (ll i = 1; i <= n; i++) cin>> ps[i], ps[i] += ps[i - 1];
for (cur = 1, t = 0; cur <= k; cur++, t ^= 1) solve();
cout<< dp[k & 1][n] << '\n';
cur = n;
for (ll i = k; i > 0; --i) cur = par[i][cur], cout<< cur << ' ';
return 0;
}
# | 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... |