JOIG Tour
Time limit1sMemory limit1024 MB
Given paintings labeled J, O, I, G on a line, find for each query the shortest walk from S that visits a J, an O, an I, and a G painting in that order, then ends at T.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Segment tree
- Solved
- No attempts yet
Problem
Do you know Just Odd Ink Way? It is a national road of length in Republic of EGOI, running from the east end to the west end. It is famous for several paintings made by "Just Odd Ink" along the road. Below, we call it JOI Way.
Several paintings of various sizes are on JOI Way. Some of them have characters written on them.
Rie is a tour guide on JOI Way. She plans to guide the participants of the JOIG Spring Training Camp. To cheer them up, she will pick paintings with 'J', 'O', 'I', and 'G' written on them, and visit them in this order. There are candidate paintings. The -th painting () is located at distance from the west end, and the character is written on it.
Rie has plans. In the -th plan (), she travels as follows.
- Rie starts the tour at the place at distance from the west end.
- She picks a painting with '
J' written on it and moves to its location. - She picks a painting with '
O' written on it and moves to its location. - She picks a painting with '
I' written on it and moves to its location. - She picks a painting with '
G' written on it and moves to its location. - She moves to the place at distance from the west end and ends the tour.
Rie may not go outside JOI Way.
For each plan, find the minimum possible total travel distance.
Input
The input is given in the following format.
Output
Print lines. The -th line contains the minimum possible total travel distance for the -th plan.
Constraints
- .
- ().
- ().
- () is one of '
J', 'O', 'I', or 'G'. - is '
J' for at least one (). - is '
O' for at least one (). - is '
I' for at least one (). - is '
G' for at least one (). - .
- ().
- ().
- ().
- , , , , are integers.