RGB Street
InterviewTime limit0.5sMemory limit128 MB
Given per-house costs for three colors, compute the minimum total cost to paint all houses in a line so that no two adjacent houses share a color.
- Level
Easy3 of 10
- Topics
- Dynamic programming, Array
- Solved
- No attempts yet
Problem
There are N houses arranged in a line, numbered from 1 to N.
Each house must be painted one of three colors: red, green, or blue. Given the cost of painting each house in each color, find the minimum total cost to paint all houses while satisfying all of the following rules.
- The color of house 1 must be different from the color of house 2.
- The color of house
Nmust be different from the color of houseN-1. - For every
iwith2 ≤ i ≤ N-1, the color of houseimust be different from the colors of housesi-1andi+1.
Input
The first line contains the number of houses N. (2 ≤ N ≤ 1,000)
Each of the next N lines contains three costs for one house, in order from house 1 to house N: the cost to paint it red, green, and blue. Every cost is a natural number between 1 and 1,000 inclusive.
Output
Print the minimum cost required to paint all houses while satisfying the rules.