logo AlgoBeat OnlineJudge
登录 注册

#215782. [JAG 2023 Summer Camp #3] Best parentheses

内存限制:1024 MiB 时间限制:2000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

A string consisting only of parentheses ‘(’ and ‘)’ is called balanced if it satisfies one of the following conditions.

  • It is an empty string.
  • It is a concatenation of two non-empty balanced strings.
  • It is a concatenation of ‘(’, , and ‘)’, for some balanced string .

You are given characters of parentheses and integers . Then, you have to choose zero or more integers so that they satisfy the following conditions.

  • .
  • The concatenation of is a balanced string.

Note that the above conditions are always satisfied if you choose zero integers.

Your task is to maximize .

输入格式

The input consists of a single test case of the following format.

The first line consists of an integer (). The second line consists of characters , each of which is either ‘(‘ or ‘)’. The third line consists of integers ().

输出格式

Output in a line the maximum possible value of by choosing zero or more integers .

样例

样例输入 1

5
()(()
3 -9 -2 1 0

样例输出 1

3

样例输入 2

6
)()()(
-3 1 -4 1 -5 9

样例输出 2

0