Logo
(追記) (追記ここまで)

23674번 - Balanced Rainbow Sequence 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 (추가 시간 없음) 512 MB59211442.424%

문제

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 $S$ is balanced if it satisfies one of the following conditions:

  1. $S$ is empty.
  2. $S = XY,ドル where both $X$ and $Y$ are non-empty balanced bracket sequences.
  3. $S = (X),ドル where $X$ 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 $n$ (1ドル \leq n \leq 6000$), the length of the bracket sequence.

The second line contains a string $s$ of length $n$ consisting of characters "(" and ")", the bracket sequence.

The third line contains $n$ integers $c_i$ (0ドル \leq c_i \leq 2$). $c_i$ denotes the color of the $i$-th bracket. 0ドル$ corresponds to lime, 1ドル$ to gray and 2ドル$ 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.

제한

예제 입력 1

5
))))(
0 1 2 2 2

예제 출력 1

4

예제 입력 2

3
()(
0 2 1

예제 출력 2

-1

예제 입력 3

6
(()())
0 0 0 0 0 0

예제 출력 3

0

힌트

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

출처

Contest > Open Cup > 2018/2019 Season > Stage 13: Grand Prix of Bytedance B번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /