1214C.md (1998B)
1 Source: https://codeforces.com/problemset/problem/1214/C 2 3 C. Bad Sequence 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 512 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Petya's friends made him a birthday present — a bracket sequence. Petya was quite disappointed with his gift, because he dreamed of correct bracket sequence, yet he told his friends nothing about his dreams and decided to fix present himself. 22 23 To make everything right, Petya is going to move at most one bracket from its original place in the sequence to any other position. Reversing the bracket (e.g. turning "(" into ")" or vice versa) isn't allowed. 24 25 We remind that bracket sequence $s$ is called correct if: 26 27 * $s$ is empty; 28 * $s$ is equal to "($t$)", where $t$ is correct bracket sequence; 29 * $s$ is equal to $t_1 t_2$, i.e. concatenation of $t_1$ and $t_2$, where $t_1$ and $t_2$ are correct bracket sequences. 30 31 For example, "(()())", "()" are correct, while ")(" and "())" are not. Help Petya to fix his birthday present and understand whether he can move one bracket so that the sequence becomes correct. 32 33 Input 34 35 First of line of input contains a single number $n$ ($1 \leq n \leq 200\,000$) — length of the sequence which Petya received for his birthday. 36 37 Second line of the input contains bracket sequence of length $n$, containing symbols "(" and ")". 38 39 Output 40 41 Print "Yes" if Petya can make his sequence correct moving at most one bracket. Otherwise print "No". 42 43 Examples 44 45 Input 46 47 ``` 48 2 49 )( 50 ``` 51 52 Output 53 54 ``` 55 Yes 56 ``` 57 58 Input 59 60 ``` 61 3 62 (() 63 ``` 64 65 Output 66 67 ``` 68 No 69 ``` 70 71 Input 72 73 ``` 74 2 75 () 76 ``` 77 78 Output 79 80 ``` 81 Yes 82 ``` 83 84 Input 85 86 ``` 87 10 88 )))))((((( 89 ``` 90 91 Output 92 93 ``` 94 No 95 ``` 96 97 Note 98 99 In the first example, Petya can move first bracket to the end, thus turning the sequence into "()", which is correct bracket sequence. 100 101 In the second example, there is no way to move at most one bracket so that the sequence becomes correct. 102 103 In the third example, the sequence is already correct and there's no need to move brackets.