My Solution
```
/**
 * Time Complexity: O(n)
 * Space Complexity: O(1)
 */
class Solution {
 public:
  long long minCost(const int n, const vector<vector<int>> &cost) {
    constexpr int n_colors = 3;
    constexpr int range = 2;
    constexpr int beautiful_colors_2[][2] = {
      {0, 1},
      {0, 2},
      {1, 0},
      {1, 2},
      {2, 0},
      {2, 1}
    };
    constexpr int beautiful_colors_4[][4] = {
      {0, 1, 1, 0},
      {0, 1, 1, 2},
      {0, 1, 2, 0},
      {0, 2, 1, 0},
      {0, 2, 2, 0},
      {0, 2, 2, 1},
      {1, 0, 0, 1},
      {1, 0, 0, 2},
      {1, 0, 2, 1},
      {1, 2, 0, 1},
      {1, 2, 2, 0},
      {1, 2, 2, 1},
      {2, 0, 0, 1},
      {2, 0, 0, 2},
      {2, 0, 1, 2},
      {2, 1, 0, 2},
      {2, 1, 1, 0},
      {2, 1, 1, 2}
    };
    unsigned long long dp[range][n_colors][n_colors];
    memset(dp, -1, sizeof(dp));
    int previous = 0;
    int current = 1;
    for (const auto [ci, cj] : beautiful_colors_2) {
      dp[previous][ci][cj] = cost.front()[ci] + cost.back()[cj];
    }

    for (int i = 1; i < (n >> 1); ++i) {
      const int j = n - i - 1;
      for (const auto [ci, cj, pci, pcj] : beautiful_colors_4) {
        dp[current][ci][cj] = min(dp[current][ci][cj], dp[previous][pci][pcj] + cost[i][ci] + cost[j][cj]);
      }

      previous ^= 1;
      current ^= 1;
      memset(dp[current], -1, sizeof(dp[current]));
    }

    unsigned long long ret = -1;
    for (const auto [ci, cj] : beautiful_colors_2) {
      ret = min(ret, dp[previous][ci][cj]);
    }
    return static_cast<long long>(ret);
  }
};
```
['C++']