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
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=(a);i<=(b);++i)
#define FORI(i,n) REP(i,1,n)
#define FOR(i,n) REP(i,0,int(n)-1)
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define ll long long
#define SZ(x) int((x).size())
#define DBG(v) cerr << #v << " = " << (v) << endl;
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define SORT(X) sort(X.begin(),X.end())
#define fi first
#define se second

const int MAXN = 1000100;

int O[MAXN];
char T[MAXN];
int K[MAXN];
int P[MAXN];


long long ans = 1000000000000000000LL;

int f(int x){
	if(T[x] == 'W') return 1;
	return -1;
}

int n,m;

vector<int> V;
vector<int> Naj;

const int SR = 10001000;
vector<int> GP[SR*2];
int GPw[SR*2];

deque<pair<int,int> > Q;

void spraw(int xx){
	if(P[xx] == -1) return;
	V.pb(SR);
	Naj.pb(0);
	int nn = SR*2, nw = 0;

	while(O[xx] != 2){
		O[xx]++;
		Naj.pb(P[xx]);
		V.pb(V.back()+f(xx));
		GP[V.back()].pb(SZ(V));
		nn = min(nn,V.back());
		nw = max(nw,V.back());
		xx = (xx+n)%m;
	}
	int diff = (V.back()-SR)/2;
	int dc = SZ(V)/2;
	//for(auto c : V) printf("%d ", c); printf("\n");
	
	Q.clear();
	FORI(i,dc-1){
		auto x = mp(V[i],i);
		while(!Q.empty() && Q.back().fi >= x.fi){
			Q.pop_back();
		}
		Q.push_back(x);
	}
	
	FORI(i,dc){
		auto x = mp(V[i+dc-1],i+dc-1);
		while(!Q.empty() && Q.back().fi >= x.fi){
			Q.pop_back();
		}
		Q.push_back(x);
		while(Q.front().se < i) Q.pop_front();
		int low = Q.front().fi-V[i-1];
		//printf("LOW: %d | %d %d\n", Q.front().fi, Q.size(), V[i+dc]);
		if(Naj[i] == -1) continue;
		if(diff >= 0 && K[Naj[i]]+low > 0) continue;
		if(K[Naj[i]]+low <= 0){
			while(GP[V[i-1]-K[Naj[i]]][GPw[V[i-1]-K[Naj[i]]]] < i){
				GPw[V[i-1]-K[Naj[i]]]++;
			}
			//printf("%d %d\n", GP[V[i-1]-K[Naj[i]]][GPw[V[i-1]-K[Naj[i]]]],i);
			//printf("%d {{ %lld\n", K[Naj[i]], Naj[i] + n*((long long)GP[V[i-1]-K[Naj[i]]][GPw[V[i-1]-K[Naj[i]]]] - i -1) +1);
			ans = min(ans, Naj[i] + n*((long long)GP[V[i-1]-K[Naj[i]]][GPw[V[i-1]-K[Naj[i]]]] -1 - i) +1);
			continue;
		}
		long long prop = Naj[i];
		int mam = K[Naj[i]];
		//printf("----------------------\n");
		//printf("%d %d %d\n", mam, diff, low);
		int LC = (mam+low-diff-1)/(-diff);
		prop += (long long)LC*dc*n;
		mam += diff*LC;
		/*
		while(mam + low > 0){
			//printf("+");
			prop += dc*n;
			mam += diff;
		}*/
		//printf("\n");
		while(GP[V[i-1]-mam][GPw[V[i-1]-mam]] < i){
			GPw[V[i-1]-mam]++;
		}
		//printf("%lld %d i=%d\n", prop, GP[V[i-1]-mam][GPw[V[i-1]-mam]], i);
		prop += n*((long long)GP[V[i-1]-mam][GPw[V[i-1]-mam]] - i -1) +1;
		//printf("%d {{ prop : %lld\n", K[Naj[i]],prop);
		ans = min(ans, prop);
	}
	
	for(int i = nn; i<=nw; i++){
		GP[i].clear();
		GPw[i] = 0;
	}
}

int main () {
	scanf("%d",&n);
	FOR(i,n) scanf("%d",K+i);
	scanf("%d",&m);
	scanf("%s",T);
	FOR(i,m) P[i] = -1;
	FOR(i,n){
		if(P[i%m] == -1 || K[P[i%m]] > K[i]) P[i%m] = i;
	}
	//FOR(i,m)
		//printf("%d ", P[i]); printf("\n");
	FOR(i,m){
		if(O[i] == 0){
			spraw(i);
		}
	}
	if(ans == 1000000000000000000LL){
		printf("-1\n");
	}
	else{
		printf("%lld\n", ans);
	}

}