This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// meowa
#include <bits/stdc++.h>
#define show(x) cerr << #x << "-> " << x << '\n';
#define ll long long
#define all(a) a.begin(), a.end()
using namespace std;
const int mxsz = 1e5+7;
const int inf = 1e9+1e2;
const int mod = 1e9+7;
int n, k, a[mxsz], s[mxsz], dp[mxsz][202], ans;
map <pair<int, int>, pair<int, int>> pr;
int main () {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = n; i >= 1; i--) {
s[i] = s[i+1] + a[i];
}
for (int i = 1; i <= n; i++) {
dp[i][1] = (s[1] - s[i+1]) * s[i+1];
pr[{i, 1}] = {i, 1};
for (int lv = 2; lv <= i; lv++) {
for (int j = 1; j < i; j++) {
if (dp[i][lv] < dp[j][lv-1] + (s[j+1] - s[i+1]) * s[i+1]) {
dp[i][lv] = dp[j][lv-1] + (s[j+1] - s[i+1]) * s[i+1];
pr[{i, lv}] = {j, lv-1};
}
}
}
}
ans = 1;
for (int i = 2; i <= n; i++) {
if (dp[i][k] > dp[ans][k]) {
ans = i;
}
}
cout << dp[ans][k] << '\n';
vector<int> an;
pair<int, int> lt = {ans, k};
while (lt.second != 1) {
an.push_back(lt.first);
lt = pr[lt];
}
an.push_back(lt.first);
reverse(all(an));
for (int x : an) cout << x << ' ';
}
/*
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... |