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
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iostream>
#include<string>
using namespace std;
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define LL long long
#define PII pair<int, int>
#define PLL pair<LL, LL>

int n, m, d, M=1;
LL res=2000000000000000000LL;
int T[1000005], A[1000005];
char C[1000005];
vector<int> V[1000005];
vector<int> D[1000005];

int mini(int p, int k, int ind)
{
	p+=M;
	k+=M;
	int r=min(D[ind][p], D[ind][k]);
	while(p/2!=k/2)
	{
		if(p%2==0)
			r=min(r, D[ind][p+1]);
		if(k%2==1)
			r=min(r, D[ind][k-1]);
		p/=2;
		k/=2;
	}
	return r;
}

int get(int v, int w, int ind)
{
	if(v>=M)
		return v-M;
	if(D[ind][2*v]<=w)
		return get(2*v, w, ind);
	return get(2*v+1, w, ind);
}

int getless(int v, int vp, int vk, int p, int k, int w, int ind)
{
	if(vp>k || vk<p || D[ind][v]>w)
		return -1;
	if(vp>=p && vk<=k)
		return get(v, w, ind);
	int mid=(vp+vk)/2;
	int x=getless(2*v, vp, mid, p, k, w, ind);
	if(x!=-1)
		return x;
	return getless(2*v+1, mid+1, vk, p, k, w, ind);
}

int main()
{
	//ios_base::sync_with_stdio(0);
	scanf("%d", &n);
	for(int i=0; i<n; i++)
		scanf("%d", T+i);
	scanf("%d%s", &m, C);
	d=__gcd(n, m);
	while(M<=2*(m/d))
		M*=2;
	//printf("%d %d\n", d, M);
	for(int i=0; i<d; i++)
	{
		V[i].PB(0);
		D[i].resize(2*M, 0);
	}
	for(int j=0; j<d; j++)
	{
		int ind=j;
		for(int i=0; i<m/d; i++)
		{
			A[ind]=i+1;
			//printf("%d %c\n", ind, C[ind]);
			if(C[ind]=='W')
				V[j].PB(V[j][i]+1);
			else
				V[j].PB(V[j][i]-1);
			ind+=n;
			ind%=m;
		}
	}
	for(int i=0; i<d; i++)
	{
		for(int j=1; j<=m/d; j++)
			D[i][j+M]=V[i][j];
		for(int j=1; j<=m/d; j++)
			D[i][j+m/d+M]=D[i][M+m/d]+V[i][j];
		for(int j=M-1; j>=1; j--)
			D[i][j]=min(D[i][2*j], D[i][2*j+1]);
	}
	//for(int i=1; i<=2*m/d; i++)
	//	printf("%d ", D[0][i+M]);
	//printf("\n");
	for(int i=0; i<n; i++)
	{
		int x=i%d, y=A[i%m]-1;//x-tree, y-pos
		//printf("%d %d %d\n", i, x, y);
		int sum=D[x][y+m/d+M]-D[x][y+M];
		int mi=mini(y+1, y+m/d, x)-D[x][y+M];
		//printf("lol %d %d\n", sum, mi);
		if(sum>=0 && T[i]+mi>0)
			continue;
		if(T[i]+mi<=0)
		{
			int ind=getless(1, 0, M-1, y+1, y+m/d, D[x][y+M]-T[i], x)-y-1;
			res=min(res, (LL)ind*n+i+1);
			continue;
		}
		int tmp=T[i]+mi;
		int tmp2=(tmp-sum-1)/abs(sum);
		tmp+=tmp2*sum-mi;
		int ind=getless(1, 0, M-1, y+1, y+m/d, D[x][y+M]-tmp, x)-y-1;
		res=min(res, (LL)ind*n+(LL)tmp2*(((LL)n*m)/d)+i+1);
	}
	if(res==2000000000000000000LL)
		printf("-1\n");
	else
		printf("%lld\n", res);
	return 0;
}