Problem 291
Paint House
Medium · 20 pts · Array, Dynamic Programming
Problem
N houses stand in a row, and each must be painted red, blue or green. Painting house i in each colour has its own cost. Two neighbouring houses may not have the same colour. Find the minimum total cost to paint all the houses. Go through the houses in order, keeping for each colour the cheapest total cost so far if the current house gets that colour. For house i, the cost of colour c is its own cost plus the cheaper of the two other colours from house i - 1.
Input
First line: N. Next N lines: the costs of red, blue and green for that house.
Output
Print the minimum cost.
Constraints
1 <= N <= 100000; 1 <= cost <= 20
Example input
3 17 2 17 16 16 5 14 3 19
Example output
10
Explanation
Paint the first house blue (2), the second green (5) and the third blue (3). Neighbours always differ, and the total cost is 2 + 5 + 3 = 10, the minimum.