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
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string>
#include<functional>
#include<ctype.h>
#include<map>
#define LL long long
#define LD long double
#define L(x) ((x).size())
#define FI first
#define SE second
#define MP make_pair
#define PB push_back
using namespace std;
int n,m,sM,act,sum,P = 2500000,needed,odj,mxmn,where;
char c[1001000];
LL wynik,now,mx;
int cash[1001000];
int tab[2001000];
bool vis[2001000];
vector<int> wyst[5001000];
vector<int> toClear;
bool tClear[5001000];
int kW[5001000];
LL bankrupt[1001000];
vector<int> cykl;
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;++i){
		scanf("%d",&cash[i]);
		bankrupt[i] = -1;
		}
	scanf("%d",&m);
	sM = m;
	scanf("%s",c);
	for(int i=0;i<m;++i){
		if(c[i]=='W')tab[i] = 1;
		else tab[i] = -1;
		}
	while(m<n){
		for(int i=0;i<sM;++i)
			tab[m+i] = tab[i];
		m+=sM;
		}

	for(int i=0;i<m;++i){
		act = i;
		for(int j=0;j<L(toClear);++j){
			wyst[toClear[j]].clear();
			kW[toClear[j]] = 0;
			tClear[toClear[j]] = false;
			}
		toClear.clear();
		if(!vis[act]){
			cykl.clear();
			mxmn = 1e9;
			sum = 0;
			while(!vis[act]){
				sum+= tab[act];
				if(sum<mxmn){
					mxmn = sum;
					where = L(cykl);
					}
				kW[sum+P] = 0;
				wyst[sum+P].PB(L(cykl));
				if(!tClear[sum+P]){
					toClear.PB(sum+P);
					tClear[sum+P] = true;
					}
				cykl.PB(act);
				vis[act] = true;
				act = (act+n)%m;
				}
			odj = 0;
			for(int j=0;j<L(cykl);++j){
				act = cykl[j];
				if(act<n){
					needed = odj-cash[act];
					needed+= P;
					if(L(wyst[needed])>kW[needed]){
						bankrupt[act] = wyst[needed][kW[needed]]-j+1;
						}
					else{
						if(sum<0){
							bankrupt[act] = (odj-mxmn-cash[act]);
							if((bankrupt[act]%sum)==0)bankrupt[act]/=sum;
							else bankrupt[act] = (bankrupt[act]/sum) + 1;
							cash[act]+= bankrupt[act]*sum;
							bankrupt[act]*=L(cykl);
							needed = odj-cash[act];
							needed+= P;
							if(L(wyst[needed])>kW[needed]){
								bankrupt[act]+= wyst[needed][kW[needed]]-j+1;
								}
							}
						}
					}
				odj+=tab[act];
				++kW[odj+P];
				if(!tClear[odj+P]){
					toClear.PB(odj+P);
					tClear[odj+P] = true;
					}
				if(sum+odj<mxmn){
					mxmn = sum+odj;
					where = L(cykl)+j;
					}
				if(L(wyst[sum+odj+P])==0)
					kW[sum+odj+P] = 0;
				wyst[sum+odj+P].PB(L(cykl)+j);
				if(!tClear[sum+odj+P]){
					toClear.PB(sum+odj+P);
					tClear[sum+odj+P] = true;
					}
				}
			}
		}
	wynik = 1e9;wynik*=wynik;
	mx = wynik;
	for(int i=0;i<n;++i){
		if(bankrupt[i]!=-1){
			now = (bankrupt[i]-1)*n;
			now+= i+1;
			if(now<wynik)wynik = now;
			}
		}
	if(wynik==mx)printf("-1\n");
	else printf("%lld\n",wynik);
	}