Emacs++
Time limit60sMemory limit1024 MB
Given a parenthesis string with left, right, and teleport costs per cursor position, find the cheapest move cost for each query and sum them.
- Level
Medium7 of 10
- Topics
- Shortest path, Graph, Tree
- Solved
- No attempts yet
Statement
In 2016's Distributed Code Jam, we introduced the Lisp++ language for Lisp fans who prefer a higher density of parentheses. Here is a reminder of how the syntax works.
A Lisp++ program is a string of balanced parentheses. More formally, a Lisp++ program is one of the following. In this specification, stands for some program code, which does not have to be the same code each time.
()A single opening parenthesis followed by a closing parenthesis. This(matches this), and vice versa.()A program enclosed in a pair of parentheses. This(matches this), and vice versa.- Two programs, not necessarily the same, placed back to back.
This year we announce Emacs++, a text viewer for Lisp++. Emacs++ shows a Lisp++ program of length as a single long line with a cursor you can move. The cursor is a block cursor that always sits on one of the characters, never between two characters.
At any point, you can perform one of the following three actions to move the cursor. Let be the current cursor position, counting from 1 at the leftmost character.
- Move the cursor one character left. If the cursor is already on the leftmost character, nothing happens. This takes seconds.
- Move the cursor one character right. If the cursor is already on the rightmost character, nothing happens. This takes seconds.
- Teleport the cursor to the parenthesis that matches the -th character. This takes seconds.
We want to know how efficient Emacs++ is. There is one Lisp++ program and a list of queries about it. Each query has a start position and an end position . For the -th query, find the smallest time (in seconds) needed to move the cursor from to when you make optimal decisions.
Output the sum of all values.
Input
The first line contains the number of test cases . Each test case follows.
The first line of a test case contains two integers , the length of the Lisp++ program, and , the number of queries.
The second line contains a string of characters, each ( or ), representing a balanced Lisp++ program.
The third, fourth, and fifth lines each contain integers. The -th integers on these lines are , , and , respectively.
The sixth and seventh lines each contain integers. The -th integers on these lines are and , respectively.
Output
For each test case, output one line in the format Case #x: y, where is the test case number starting from 1 and is the sum of all values.
Constraints
- .
- and for at most 9 test cases.
- and in all other cases.
- The length of is , and is a string of balanced parentheses as described above.
- for all .
- for all .
Hint
In the sample case, which obeys the limits for Test Set 1, every move costs 1 second. The shortest times for the queries are as follows.
- Move right from 7 five times to 12, taking 5 seconds.
- Teleport from 4 to 11, taking 1 second.
- Teleport from 4 to 11, then move left to 10, taking 2 seconds.
- Teleport from 12 to 1, taking 1 second.
- Move right from 5 to 6, taking 1 second.
The sum of the query times is 5 + 1 + 2 + 1 + 1 = 10 seconds.