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;
const int inf = 2e9;
int dp[100001][202], a[100001], n, m;
int par[100001][202];
void solve () {
cin >> n >> m; m++;
for (int i = 1; i <= n; i++) {
cin >> a[i]; a[i] += a[i - 1];
}
memset(par, -1, sizeof(par));
for (int i = 1; i <= n; i++) {
dp[i][0] = -inf;
}
auto f = [&] (int l, int r) -> int {
return (a[r] - a[l - 1]) * a[l - 1];
};
for (int j = 1; j <= m; j++) {
for (int i = 0; i < j; i++) {
dp[i][j] = -inf;
}
for (int i = j; i <= n; i++) {
dp[i][j] = -inf;
for (int k = i - 1; k >= 0; k--) {
int x = dp[k][j - 1] + f(k + 1, i);
if (x > dp[i][j]) {
dp[i][j] = x;
par[i][j] = k;
}
}
}
}
cout << dp[n][m] << '\n';
vector <int> u;
int t = n, c = m;
while (true) {
if (par[t][c] == -1) {
break;
}
u.push_back(par[t][c]);
t = par[t][c]; c--;
}
u.pop_back();
reverse(u.begin(), u.end());
for (auto i : u) cout << i << " ";
cout << '\n';
}
signed main () {
ios::sync_with_stdio(0); cin.tie(0);
int tc = 1; //cin >> tc;
while (tc--) solve();
}
# | 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... |