Best parentheses
시간 제한2초메모리 제한1024 MB
주어진 괄호열에서 올바른 괄호열이 되는 부분수열을 골라 선택한 위치의 가중치 합을 최대로 만든다.
문제
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 .