1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

import static java.lang.Math.*;

public class haz {

	int[]A;
	boolean[]W;
	int n,m;
	Cyc[] C;
	int[] Cpos;
	long best = Long.MAX_VALUE;
	
	class Guy {
		long time;
		int money;
		int startIdx;
		
		public Guy(int idx, int m) {
			startIdx = idx;
			money = m;
			time = idx;
		}
		
		@Override
		public String toString() {
			return startIdx + "("+money+")";
		}
	}
	
	class Cyc {
		int size;
		int sum;
		int start;
		int[]pref;
		boolean[] B;
		boolean done = false;
		int maxDip=0;
		ArrayList<ArrayList<Guy>> guys = new ArrayList<>();
		public Cyc(int st) {
			int p = st;
			start = st;
			int idx =0;
			do {
				if(W[p])sum++;
				else sum--;
				C[p]=this;
				Cpos[p]=idx++;
				size++;
				p = (p+n)%m;
			} while(p != st);
			init();
		}
		
		void init() {
			for(int i=0;i<size;i++)guys.add(new ArrayList());
			pref = new int[size+1];
			B = new boolean[size];
			Arrays.fill(pref, -1);
			pref[0]=0;
			int sum =0;
			int p = start;
			for(int i=0;i<size;i++) {
				B[i]=W[p];
				if(W[p])sum++;
				else sum--;
				if(sum <0 && pref[-sum]==-1)
					pref[-sum] = i;
				maxDip = max(maxDip, -sum);
				p = (p+n)%m;
			}
		}
		
		public void calculate() {
			if (done)return;
			int[] levels = new int[2*size + 10];
			Arrays.fill(levels, -1);
			int current = sum;
			int zeroSum = sum ;
			for(int i = size-1;i>0;i--) {
				for (Guy g: guys.get(i)) {
					int need = g.money;
					if(B[i])need++;
					else need --;
					if(need==0) {
						best = min(best, g.time);
					} else {
						int needLvl = current - need;
						if(needLvl + size +2 >= 0 && levels[needLvl+size+2]!=-1) {
							long d = levels[needLvl+size+2] - i;
							long r = d* n + g.time;
							best = min(best, r);
						} else {
							long d = size -i;
							g.time += d * n;
							g.money += zeroSum - current +(B[i]?1:-1);
							if(g.money <=0) {
								System.out.println("WTF");
							}
							guys.get(0).add(g);
						}
					}
				}
				levels[current + size +2] = i;
				if(B[i])current = current -1;
				else current = current + 1;
			}
			for(Guy g:guys.get(0)) {
				long need = g.money;
				if (need <=maxDip) {
					long d = pref[(int)need];
					long r = g.time + d * n;
					best = min(best, r);
				} else if(sum < 0) {
					long rolls = (need - maxDip + (-sum -1)) / (-sum);
					long r = rolls * n * size;
					need += rolls * sum;
					long d = pref[(int)need];
					r += g.time + d * n;
					best = min(best, r);
				}
			}
			
			done = true;
		}
		
		public String toString() {
			return start +": "+sum+" ("+size+")";
		}
	}
	
	long solveBrut(){
		long best = Long.MAX_VALUE;
		for(int i=0;i<n;i++) {
			int cash = A[i];
			int pos = i % m;
			long t = i;
			while(cash > 0) {
				if(W[pos])cash++;
				else cash --;
				if(cash==0)break;
				pos = (pos + n) %m;
				t +=n;
				if(pos == i%m && cash >= A[i])break;
			}
			if(cash ==0)best = Math.min(best, t);
		}
		return best;
	}
	
	long solveNonBrut() {
		C = new Cyc[m];
		Cpos = new int[m];
		for(int i=0;i<m;i++){
			if(C[i]==null)new Cyc(i);
		}
		for(int i=0;i<n;i++) {
			int ci = i%m;
			Guy g = new Guy(i, A[i]);
			C[ci].guys.get(Cpos[ci]).add(g);
		}
		for(Cyc c:C)c.calculate();
		return best;
	}
	
	long solve(int[]A, char[]M, boolean brut){
		this.A = A;
		n = A.length;m=M.length;
		W = new boolean[m];
		for(int i=0;i<m;i++)W[i]=M[i]=='W';
		
		long res = brut?solveBrut():solveNonBrut();
		if(res<Long.MAX_VALUE)return res+1;
		return -1;
	}
	static void test() {
		for(int it =0;;it++) {
			Random rand = new Random(it);
			int n = rand.nextInt(500)+1;
			int m = rand.nextInt(500)+1;
			int[]A = new int[n];
			char[]M = new char[m];
			for(int i=0;i<n;i++){
				A[i]= 1 + rand.nextInt(500);
			}
			for(int i=0;i<m;i++){
				M[i] = rand.nextBoolean()?'W':'P';
			}
			long r1 = new haz().solve(A, M, true);
			long r2 = new haz().solve(A, M, false);
			if(r1 != r2){
				System.err.println("DIff "+it);
				throw new RuntimeException();
			} else {
				System.err.println("OK "+it);
			}
		}
	}
	public static void main(String[] args) throws Exception{

			long startTime = System.currentTimeMillis();
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
				//new BufferedReader(new FileReader("big"+t+".in"));
		int n = Integer.parseInt(br.readLine());
		String[]s = br.readLine().split(" ");
		int[] A = new int[n];
		for(int i=0;i<n;i++)A[i]=Integer.parseInt(s[i]);
		int m = Integer.parseInt(br.readLine());
		char[] M = br.readLine().toCharArray();
		long res = new haz().solve(A,M, false);
		System.out.println(res);
	//	System.err.println(System.currentTimeMillis()-startTime);
		//test();
	}
}