View Code of Problem 4063

#include <bits/stdc++.h>
using namespace std;    
int a[1001][1001],dp[1001][1001];
int main(){
    int n;
    while(cin>>n, n){
        for(int i = 0; i<n; i++){
            for(int j = 0; j<=i; j++){
                cin>>a[i][j];
            }
        }
        for(int i = 0; i<n; i++){
            dp[n-1][i] = a[n-1][i];
        }
        for(int i = n-2; i>=0; i--){
            for(int j = 0; j<=i; j++){
                dp[i][j] = min(dp[i+1][j], dp[i+1][j+1])+a[i][j];
            }
        }
        cout<<dp[0][0]<<endl;

    }


    return 0;
}

Double click to view unformatted code.


Back to problem 4063