#include<iostream> #include<algorithm> #include<math.h> using namespace std; const int N = 1010; int a[N][N], ans, n; void dfs(int x, int y, int sum) { if (x == n) { if (sum < ans) ans = sum; return; } dfs(x + 1, y, sum + a[x + 1][y]); dfs(x + 1, y, sum + a[x + 1][y + 1]); } int main() { while (cin >> n) { ans=1e10; for (int i = 1; i <= n; i++) for (int j = 1; j <= i; j++) cin>>a[i][j]; dfs(1,1,a[1][1]); cout<<ans<<endl; } } |
Double click to view unformatted code.