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
#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;

const int MAXN = 1000009;
int A[MAXN], V[MAXN];
char cycle[MAXN];

int gcd(int a, int b) {
	if (a == 0) return b;
	return gcd(b % a, a);
}

int balanse(char c) {
	if (c == 'W') return 1;
	return -1;
}

int ans(int n, int m) {
	int d = m * n / gcd(m, n);
	int i = 0, j = 0;
	int k = 0;

	while (k < d) {
		A[i] += balanse(cycle[j]);
		V[i] += balanse(cycle[j]);
		if (A[i] <= 0) return (k + 1);
		i = (i + 1) % n;
		j = (j + 1) % m;
		++k;
	}

	bool allNonN = true;
	for (int i = 0; i < n; i++) 
		allNonN = allNonN && (V[i] >= 0);

	if (allNonN) return -1;

	while (true) {
		A[i] += balanse(cycle[j]);
		if (A[i] <= 0) return (k + 1);
		i = (i + 1) % n;
		j = (j + 1) % m;
		++k;
	}

	return -1;
}

int main(int argc, char* argv[]) {
	int n, m;

	scanf("%d", &n);
	for (int i = 0; i < n; i++)
		scanf("%d", &A[i]);

	scanf("%d", &m);
	scanf("%s", cycle);

	printf("%d\n", ans(n, m));

	return 0;
}