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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
#define maxn 1000100
int n, hajs[maxn], m, t[maxn];
const int cst = 1e5;
int ile;
int main() {
	scanf("%d", &n);
	for (int i=1; i<=n; ++i) scanf("%d", &hajs[i]);
	scanf("%d", &m);
	for (int i=1; i<=m; ++i)
	{
		char zn;
		scanf(" %c", &zn);
		if (zn == 'W') t[i] = 1;
		else t[i] = -1;
	}
	ll WYN = INF;
	for (int i=1; i<=n; ++i) 
	{
		//printf("i: %d\n", i);
		ile = 0; int op = 0;
		int j = (i % m);
		if (j == 0) j = m;
		while (hajs[i] > 0 && op <= cst)
		{
			hajs[i] += t[j];
			if (hajs[i] <= 0) break;
			j = (j + n) % m;
			
			if (j == 0) j = m;
			ile += n; ++op;

		}
		if (op <= cst) WYN = min(WYN, (ll)ile + i);
	}
	if (WYN == INF) puts("-1");
	else printf("%lld", WYN);
}