代码如下:
#include<bits/stdc++.h>
using namespace std;
#define maxN 1005
int a[maxN][maxN],N,ans=0;
void Dfs(int x,int y,int CurrentValue){
//终止条件
if(x==N-1){
if(CurrentValue>ans){
ans=CurrentValue;
}
return;
}
Dfs(x+1,y,CurrentValue+a[x+1][y]);
Dfs(x+1,y+1,CurrentValue+a[x+1][y+1]);
}
int main(){
cin>>N;
for(int i=0;i<N;i++)
for(int j=0;j<=i;j++)
cin>>a[i][j];
Dfs(0,0,a[0][0]);
cout<<ans<<endl;
return 0;
}