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;
int main(){
int n, m;
cin >> n >> m;
vector<int> a(n + 1);
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
vector<int> pref(n + 1);
vector<int> suf(n + 2);
for(int i = 1; i <= n; i++) {
pref[i] = pref[i - 1] + a[i];
}
for(int i = n; i > 0; i--) {
suf[i] = suf[i + 1] + a[i];
}
vector<vector<int>> dp(n + 1, vector<int>(m + 1));
for(int i = 0; i <= n; i++) {
for(int j = 0; j <= m; j++) {
dp[i][j] = 0;
}
}
vector<vector<int>> P(n + 1, vector<int>(m + 1));
for(int i = 1; i < n; i++) {
dp[i][1] = (pref[i]) * (suf[i + 1]);
P[i][1] = i;
}
for(int i = 1; i <= n; i++) {
for(int j = 2; j <= min(m, i - 1); j++) {
for(int k = 1; k < i; k++) {
//dp[i][j] = max(dp[i][j], dp[k][j - 1] + (pref[i] - pref[k]) * suf[i + 1]);
int s = dp[k][j - 1] + (pref[i] - pref[k]) * suf[i + 1];
if(s > dp[i][j]) {
P[i][j] = k;
dp[i][j] = s;
}
}
}
}
int ans = 0;
for(int i = m + 1; i <= n; i++) {
ans = max(ans, dp[i][m]);
}
int x = 0;
for(int i = m + 1; i <= n; i++) {
if(ans == dp[i][m]) {
x = i;
}
}
vector<int> R;
R.push_back(x);
while(m > 1) {
int u = P[x][m];
R.push_back(u);
x = u;
m--;
}
reverse(R.begin(), R.end());
cout << ans << "\n";
for(auto it : R) {
cout << it << " ";
}
}
# | 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... |