#P5581. [PA 2015] Hazard

[PA 2015] Hazard

Description

There are nn people taking turns playing a slot machine game. Initially, person ii has aia_i dollars.

The slot machine can be modeled as a sequence of length mm containing only 11 and 1-1. If you draw 11, you win 11 dollar; if you draw 1-1, you lose 11 dollar.

In round 11, person 11 draws the 11-st element of the sequence. In round 22, person 22 draws the 22-nd element. In round 33, person 33 draws the 33-rd element, and so on. That is, after person ii finishes drawing, it is person i+1i+1's turn to draw. In particular, after person nn finishes drawing, it becomes person 11's turn.

After the ii-th element of the sequence is drawn, the next element drawn will be the (i+1)(i+1)-th element. In particular, after the mm-th element is drawn, the next element drawn will be the 11-st element.

If in some round a person loses all of their money, the gambling game ends. Find in which round the game ends, or determine that the game will continue forever.

Input Format

The first line contains a positive integer nn, the number of players.

The second line contains nn positive integers a1,a2,,ana_1,a_2,\ldots,a_n, where aia_i is the initial amount of money held by player ii.

The third line contains a positive integer mm, the length of the sequence.

The fourth line contains a string of length mm consisting only of W and P, representing the sequence, where W means 11 and P means 1-1.

Output Format

If the game will continue forever, output -1. Otherwise, output the round in which the game ends.

4
2 3 2 1
3
WPP
12

Hint

For 100%100\% of the testdata, 1n1061 \le n \le 10^6, 1ai1061 \le a_i \le 10^6, 1m1061 \le m \le 10^6.

Translated by ChatGPT 5