이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
const int mod = 1e17;
const int N = 1e5;
/*
7 3
4 1 3 4 0 2 3
*/
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, k; cin >> n >> k;
vector< vector<int> > dp(n + 1, vector<int>(k + 1, -mod));
vector< vector<int> > dp1(n + 1, vector<int>(k + 1, mod));
vector<int> a(n + 1, 0);
for(int i = 1;i <= n; i++) cin >> a[i];
vector<int> suff(n + 1, 0), pref(n + 1, 0);
suff[n] = a[n];
for(int i = 1;i <= n; i++) pref[i] = pref[i-1] + a[i];
for(int i = n-1;i >= 1; i--) suff[i] = suff[i + 1] + a[i];
dp1[0][0] = 0, dp[0][0] = 0;
for(int i = 1;i <= n; i++){
for(int sub = 0; sub <= k; sub++){
dp[i][sub] = max(dp[i-1][sub], dp[i][sub]);
if(sub == 0) continue;
for(int j = i; j >= 1; j--){
int pr = pref[i] - pref[j-1];
dp[i][sub] = max(dp[i][sub], dp[j-1][sub-1] + (suff[i+1] * pr));
}
}
}
vector<int> path;
int ans = dp[n][k];
cout << dp[n][k] << '\n';
for(int i = n;i >= 1; i--){
if(dp[i][k] != ans){
ans = dp[i][k];
k--;
path.push_back(i);
}
}
reverse(all(path));
for(auto x : path) cout << x << " ";
return 0;
}
# | 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... |