White Light 2
Time limit1sMemory limit1024 MB
Given a row of N red, green, or blue lights, find the minimum cost to turn off lights from either end or recolor lights so the lit colors repeat RGB.
- Level
Medium7 of 10
- Topics
- Dynamic programming, String, Greedy
- Solved
- No attempts yet
Problem
There are N lights in a row, numbered 1 to N from left to right. Each light is red, green, or blue. The colors are given by a string S: light i is red if the i-th character of S is R, green if it is G, and blue if it is B. At the start, all lights are on.
As long as at least one light is on, JOI can perform the following three operations in any order and any number of times. He may also perform no operations at all.
A: Pay A yen to turn off the leftmost lit light.B: Pay B yen to turn off the rightmost lit light.C: Pay C yen to choose one lit light and relight it in any color.
Seen from far away, the lights look white when the colors of the lit lights, read from left to right, form a repetition of RGB, such as RGBRGB...RGB. A state with no lit lights also counts as a repetition of RGB. Sequences such as GBRGBR or RGBRG do not satisfy this condition.
Given the lights and the cost of each operation, find the minimum total cost needed to make the colors of the lit lights a repetition of RGB.
Input
N
S
A B C
Output
Print the minimum total cost needed to make the colors of the lit lights a repetition of RGB, on one line, without the unit (yen).
Constraints
Sis a string of lengthN, and each character isR,G, orB.N,A,B, andCare integers.