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 int long long
#define all(x) x.begin(), x.end()
using namespace std;
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, k;
cin >> n >> k;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
a[i] += a[i - 1];
}
k++;
vector<vector<int>> dp(k + 1, vector<int>(n + 1, -1));
vector<vector<int>> prev(k + 1, vector<int>(n + 1, -1));
dp[0][0] = 0;
for (int j = 1; j <= k; j++) {
dp[j][0] = 0;
for (int i = j; i <= n; i++) {
for (int i1 = i - 1; i1 >= 0; i1--) {
if (dp[j - 1][i1] == -1) {
continue;
}
int val = dp[j - 1][i1] + a[i1] * (a[i] - a[i1]);
if (val > dp[j][i]) {
dp[j][i] = val;
prev[j][i] = i1;
}
}
}
}
int ans = dp[k][n];
cout << ans << '\n';
vector<int> path;
int now = n;
for (int i = k; i >= 2; i--) {
now = prev[i][now];
path.push_back(now);
}
reverse(all(path));
for (int el : path) {
cout << el << ' ';
}
cout << '\n';
}
/*
7 3
4 1 3 4 0 2 3
*/
# | 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... |