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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <cstdlib> 
#include <algorithm>
#include <vector>
#include <queue>

#define endl "\n"
typedef long long ll;

const int MAKS = 1000 * 1000;
using namespace std;
int n, m;
int boys[MAKS][2];
bool cycle[MAKS];

int nwd(int a, int b){
	while(a%b){
		a %= b;
		swap(a,b);
	}
	return b;
}

void read(){
	cin >> n;
	for (int i = 0; i < n; i++){
		cin >> boys[i][1];
		boys[i][0] = boys[i][1];
	}
	cin >> m;
	char a;
	for (int i = 0; i < m; i++){
		cin >> a;
		if (a == 'W')
			cycle[i] = 1;
		else 
			cycle[i] = 0;
	}
}

void solve(){
	ll counter = 0LL;
	int idx = 0;
	int end = n / nwd(n,m);

	for (int j = 0; j < end; j++){
		for (int i = 0; i < m; i++){						
			counter++;
			int pom = (idx + i) % n;
			boys[pom][0] += -1 + 2*cycle[i];
			if (boys[pom][0] == 0){
				cout << counter << endl;				
				return;				
			}
		}		
		idx += m;
		idx %= n;
	}

	for(int j = 0; j < n; j++){
		if (boys[j][0] < boys[j][1]){
			while(true){
				for (int i = 0; i < m; i++){
					counter++;
					int pom = (idx + i) % n;
					boys[pom][0] += -1 + 2*cycle[i];
					if (boys[pom] == 0)
						cout << counter << endl;
						return;
				}				
				idx += m;
				idx %= n;		
			}
		}		
	}
	cout << -1 << endl;
}

int main() {
	// ios_base::sync_with_stdio(0);
	read();	
	solve();	
	return 0;
}