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

const int MAXN = 1000005;

int t[MAXN];
char w[MAXN];

int main()
{
	int n, m;
	scanf("%d", &n);
	for (int i=0; i<n; i++)
		scanf("%d", &t[i]);
	scanf("%d%*c", &m);
	scanf("%s", w);
	int it1=0, it2=0, time;
	for (time=1; 1; time++) {
		if (time==2*n*m) {
			printf("-1\n");
			return 0;
		}
		if (w[it2]=='W')
			t[it1]++;
		else
			t[it1]--;
		if (t[it1]==0)
			break;
		it1++;
		it2++;
		if (it1>=n)
			it1-=n;
		if (it2>=m)
			it2-=m;
	}
	printf("%d\n", time);
	return 0;
}
//ADG