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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Krzysztof Małysa
#include <bits/stdc++.h>
#include <unistd.h>

using namespace std;

#define FOR(i,a,n) for (int i = (a), __n ## i = n; i < __n ## i; ++i)
#define REP(i,n) FOR(i,0,n)
#define FORD(i,a,n) for (int i = (a), __n ## i = n; i >= __n ## i; --i)
#define LET(x,a) __typeof(a) x = (a)
#define FOREACH(i,x) for (LET(i, x.begin()), __n##i = x.end(); i != __n##i; ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int(x.size()))
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define ST first
#define ND second
#define MP make_pair
#define PB push_back
#define O(...) ostream& operator <<(ostream& os, const __VA_ARGS__& x)
#define OO(...) O(__VA_ARGS__) { return __out(os, ALL(x)); }
#define T template
#define CL class

typedef unsigned uint;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef vector<VPII> VVPII;
typedef pair<LL, LL> PLLLL;
typedef vector<PLLLL> VPLLLL;
typedef vector<bool> VB;
typedef vector<char> VC;
typedef vector<string> VS;

T<CL A>
inline A abs(const A& a) { return a < A() ? -a : a; }

T<CL A, CL B>
inline void mini(A& a, const B& b) {
	if (b < a)
		a = b;
}

T<CL A, CL B>
inline void maxi(A& a, const B& b) {
	if (b > a)
		a = b;
}

T<CL Iter>
ostream& __out(ostream& os, Iter a, Iter b, const string& s = ", ");

T<CL A, CL B>
O(pair<A, B>);

T<CL A>
OO(vector<A>)

T<CL A>
OO(deque<A>)

T<CL A>
OO(list<A>)

T<CL A, CL B>
OO(set<A, B>)

T<CL A, CL B, CL C>
OO(map<A, B, C>)

T<CL A, CL B>
OO(multiset<A, B>)

T<CL A, CL B, CL C>
OO(multimap<A, B, C>)

T<CL Iter>
ostream& __out(ostream& os, Iter a, Iter b, const string& s) {
	os << "{";
	if (a != b) {
		os << *a;
		while (++a != b)
			os << s << *a;
	}
	return os << "}";
}

T<CL A, CL B>
O(pair<A, B>) {
	return os << "(" << x.ST << ", " << x.ND << ")";
}

CL Input {
	static const int BUFF_SIZE = 1 << 16;
	unsigned char buff[BUFF_SIZE], *pos, *end;

	void grabBuffer() {
		pos = buff;
		end = buff + read(0, buff, BUFF_SIZE);
	}

public:
	Input() : pos(buff), end(buff) {}

	int peek() {
		if (pos == end)
			grabBuffer();
		return pos != end ? *pos : -1;
	}

	int getChar() {
		if (pos == end)
			grabBuffer();
		return pos != end ? *pos++ : -1;
	}

	void skipWhiteSpaces() {
		while (isspace(peek()))
			++pos;
	}

	T<CL A>
	A get();

	void operator()(char* s) {
		skipWhiteSpaces();
		while (!isspace(peek()))
			*s++ = *pos++;
		*s = '\0';
	}

	T<CL A>
	void operator()(A& x) { x = get<A>(); }

	T<CL A, CL B>
	void operator()(A& a, B& b) { operator()(a), operator()(b); }

	T<CL A, CL B, CL C>
	void operator()(A& a, B& b, C& c) { operator()(a, b), operator()(c); }

	T<CL A, CL B, CL C, CL D>
	void operator()(A& a, B& b, C& c, D& d) {
		operator()(a, b, c);
		operator()(d);
	}

	T<CL A, CL B, CL C, CL D, CL E>
	void operator()(A& a, B& b, C& c, D& d, E& e) {
		operator()(a, b, c, d);
		operator()(e);
	}

	T<CL A, CL B, CL C, CL D, CL E, CL F>
	void operator()(A& a, B& b, C& c, D& d, E& e, F& f) {
		operator()(a, b, c, d, e);
		operator()(f);
	}

	Input& operator>>(char *s) {
		operator()(s);
		return *this;
	}

	T<CL A>
	Input& operator>>(A& a) {
		operator()(a);
		return *this;
	}
} fin;

T<> char Input::get<char>() {
	skipWhiteSpaces();
	return *pos++;
}

T<> uint Input::get<uint>() {
	skipWhiteSpaces();
	uint x = 0;
	while (isdigit(peek()))
		x = x * 10 + *pos++ - '0';
	return x;
}

T<> int Input::get<int>() {
	skipWhiteSpaces();
	return peek() == '-' ? (++pos, -get<uint>()) : get<uint>();
}

T<> ULL Input::get<ULL>() {
	skipWhiteSpaces();
	ULL x = 0;
	while (isdigit(peek()))
		x = x * 10 + *pos++ - '0';
	return x;
}

T<> LL Input::get<LL>() {
	skipWhiteSpaces();
	return peek() == '-' ? (++pos, -get<ULL>()) : get<ULL>();
}

T<> string Input::get<string>() {
	skipWhiteSpaces();
	string x;
	while (!isspace(peek()))
		x += *pos++;
	return x;
}

#undef T
#undef CL
#ifdef DEBUG
# define D(...) __VA_ARGS__
# define E(...) eprintf(__VA_ARGS__)
# define OUT(a,b) cerr << #a ":", __out(cerr, a, b), E("\n")
# define LOG(x) cerr << #x ": " << (x)
# define LOG2(x, y) cerr << x ": " << (y)
# define LOGN(x) LOG(x) << endl
# define LOGN2(x, y) LOG2(x, y) << endl
#else
# define D(...)
# define E(...)
# define OUT(...)
# define LOG(...)
# define LOG2(...)
# define LOGN(...)
# define LOGN2(...)
#endif
/// End of templates

int ceil2(int x) { return 1 << (sizeof(x) * 8 -__builtin_clz(x - 1)); }

void mono_push(deque<PII>& Q, int x, int pos) {
	while (SZ(Q) && Q.back().ND > x)
		Q.pop_back();
	Q.PB(MP(pos, x));
}

void mono_pop(deque<PII>& Q, int pos) {
	while (SZ(Q) && Q.front().ST <= pos)
		Q.pop_front();
}

int ceil_from_division(int a, int b) {
	return (a + b - 1) / b;
}

int main() {
	// Input
	int n, m;
	fin >> n;
	VI a(n);
	REP (i, n)
		fin >> a[i];
	fin >> m;
	char s[n + 1];
	fin >> s;
	LOGN(s);

	// Find cycles
	VVI cycles;
	VB vis(m);
	VI sum, which_cycle(m);
	REP (i, m)
		s[i] = (s[i] == 'P' ? -1 : 1);
	REP (i, m) {
		if (vis[i])
			continue;
		cycles.PB(VI(1, i));
		sum.PB(s[i]);
		vis[i] = true;
		which_cycle[i] = SZ(cycles) - 1;
		for (int x = (i + n) % m; x != i; x = (x + n) % m) {
			assert(!vis[x]);
			vis[x] = true;
			which_cycle[x] = SZ(cycles) - 1;
			cycles.back().PB(x);
			sum.back() += s[x];
		}
	}
	D(FOREACH (i, cycles)
		LOGN(*i));
	LOGN(sum);

	VI mn(m);
	// Calculate minimum sum for each node of a cycle
	FOREACH (alpha, cycles) {
		VI &cycle = *alpha;
		deque<PII> Q; // (pos, value)
		int len = SZ(cycle), sm = 0, sm_pref = 0;
		FOR (i, 0, len - 1) {
			sm += s[cycle[i]];
			mono_push(Q, sm, i);
		}
		LOGN(sm);
		LOGN(Q);
		REP (i, len) {
			mini(mn[cycle[i]], Q.front().ND - sm_pref);

			mono_pop(Q, i);
			sm_pref += s[cycle[i]];
			// sm -= s[cycle[i]];
			sm += s[cycle[(i + len - 1) % len]];
			mono_push(Q, sm, i + len - 1);
		}
	}
	LOGN(mn);

	// For each person compute integer # of laps he can do
	VVPII Q(m); // Q[i] (i < m) - list of sums to get result of pair(sum, id)
	VI res(n, -1);
	REP (i, n) {
		int which = which_cycle[i % m];
		if (mn[i % m] + a[i] <= 0) {
			res[i] = i + 1;
			Q[i % m].PB(MP(a[i], i));

		} else if (sum[which] < 0) {
			res[i] = ceil_from_division(a[i] + mn[i % m], abs(sum[which]));
			assert(res[i] > 0);
			Q[i % m].PB(MP(a[i] + res[i] * sum[which], i));
			res[i] = res[i] * n * SZ(cycles[which]) - n + 1 + i;
		}
	}
	LOGN(res);
	LOGN(Q);

	// Add non integer lap parts to results
	REP (k, SZ(cycles)) {
		E("===============================================\n");
		VI &cycle = cycles[k];

		int len = SZ(cycle), len2 = len << 1, sm = 0;
		LOGN(len);

		VVI t(len << 2);
		REP (i, len << 1) {
			sm += s[cycle[i % len]];
			t[len2 + sm].PB(i);
		}

		FOREACH (i, t)
			reverse(ALL(*i));

		D(REP (i, len << 2)
			if (t[i].size()) {
				E("%i", i);
				LOGN2("", t[i]);
			})
		E("++++++++++++++\n");

		sm = 0;
		REP (i, len << 1) {
			FOREACH (j, Q[cycle[i]])
				if (j->ST > 0) {
					LOGN(i);
					LOGN(sm);
					LOGN(*j);
					LOGN(t[len2 + sm - j->ST].back());
					LOGN(t[len2 + sm - j->ST].back() - i + 1);
					res[j->ND] += ((t[len2 + sm - j->ST].back() - i + 1)) * n;
				}
			sm += s[cycle[i]];
			t[len2 + sm].pop_back();
		}
	}
	LOGN(res);
	int rs = 1 << 30;
	REP (i, n)
		if (res[i] > -1)
			mini(rs, res[i]);

	printf("%i\n", rs);
	return 0;
}
/*
4
2 3 2 1
3
WPP

4
1 1 1 1
6
WPPPWP

*/