Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <bits/stdc++.h>
- #include <vector>
- using namespace std;
- const int maxn = 22;
- const int INF = 1e9;
- int n;
- int mat[maxn][3];
- int dp[maxn][3][3];
- int rec(int at, int last_color, int first_color) {
- if(at == n) {
- return 0;
- }
- if(dp[at][last_color][first_color] != -1) {
- return dp[at][last_color][first_color];
- }
- int res = INF;
- if(at == n - 1) {
- for(int color = 0; color <= 2; color++) {
- if(color != last_color and color != first_color) {
- res = min(res, rec(at + 1, color, first_color) + mat[at][color]);
- }
- }
- }
- else {
- for(int color = 0; color <= 2; color++) {
- if(color != last_color) {
- res = min(res, rec(at + 1, color, first_color) + mat[at][color]);
- }
- }
- }
- dp[at][last_color][first_color] = res;
- return res;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin >> n;
- for(int i = 0; i < n; i++) {
- cin >> mat[i][0] >> mat[i][1] >> mat[i][2];
- }
- memset(dp, -1, sizeof dp);
- int res = rec(1, 0, 0) + mat[0][0];
- res = min(res, rec(1, 1, 1) + mat[0][1]);
- res = min(res, rec(1, 2, 2) + mat[0][2]);
- cout << res << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement