1class Solution:2def minCost(3self, houses: List[int], cost: List[List[int]], m: int, n: int, target: int4) -> int:5@cache6def dp(i, p, h):7if (h > target) or (i == m and h != target):8return inf9if i == m:10return 011if houses[i] != 0:12return dp(i + 1, houses[i], h + int(p != houses[i]))1314best = inf15for j, c in enumerate(cost[i], 1):16best = min(best, dp(i + 1, j, h + int(p != j)) + c)17return best1819res = dp(0, 0, 0)20return res if res != inf else -1