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
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include<cstdio>
#include<ctime>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<bitset>
using namespace std;
typedef long long ll;
typedef double db;
const db pi=acos(-1);
void gn(int &x){
    int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');
    if(c=='-')sg=-1,x=0;else x=c-'0';
    while((c=getchar())>='0'&&c<='9')x=x*10+c-'0';
    x*=sg;
}
void gn(ll &x){
    int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');
    if(c=='-')sg=-1,x=0;else x=c-'0';
    while((c=getchar())>='0'&&c<='9')x=x*10+c-'0';
    x*=sg;
}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
#define inf 6000000000000000000ll

int a[1111111],s[1111111];
int s2[2111111];
int mi[1111111];
int st[1111111];
int nea[1111111],nek[1111111],ann[1111111];
int ord[1111111];
int cmp(int i,int j){
	return nea[i]<nea[j];
}
int cmps(int i,int j){
	return s2[i]<s2[j];
}
int ords[2222222];

int qu[2222222],p,q;

struct node{int ch[2],sz,v,fix;}t[2222222];int ndtot=0;
int root=0;
void rot(int &x,int f){
	int y=t[x].ch[!f];
	t[x].ch[!f]=t[y].ch[f];
	t[y].ch[f]=x;
	t[y].sz=t[x].sz;
	t[x].sz=t[t[x].ch[0]].sz+t[t[x].ch[1]].sz+1;
	x=y;
}
void ins(int &x,int v){
	if(x==0){
		x=++ndtot;
		t[x].ch[0]=t[x].ch[1]=0;
		t[x].sz=1;
		t[x].fix=rand();
		t[x].v=v;
	}else{
		int f=v>=t[x].v;
		ins(t[x].ch[f],v);
		t[x].sz++;
		if(t[t[x].ch[f]].fix<t[x].fix)rot(x,!f);
	}
}
int suc(int v){
	int x=root,y=0;
	while(x){
		if(v<t[x].v){
			y=t[x].v;
			x=t[x].ch[0];
		}else x=t[x].ch[1];
	}
	return y;
}

ll work(int n,int m){
	for (int i=0;i<m;i++){
		int k=1ll*i*n%m;
		s2[i]=s[k];
		st[k]=i;
	}
	for (int i=0;i<m;i++)s2[i+m]=s2[i];
	for (int i=1;i<2*m-1;i++)s2[i]+=s2[i-1];
	for (int i=0;i<2*m-1;i++)ords[i]=i;	
	int totdel=s2[m-1];

	p=q=0;
	for (int i=0;i<2*m-1;i++){
		while(p!=q && i-qu[p]>=m)p++;
		while(p!=q && s2[i]<s2[qu[q-1]])q--;
		qu[q++]=i;
		if(i>=m-1)mi[i-m+1]=s2[qu[p]];
	}
	/*for (int i=0;i<m;i++)se.insert(s2[i]);mi[0]=*se.begin();
	for (int i=1;i<m;i++){
		se.erase(se.find(s2[i-1]));
		se.insert(s2[i+m-1]);
		mi[i]=*se.begin();
	}
	se.clear();*/
	for (int i=0;i<n;i++){
		int s=st[i%m];
		int mind=mi[s]-(s?s2[s-1]:0);
		if(a[i]+mind<=0){
			nea[i]=a[i];
			nek[i]=0;
		}else{
			if(totdel<0){
				int k=(a[i]+mind-1)/(-totdel)+1;
				nek[i]=k;
				nea[i]=a[i]+k*totdel;
			}else{
				nek[i]=-1;
			}
		}
		nea[i]=(s?s2[s-1]:0)-nea[i];
		ord[i]=i;
	}
	sort(ord,ord+n,cmp);
	sort(ords,ords+2*m-1,cmps);
	ndtot=root=0;
	int cur=0;
	for (int j=0;j<n;j++){
		int i=ord[j];
		if(nek[i]==-1)continue;
		while(cur<2*m-1 && s2[ords[cur]]<=nea[i])ins(root,ords[cur++]);
			//se.insert(ords[cur++]);
		//ann[i]=*se.lower_bound(st[i%m])-(st[i%m]);
		ann[i]=suc(st[i%m]-1)-(st[i%m]);
	}
	//se.clear();
	ll mi=inf;
	for (int i=0;i<n;i++)if(nek[i]!=-1)mi=min(mi,(1ll*nek[i]*m+ann[i])*n+i);
	return mi;
}

int n,m;
int tmpa[1111111];
char tmps[1111111];
int main()
{
	//freopen("big8.in","r",stdin);
	//int tl=clock();
	scanf("%d",&n);
	for (int i=0;i<n;i++)gn(tmpa[i]);
	scanf("%d",&m);
	scanf("%s",tmps);
	int r=gcd(n,m);
	ll mi=inf;
	for (int d=0;d<r;d++){
		for (int j=0;j*r+d<n;j++)a[j]=tmpa[j*r+d];
		for (int j=0;j*r+d<m;j++)s[j]=tmps[j*r+d]=='W'?1:-1;
		ll an=work(n/r,m/r);
		if(an!=inf){
			mi=min(mi,an*r+d);
		}
	}
	if(mi==inf)printf("-1\n");
	else cout<<mi+1<<endl;
	//printf("%d\n",clock()-tl);
	return 0;
}