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;
using ll = long long;
ll dp[1005][205];
ll pref[1005];
int par[1005][205];
int main(){
int n,k;
cin >> n >> k;
vector<int> a(n);
for(auto &x: a){
cin >> x;
}
for(int i = 0; i < n; i++){
pref[i+1] = pref[i] + a[i];
}
ll sm = accumulate(a.begin(),a.end(),0);
int mx = 0,id = 0;
for(int i = 1; i <= n; i++){
sm -= a[i-1];
for(int j = 1; j <= k; j++){
for(int l = 0; l < i; l++){
ll res = dp[l][j-1] + (pref[i] - pref[l]) * sm;
if(res > dp[i][j]){
dp[i][j] = res;
par[i][j] = l;
}
}
}
if(dp[i][k] > mx){
mx = dp[i][k];
id = i;
}
}
cout << dp[id][k] << "\n";
vector<int> res;
while(id != 0){
res.push_back(id);
id = par[id][k--];
}
reverse(res.begin(), res.end());
for(int x: res){
cout << x << " ";
}
}
# | 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... |