Python 4-line cached DFS
Code
# Code
```python3 []
class Solution:
    def minCost(self, n: int, cost: List[List[int]]) -> int:
        @cache
        def dfs(i, prev_l, prev_r):
            return (i != len(cost) // 2) and min(
                cost[i][l] + cost[-i - 1][r] + dfs(i + 1, l, r)
                for l, r in [(0, 1), (0, 2), (1, 2), (2, 1), (1, 0), (2, 0)]
                if l != prev_l and r != prev_r)
        return dfs(0, -1, -1)

```
['Python3']