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;
typedef long long ll;
typedef long double ld;
ll n, m, a[1100], pref[1100], dp[1100][210];
void rec(int n, int m){
if (m == 0)
return;
for (int i = n; i >= 1; i--){
if (dp[i][m-1] + (pref[n]-pref[i])*pref[i] == dp[n][m]){
rec(i, m-1);
cout << i << ' ';
return;
}
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++){
cin >> a[i];
pref[i] = pref[i-1] + a[i];
}
for (int i = 0; i <= n; i++){
for (int j = 0; j <= m; j++)
dp[i][j] = -1e15;
}
dp[1][0] = 0;
for (int ii = 1; ii <= m; ii++){
for (int i = 1; i <= n; i++){
for (int j = i; j >= 1; j--){
dp[i][ii] = max(dp[i][ii], dp[j][ii-1] + (pref[i]-pref[j])*(pref[j]) );
}
}
}
cout << dp[n][m] << endl;
rec(n, m);
}
/**
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... |