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
#include <algorithm>
#include <cstdio>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

typedef long long LL;

int n, m;
LL a[1000000], b[1000000];
char c[1000001];

int main() {
	scanf("%d", &n);
	REP(i,n) scanf("%lld", &a[i]);
	REP(i,n) b[i] = a[i];
	LL mi = a[0];
	scanf("%d\n", &m);
	gets(c);
	int i = 0, j = 0;
	LL r = 0;
	bool first = 1;
	for (;;) {
		++r;
		if (c[j] == 'W')
			++a[i];
		else if (!--a[i]) {
			printf("%lld\n", r);
			return 0;
		}
		if (first) mi = min(mi, a[i]);
		if (++i == n) i = 0;
		if (++j == m) j = 0;
		if (!i && !j && first && r == LL(n) * m) {
			first = 0;
			bool ok = 0;
			int s = b[0] - a[0];
			if (s <= 0) {
				printf("-1\n");
				return 0;
			}
			LL k = ((mi - 1) / s);
			r *= k + 1;
			REP(i,n) a[i] -= k * s;
		}
	}
}