发布题解

· · 题解

include<bits/stdc++.h>

using namespace std; int n; int mul[35]; int dp[35]; int temp; int b[35][35]; int S[35]; int main(){ cin>>n; for(int i=1;i<=n;i++) { cin>>mul[i]; dp[i]=mul[i]; } for(int i=1;i<n;i++) { for(int j=i+1;j<=n;j++) { cin>>b[i][j]; } } dp[n]=mul[n]; int maxo=dp[n]; int maxIndex=n; for(int i=n;i>=1;i--) { for(int j=i+1;j<=n;j++) { if(b[i][j]&&dp[i]<dp[j]+mul[i]) { dp[i]=dp[j]+mul[i]; S[i]=j; } } if(maxo<dp[i]) { maxo=dp[i]; maxIndex=i; } } int j=maxIndex; while(S[j]) { cout<<j<<' '; j=S[j]; } cout<<j<<endl; cout<<maxo; return 0; }