이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
* author : Apiram
* created: 26.06.2023 00:43:43
*/
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n,k;cin>>n>>k;
vector<long long>arr(n);
for (int i = 0;i<n;++i){
cin>>arr[i];
}
vector<long long>pref(n + 1,0);
for (int i = 0;i<n;++i){
pref[i + 1] = pref[i] + arr[i];
}
auto query = [&](int u,int v){
return pref[v + 1] - pref[u];
};
vector<vector<vector<long long>>>dp(n + 1,vector<vector<long long>>(n + 1,vector<long long>(k + 1,-1)));
function<long long(int,int,int)>solve = [&](int l,int r,int kk){
if (l == r)return dp[l][r][kk] = 0LL;
if (kk == 0)return dp[l][r][kk] = 0LL;
if (dp[l][r][kk] != -1)return dp[l][r][kk];
long long ans = 0;
for (int i = l;i<r;++i){
for (int j = 0;j<kk;++j){
if (i - l >= j && r - i - 1 >= kk - j - 1)
ans = max(ans,solve(l,i,j) + solve(i + 1,r,kk - j - 1) + query(l,i) * query(i + 1,r));
}
}
return dp[l][r][kk] = ans;
};
vector<int>pos;
cout<<solve(0,n - 1,k)<<'\n';
queue<pair<int,pair<int,int>>>q;
q.push({0,{n - 1,k}});
for (int i = 0;i<k;++i){
auto u = q.front();
q.pop();
//cout<<u.first<<" "<<u.second.first<<" "<<u.second.second<<'\n';
int ans = -1;
for (int j = u.first;j<u.second.first;++j){
if (ans != -1)break;
for (int p = 0;p<u.second.second;++p){
if (dp[u.first][j][p] + dp[j + 1][u.second.first][u.second.second - p - 1] + query(u.first,j) * query(j + 1,u.second.first) == dp[u.first][u.second.first][u.second.second]){
ans = 1;
if (p > 0){
q.push({u.first,{j,p}});
}
if (u.second.second - p > 0){
q.push({j + 1,{u.second.first,u.second.second - p - 1}});
}
pos.push_back(j + 1);
break;
}
}
}
}
for (auto x:pos)cout<<x<<" ";
cout<<'\n';
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... |