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 f first
#define s second
#define int long long
using namespace std;
const int N = 1e5 + 69;
const int K = 220;
int n, a[N], dp[N][K], p[N], k;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n >> k;
for (int i = 1;i <= n;i++)
cin >> a[i];
for (int i = 1;i <= n;i++)
p[i] = p[i - 1] + a[i];
auto Get = [&](int l, int r) {
return p[r] - p[l - 1];
};
for (int i = 0;i <= n;i++) {
for (int j = 0;j <= k;j++)
dp[i][j] = -1e18;
}
dp[0][0] = 0;
for (int i = 1;i < n;i++) {
for (int j = 1;j <= k;j++) {
for (int k = i - 1;k >= 0;k--) {
dp[i][j] = max(dp[i][j], dp[k][j - 1] + Get(k + 1, i) * Get(i + 1, n));
}
}
}
int ans = 0;
for (int i = 1;i < n;i++)
ans = max(ans, dp[i][k]);
vector<int> res;
int t = -1;
for (int i = 1;i < n;i++) {
if (ans == dp[i][k])
t = i;
}
assert(t != -1);
res.push_back(t);
for (int j = t - 1;j >= 1 && k > 0;j--) {
int dd = dp[j][k - 1] + Get(j + 1, t) * Get(t + 1, n);
if (dd == dp[t][k]) {
res.push_back(j);
--k;
t = j;
}
}
cout << ans << '\n';
reverse(res.begin(), res.end());
for (auto j : res)
cout << j << ' ';
}
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... |