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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <iostream>
#include <vector>
#define int long long

using namespace std;

const int maxn = 1e6;

int n, m;
int start[maxn];
int cycle[maxn];
int patterns[maxn];
int change[maxn];
int move_p[maxn];

int pattern_c, pattern_l;
int pttrpos(int p, int i) {
	return (pattern_l ) * p + i;
}
void setpttr(int p, int i, int v) {
	patterns[pttrpos(p, i)] = v;
}

int getpttr(int p, int i) {
	return patterns[pttrpos(p, i)];
}

int gcd(int a, int b) {
	if (b > a) return gcd(b, a);
	if (b == 0) return a;
	return gcd(b, a % b);
}

vector<vector<int> > tree;
int treem;
int inf = 1e7;

void prep_tree() {
	tree.push_back(vector<int>(2*treem));
}

int span_min_in(int pt, int x, int p, int q, int s, int t) {
	if (p == s && q == t) return tree[pt][x];

	int a = inf, b = inf;
	int mid = (s+t) / 2;

	if (p <= mid) {
		a = span_min_in(pt, x*2, p, min(q, mid), s, mid);
	}

	if (q > mid) {
		b = span_min_in(pt, x*2+1, max(p, mid+1), q, mid+1, t);
	}

	return min(a, b);
}

int span_min(int pt, int p, int q) {
	span_min_in(pt, 1, p, q, 0, treem-1);
}

int leftmost_in(int pt, int x, int b, int p, int q, int s, int t) {
	// cout << "\tli x: " << x << ", b: " << b << ", p: " << p << ", q: " << q << ", s: " << s << ", t: " << t << endl;
	if (tree[pt][x] > b) return -1;
	if (s == t) {
		return x - treem;
	}

	int mid = (s+t) / 2;
	int r = -1;

	if (p <= mid) {
		r = leftmost_in(pt, x*2, b, p, min(mid, q), s, mid);
	}

	if (r != -1) return r;

	if (q > mid) {
		r = leftmost_in(pt, x*2+1, b, max(p, mid+1), q, mid+1, t);
	}

	return r;
}

int leftmost(int pt, int b, int p, int q) {
	return leftmost_in(pt, 1, b, p, q, 0, treem-1);
}

int pattern_abs(int pt, int i) {
	return tree[pt][treem+i];
}

int u_moment(int u, int i) {
	return i * n + u;
}

int forward(int pt, int s, int b, int v, int e) {
	// cout << "\tforward " << pt << ", " << s << ", " << b << ", " << v << ", " << e << ", " << endl;
	int tmp =  leftmost(pt, pattern_abs(pt, s) + v - b, s+1, e);
	// cout << "\tresult " << tmp - s << endl;
	if (tmp == -1) return tmp;
	return tmp - s;
}

int relative_zero(int pt, int i, int s) {
	return forward(pt, i, s, 0, i + pattern_l);
}

main() {
	ios_base::sync_with_stdio(0);

	char c;

	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> start[i];
	}

	cin >> m;
	for (int i = 0; i < m; i++) {
		cin >> c;
		cycle[i] = c == 'W' ? 1 : -1;
	}

	pattern_c = gcd(n, m);
	pattern_l = m / pattern_c;
	int offset = n % m;

	treem = 1;
	while(treem < pattern_l * 2) {
		treem = treem << 1;
	}

	for (int i = 0; i < pattern_c; i++) {
		prep_tree();
		setpttr(i, 0, i);
		for (int j = 1; j < pattern_l; j++) {
			setpttr(i, j, (getpttr(i, j-1) + offset) % m);
		}
	}

	int absv;
	for (int i = 0; i < pattern_c; i++) {
		absv = 0;
		for (int j = 0; j < pattern_l * 2; j++) {
			absv += cycle[getpttr(i, j % pattern_l)];
			tree[i][treem+j] = absv;
		}

		change[i] = absv / 2;

		for (int j = treem+pattern_l*2; j < 2*treem; j++) {
			tree[i][j] = inf;
		}

		for (int j = treem-1; j > 0; j--) {
			tree[i][j] = min(tree[i][j*2], tree[i][j*2+1]);
		}
	}

	for (int i = 0; i < m / pattern_c; i++) {
		int x = i * n % m;
		move_p[x] = i;
	}

	int uoffset, uoffsetr, pt, ures = -1, tmpr, worst, steps, diff, sm, beg, rest;
	for (int i = 0; i < n; i++) {
		pt     = i % pattern_c;
		if (pt == 0) {
			uoffset = move_p[i % m];
			uoffsetr = (pattern_l + uoffset - 1) % pattern_l;
		}

		tmpr = -1;

		if (change[pt] >= 0) {
			rest = relative_zero(pt, uoffsetr, start[i]);
			if (rest != -1) tmpr = i + n * rest;
		} else {
			sm    = span_min(pt, uoffset, uoffset + pattern_l - 1);
			beg   = pattern_abs(pt, uoffset) - cycle[getpttr(pt, uoffset)];
			worst = -(sm - beg);
			diff  = start[i] - worst;
			steps = (diff - change[pt] - 1) / (-change[pt]);
			rest  = relative_zero(pt, uoffsetr, start[i] + change[pt] * steps);
			tmpr  = n * (steps * pattern_l + rest - 1) + i;
		}

		if (tmpr != -1) {
			ures = ures == -1 ? tmpr : min(ures, tmpr);
		}
	}

	if (ures == -1) {
		cout << -1 << endl;
	} else {
		cout << ures + 1 << endl;
	}

	return 0;
}