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
85
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>

using namespace std;

vector<int> tab;

long long inf = 1000000000000000000 + 9;
long long ans = inf, ile;

int n, m, money[1000001], ciag[1000001];
long long cur, roz, sum, cp;
char znak; 
long long ilo;


long long nwd( long long n, long long m )
{
	if( n == 0 )return m;
	else return nwd( m%n, n );
}
long long nww( long long n, long long m )
{
	return ( n * m ) / nwd( n, m );
}
int main()
{
	ios_base::sync_with_stdio( 0 );
	cin>>n;
	for( int a = 1; a <= n; a++ )cin>>money[a];
	cin>>m;
	ilo = nww( n, m );
	for( int a = 1; a <= m; a++ )
	{
		cin>>znak;
		if( znak == 'W' )ciag[a] = 1;
		else ciag[a] = -1;
	}
	for( int a = 1; a <= n; a++ )
	{
//		cout<<a<<" ok"<<endl;
		roz = 0;
		cur = a%m;
		
		if( cur == 0 )cur = m;
		cp = cur;
		sum = 0;
		tab.push_back( cur );
		roz += ciag[ cur ];
		cur = ( cur + (n%m) )%m;
		if( cur == 0 )cur = m;
		while( cur != cp )
		{
//			cout<<"ok  "<<cur<<endl;
			tab.push_back( cur );
			roz += ciag[ cur ];
			cur = ( cur + (n%m) )%m;
			if( cur == 0 )cur = m;
		}
		for( int b = 0; b < tab.size(); b++ )
		{
			ile = inf;
			sum += ciag[ tab[b] ];
			if( roz < 0 )
			{
				if( sum < 0 )
				{
					if( ( money[a] + sum )%(-roz) == 0 )
					{
						ile = (b * n) + (( money[a] + sum )/( -roz ))*ilo + a;	
					}	
				}  
			}
			else if( money[a] + sum == 0 )ile = ( b * n ) + a;
//			cout<<a<<" "<<b<<" "<<ile<<endl;
			ans = min( ans, ile );
		}
		tab.clear();
	}
	if( ans != inf )cout<<ans;
	else cout<<"-1";
	return 0;
}