問題一覧 > 通常問題

No.2591 Cheap Bracket Sequence

レベル : / AI推定難易度 : 800 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 77
作問者 : H20 / テスター : minato
お気に入りにしたユーザー ProblemId : 10351 / 自分の提出
問題文最終更新日: 2023-12-10 23:06:53
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
Advent Calendar Contest 2023 (順位表) の他の問題:

Problem Statement

A string consisting only of ( and ) is called a bracket sequence.

There is a bracket sequence $S$ of length $2N$.

By paying a cost of $A_i$, you can change the $i$-th character from ( to ), or from ) to (.

Find the minimum cost required to make the bracket sequence $S$ balanced.

For a bracket sequence $S$, if it can be turned into the empty string by repeating, $0$ or more times, the operation of removing a place where $2$ consecutive characters are (), then the bracket sequence $S$ is said to be balanced.

Input

$N$
$S$
$A_1$ $A_2$ $\ldots$ $A_{2N}$

Constraints

  • $ 1 \le N \le 1000$
  • $|S|=2N$
  • $S$ consists only of ( and )
  • $ 1 \le A_i \le 10^{9}$
  • $N,A_i$ are integers

Output

Output the minimum cost required to make the bracket sequence $S$ balanced.

Print a newline at the end.

Sample

Sample 1
Input
3
)(((()
10 22 3 5 2 13
Output
15

By flipping the $1$-st, $3$-rd, and $5$-th characters, the bracket sequence becomes (()()), which is balanced. The cost is $15$, and it cannot be made balanced at a smaller cost, so the answer is $15$.

Sample 2
Input
4
()(()())
3141592 653589 793238 462643 38327 9502884 1971 69399375
Output
0

The sequence may already be balanced from the start.

Sample 3
Input
5
((((((((((
1 2 3 4 5 6 7 8 9 10
Output
30

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。