比赛 2025暑期集训第4场 评测结果 AAAAAAA
题目名称 加分二叉树 最终得分 100
用户昵称 pcx 运行时间 0.019 s
代码语言 C++ 内存使用 3.71 MiB
提交时间 2025-07-05 08:49:03
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
const int N=100;
int n,d[N],dp[N][N],root[N][N];
void f(int l,int r){
	if(l>r) return;
	cout<<root[l][r]<<' ';
	f(l,root[l][r]-1);
	f(root[l][r]+1,r);
}
int main(){
	freopen("jfecs.in","r",stdin);
	freopen("jfecs.out","w",stdout);
	cin>>n;
	for(int i=1;i<=n;i++){
	    cin>>d[i];	
	}
	for(int i=1;i<=n;i++){
		dp[i][i]=d[i];	
		root[i][i]=i;
	}
	for(int len=2;len<=n;len++){
		for(int l=1;l<=n;l++){
			int r=len+l-1;
			if(r>n) continue;
			for(int k=l;k<=r;k++){
				int ls=0,rs=0;
				if(k==l) ls=1;
				else ls=dp[l][k-1];
				if(k==r) rs=1;
				else rs=dp[k+1][r];
				int c=ls*rs+d[k];
				if(c>dp[l][r]){
					dp[l][r]=c;
					root[l][r]=k;
				}
			}
		}
	}
	cout<<dp[1][n]<<endl;
	f(1,n);
	return 0;
}