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

#define MAX 1000000

long long cash[MAX];
long long cash_start[MAX];
long long t[MAX];

int main()
{
	int n, m;

	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%lld", &cash[i]);
		cash_start[i] = cash[i];
	}

	scanf("%d\n", &m);
	for (int i = 0; i < m; i++) {
		char c = getchar();
		if (c == 'W') {
			t[i] = 1;
		} else {
			t[i] = -1;
		}
	}

	long long games = 0;
	bool end = false;
	int people_ptr = 0;
	int tab_ptr = 0;
	bool less = true;
	while (!end && less) {
		cash[people_ptr] += t[tab_ptr];
		games++;
		if (!cash[people_ptr]) {
			end = true;
		} else {
			people_ptr++;
			people_ptr %= n;
			tab_ptr++;
			tab_ptr %= m;
			if (!people_ptr && !tab_ptr) {
				less = false;
				for (int i = 0; i < n; i++) {
					if (cash[i] < cash_start[i]) {
						less = true;
						break;
					}
				}
			}
		}

	}

	if (end) {
		printf("%lld\n", games);
	} else {
		printf("-1\n");
	}
}