Balanced Rainbow Sequence

아직 제출이 없습니다시간 제한2초메모리 제한512 MB

문제

Lewin and Gan have a parentheses sequence. Nah, that word is clumsy as hell. Okay, one more time:

You are given a bracket sequence. Each bracket has one of 3 colors: lime, gray and blue and is either opening or closing.

You are allowed to change some brackets from opening to closing and vice versa. You are not allowed to change the order or colors of brackets. You want to do zero or more changes such that the resulting sequence satisfies the following:

  1. If all the lime brackets are removed, the remaining brackets form a balanced bracket sequence.
  2. If all the gray brackets are removed, the remaining brackets form a balanced bracket sequence.

A bracket sequence SS is balanced if it satisfies one of the following conditions:

  1. SS is empty.
  2. S=XYS = XY, where both XX and YY are non-empty balanced bracket sequences.
  3. S=(X)S = (X), where XX is a balanced bracket sequence. Note that the starting opening brace and the trailing closing brace may have different colors.

Is it possible to do so and if it is, what is the minimum number of changes you have to make?

입력

The first line contains a single integer nn (1n60001 \leq n \leq 6000), the length of the bracket sequence.

The second line contains a string ss of length nn consisting of characters "(" and ")", the bracket sequence.

The third line contains nn integers c_ic\_i (0c_i20 \leq c\_i \leq 2). c_ic\_i denotes the color of the ii-th bracket. 00 corresponds to lime, 11 to gray and 22 to blue.

출력

Print a single integer --- the minimum number of changes you have to make if it is possible to satisfy the conditions and -1 otherwise.

힌트

In the first example a possible resulting sequence is (()().