QOJ.ac

QOJ

Limite de temps : 2 s Limite de mémoire : 512 MB Points totaux : 100

#7973. Parentheses

Statistiques

A string $s$ consisting only of ( and ) is defined as a bracket sequence. A valid bracket sequence is defined as follows:

  • The empty string is a valid bracket sequence.
  • If $A$ and $B$ are both valid bracket sequences, then $A+B$ is also a valid bracket sequence.
  • If $A$ is a valid bracket sequence, then $(A)$ is also a valid bracket sequence.

Given a bracket sequence $s$ of length $2n$, modifying the $i$-th bracket has a cost $a_i$. There are $q$ queries, each consisting of modifying $a_i$ and then asking for the minimum cost to transform $s$ into a valid bracket sequence. Modifications are permanent, and queries are independent.

Input

The first line contains two positive integers $n$ and $q$.

The next line contains $2n$ non-negative integers $a_1, \ldots, a_{2n}$.

The next line contains a bracket sequence $s$ of length $2n$.

The next $q$ lines each contain two non-negative integers $i$ and $x$, indicating that $a_i$ is updated to $x$.

Output

Output $q+1$ lines, representing the answer for the initial state and after each modification, respectively.

Examples

Input 1

3 5
9 2 2 2 2 10
())(((
3 7
5 10
6 5
2 2
6 2

Output 1

14
14
14
9
9
6

Input 2

5 10
5 10 6 8 3 6 2 1 16 11
((())()(((
4 9
9 9
2 11
4 20
7 9
1 5
9 16
8 4
2 18
4 20

Output 2

12
12
12
12
12
12
12
12
15
15
15

Examples 3~5

See the provided files.

Constraints

For all data, $1\le n\le 10^5$, $1\le q\le 5\times 10^5$, $0\le a_i, x\le 10^9$, and $s$ is a bracket sequence.

  • Subtask 1 (15 pts): $n, q\le 100$
  • Subtask 2 (10 pts): $n, q\le 1000$, $s$ is generated randomly among all bracket sequences
  • Subtask 3 (10 pts): $n, q\le 1000$
  • Subtask 4 (5 pts): $a_i=x=1$
  • Subtask 5 (25 pts): $q\le 10^5$
  • Subtask 6 (35 pts): No special restrictions

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download
#122EditorialOpen题解dXqwq2025-12-12 23:11:42View

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 1
IDTypeStatusTitlePosted ByLast UpdatedActions
#1250General DiscussionOpenFix the scoringQingyu2026-03-11 17:06:23View

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.