Typing Contest
Time limit2.5sMemory limit1024 MB
Given a 15-letter string and each contestant's press, left, and right costs, find the minimum typing time when the keys can be placed in any order on one row.
- Level
Hard9 of 10
- Topics
- Dynamic programming, Bit manipulation, Geometry
- Solved
- No attempts yet
Problem
In the JOIG country, the JOIG language is used. It has 15 letters: A, B, C, D, E, F, G, H, I, J, K, L, M, N, O.
At a typing contest held in the JOIG country next month, contestants compete on how long it takes to enter a string of length made of these 15 letters. Each contestant types under the following rules.
- A contestant types with one finger.
- The contestant uses a long, narrow keyboard where each of the 15 letter keys appears once in a single row. The contestant may choose the position of each letter key freely.
- The contestant enters by pressing the keys for the 1st, 2nd, ..., th characters of , in that order.
Q contestants will take part. Each contestant has a different typing ability. For the th contestant (), the times are as follows.
- Pressing the key directly under the finger takes milliseconds.
- Moving the finger one key to the left takes milliseconds.
- Moving the finger one key to the right takes milliseconds.
You are given the string and the information for each contestant. For each contestant, find the shortest possible time in milliseconds from pressing the first character of to finishing the last character.
Input
N
S
Q
A1 L1 R1
:
AQ LQ RQ
Output
Print lines. On line (), print the shortest time in milliseconds for contestant to type , from pressing its first character to finishing its last character.
Constraints
- .
- is a string of length .
- Each character of is one of
A,B,C,D,E,F,G,H,I,J,K,L,M,N,O. - .
- ().
- ().
- ().
- , , , , and are integers.