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
// Po 1 punkt ;-)

#include <bits/stdc++.h>
using namespace std;

char G[1000001];
long long a, b, m, n, O[1000000];

int main() {
	scanf("%lld", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%lld", O + i);
	}
	scanf("%lld %s", &m, G);
	a = 1;
	for (int i = 0; i < m && a; ++i) {
		a = G[i] == 'W';
	}
	if (a) {
		puts("-1");
		return 0;
	}
	if (n % m == 0) {
		a = -1;
		for (int i = 0, j = 0; i < n; ++i) {
			if (G[j] == 'P') {
				b = (O[i] - 1) * n + i + 1;
				if (a == -1 || b < a) {
					a = b;
				}
			}
			++j;
			if (j >= m) {
				j = 0;
			}
		}
		printf("%lld\n", a);
		return 0;
	}
	a = b = 0;
	for (int i = 1; i <= 100000000; ++i) {
		if (G[b] == 'W') {
			++O[a];
		} else {
			--O[a];
		}
		if (O[a] <= 0) {
			printf("%d\n", i);
			return 0;
		}
		++a;
		if (a >= n) {
			a = 0;
		}
		++b;
		if (b >= m) {
			b = 0;
		}
	}
	puts("-1");
	return 0;
}