이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define s second
#define f first
#define int ll
using namespace std;
const int N = 1e5 + 3;
int a[N],p[N],dp[N][203],lst[N][203];
void get(int r,int k){
if (r < 1) dp[r][k] = 0;
if (k == 0) dp[r][k] = 0;
if (dp[r][k] != -1) return;
for (int i = 1; i < r; i++){
int s1 = p[i],s2 = p[r] - p[i];
int reward = s1*s2;
get(i,k - 1);
int cur = dp[i][k - 1] + reward;
if (cur > dp[r][k]){
dp[r][k] = cur;
lst[r][k] = i;
}
}
}
signed main() {
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n,k;
cin>>n>>k;
for (int i = 1; i <= n; i++){
cin >> a[i];
p[i] = p[i - 1] + a[i];
}
for (int r = 0; r <= n + 1; r++)
for (int kk = 0; kk <= k; kk++)
dp[r][kk] = -1;
get(n,k);
cout<<dp[n][k]<<endl;
vector <int> ans;
int id = n;
for (int i = k; i >= 1; i--){
id = lst[id][i];
ans.pb(id);
}
for (int i = k - 1; i >= 0; i--)
cout<<ans[i]<<" ";
}
/*
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... |