Under construction

Still being built. Things may change

Balanced Rainbow Sequence

Time limit2sMemory limit512 MB

Summary
Flip the fewest brackets so that the sequence stays balanced after removing all lime brackets and after removing all gray brackets.
Level

Hard8 of 10

Topics
Dynamic programming, String, Prefix sum
Solved
No attempts yet

Problem

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?

Input

The first line contains a single integer nn (1≤n≤60001 \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 cic_i (0≤ci≤20 \leq c_i \leq 2). cic_i denotes the color of the ii-th bracket. 00 corresponds to lime, 11 to gray and 22 to blue.

Output

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

Hint

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

Examples3

  1. Example 1

    Input
    5
    ))))(
    0 1 2 2 2
    
    Expected output
    4
    
  2. Example 2

    Input
    3
    ()(
    0 2 1
    
    Expected output
    -1
    
  3. Example 3

    Input
    6
    (()())
    0 0 0 0 0 0
    
    Expected output
    0