def min_cost(n, heights, costs):
    dp = [0] * (n + 1)
    dp[1] = costs[1]
    
    for i in range(2, n + 1):
        dp[i] = min(dp[j] + costs[j+1] + abs(heights[i] - heights[j+1]) for j in range(i))
    
    return dp[n]