CF BUDDY
← Problems·

1776L · Controllers

1500 · binary search, math

Problem: You are at your grandparents' house and you are playing an old video game on a strange console. Your controller has only two buttons and each button has a number written on it.

Initially, your score is 00. The game is composed of nn rounds. For each 1in1\le i\le n, the ii-th round works as follows.

On the screen, a symbol sis_i appears, which is either +\texttt{+} (plus) or -\texttt{-} (minus). Then you must press one of the two buttons on the controller once. Suppose you press a button with the number xx written on it: your score will increase by xx if the symbol was +\texttt{+} and will decrease by xx if the symbol was -\texttt{-}. After you press the button, the round ends.

After you have played all nn rounds, you win if your score is 00.

Over the years, your grandparents bought many different controllers, so you have qq of them. The two buttons on the jj-th controller have the numbers aja_j and bjb_j written on them. For each controller, you must compute whether you can win the game playing with that controller.

Input Format: The first line contains a single integer nn (1n21051 \le n \le 2\cdot 10^5) — the number of rounds.

The second line contains a string ss of length nn — where sis_i is the symbol that will appear on the screen in the ii-th round. It is guaranteed that ss contains only the characters +\texttt{+} and -\texttt{-}.

The third line contains an integer qq (1q1051 \le q \le 10^5) — the number of controllers.

The following qq lines contain two integers aja_j and bjb_j each (1aj,bj1091 \le a_j, b_j \le 10^9) — the numbers on the buttons of controller jj.

Output Format: Output qq lines. On line jj print YES\texttt{YES} if the game is winnable using controller jj, otherwise print NO\texttt{NO}.

Note: In the first sample, one possible way to get score 00 using the first controller is by pressing the button with numnber 11 in rounds 11, 22, 44, 55, 66 and 88, and pressing the button with number 22 in rounds 33 and 77. It is possible to show that there is no way to get a score of 00 using the second controller.

Sample Cases

Case 1

Input

8
+-+---+-
5
2 1
10 3
7 9
10 10
5 3

Output

YES
NO
NO
NO
YES

Case 2

Input

6
+-++--
2
9 7
1 1

Output

YES
YES

Case 3

Input

20
+-----+--+--------+-
2
1000000000 99999997
250000000 1000000000

Output

NO
YES

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback