Under construction

Still being built. Things may change

Emacs++

Time limit60sMemory limit1024 MB

Summary
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, CC 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.
  • (CC) A program enclosed in a pair of parentheses. This ( matches this ), and vice versa.
  • CCCC 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 KK as a single long line with a cursor you can move. The cursor is a block cursor that always sits on one of the KK characters, never between two characters.

At any point, you can perform one of the following three actions to move the cursor. Let ii 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 LiL_i seconds.
  • Move the cursor one character right. If the cursor is already on the rightmost character, nothing happens. This takes RiR_i seconds.
  • Teleport the cursor to the parenthesis that matches the ii-th character. This takes PiP_i seconds.

We want to know how efficient Emacs++ is. There is one Lisp++ program and a list of QQ queries about it. Each query has a start position SjS_j and an end position EjE_j. For the jj-th query, find the smallest time NjN_j (in seconds) needed to move the cursor from SjS_j to EjE_j when you make optimal decisions.

Output the sum of all NjN_j values.

Input

The first line contains the number of test cases TT. Each test case follows.

The first line of a test case contains two integers KK, the length of the Lisp++ program, and QQ, the number of queries.

The second line contains a string PP of KK characters, each ( or ), representing a balanced Lisp++ program.

The third, fourth, and fifth lines each contain KK integers. The ii-th integers on these lines are LiL_i, RiR_i, and PiP_i, respectively.

The sixth and seventh lines each contain QQ integers. The jj-th integers on these lines are SjS_j and EjE_j, respectively.

Output

For each test case, output one line in the format Case #x: y, where xx is the test case number starting from 1 and yy is the sum of all NjN_j values.

Constraints

  • 1≤T≤1001 \le T \le 100.
  • K=105K = 10^5 and Q=105Q = 10^5 for at most 9 test cases.
  • 2≤K≤10002 \le K \le 1000 and 1≤Q≤10001 \le Q \le 1000 in all other cases.
  • The length of PP is KK, and PP is a string of balanced parentheses as described above.
  • 1≤Sj≤K1 \le S_j \le K for all jj.
  • 1≤Ej≤K1 \le E_j \le K for all jj.

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.

  1. Move right from 7 five times to 12, taking 5 seconds.
  2. Teleport from 4 to 11, taking 1 second.
  3. Teleport from 4 to 11, then move left to 10, taking 2 seconds.
  4. Teleport from 12 to 1, taking 1 second.
  5. Move right from 5 to 6, taking 1 second.

The sum of the query times is 5 + 1 + 2 + 1 + 1 = 10 seconds.

Examples1

  1. Example 1

    Input
    1
    12 5
    (()(((()))))
    1 1 1 1 1 1 1 1 1 1 1 1
    1 1 1 1 1 1 1 1 1 1 1 1
    1 1 1 1 1 1 1 1 1 1 1 1
    7 4 4 12 5
    12 11 10 1 6
    
    Expected output
    Case #1: 10