#include <stdio.h>
int dp[1001][3];
int rgb[1001][3];

int min(int a,int b) {
    return a<b ? a:b;
}

int bottom_up(int n) {
    dp[0][0]=rgb[0][0];
    dp[0][1]=rgb[0][1];
    dp[0][2]=rgb[0][2];

    for(int i=1; i<n; i++) {
        dp[i][0]=min(dp[i-1][1],dp[i-1][2])+rgb[i][0];
        dp[i][1]=min(dp[i-1][0],dp[i-1][2])+rgb[i][1];
        dp[i][2]=min(dp[i-1][1],dp[i-1][0])+rgb[i][2];
    }
    return min(dp[n-1][0],min(dp[n-1][1],dp[n-1][2]));
}
int main() {
    int n;
    scanf("%d",&n);
    for(int i=0; i<n; i++) {
        for(int j=0; j<3; j++) {
            scanf("%d",&rgb[i][j]);
        }
    }
    
    printf("%d",bottom_up(n));
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: