分析:数字三角形,经典题
1 /* 2 PROB:numtri 3 ID:wanghan 4 LANG:C++ 5 */ 6 #include "iostream" 7 #include "cstdio" 8 #include "cstring" 9 #include "string" 10 using namespace std; 11 const int maxn=1000+10; 12 int a[maxn][maxn]; 13 int dp[maxn][maxn]; 14 int n; 15 int main() 16 { 17 freopen("numtri.in","r",stdin); 18 freopen("numtri.out","w",stdout); 19 cin>>n; 20 for(int i=1;i<=n;i++){ 21 for(int j=1;j<=i;j++) 22 cin>>a[i][j]; 23 } 24 for(int i=1;i<=n;i++){ 25 for(int j=1;j<=i;j++) 26 dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+a[i][j]; 27 } 28 int ans=0; 29 for(int i=1;i<=n;i++) 30 ans=max(ans,dp[n][i]); 31 cout<<ans<<endl; 32 }