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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <utility>
#include <set>
using namespace std;

struct Node {
	long long sum;
	long long worstStreak;
};

long long money[1000*1000];
long long result[1000 * 1000];
long long n, m;
long long gcd, lcm, sequenceLength, treeLength;
Node tree[2<<21 + 1];
long long totalSum;
long long loseTime = -1;
long long totalStreak;
long long lastTreeElement;

int leftChild(int i) {
	return 2 * i;
}

int rightChild(int i) {
	return leftChild(i) + 1;
}

long long getChildSum(int elementId) {
	if (rightChild(elementId) > lastTreeElement) {
		return tree[leftChild(elementId)].sum;
	}
	return tree[leftChild(elementId)].sum + tree[rightChild(elementId)].sum;
}

long long getChildStreak(int elementId) {
	if (rightChild(elementId) > lastTreeElement) {
		return min(tree[leftChild(elementId)].sum, tree[leftChild(elementId)].worstStreak);
	}
	return min(tree[leftChild(elementId)].sum + tree[rightChild(elementId)].worstStreak, tree[leftChild(elementId)].worstStreak);
}

void fixupTree(int elementId) {
	do {
		elementId /= 2;
		tree[elementId].sum = getChildSum(elementId);
		tree[elementId].worstStreak = getChildStreak(elementId);
	} while (elementId != 1);
}

void calculateTree(int sequenceId) {
	int leaf = treeLength ;
	int elementId = sequenceId;
	lastTreeElement = leaf + sequenceLength - 1;
	for (int i = 0; i < sequenceLength; ++i) {
		tree[leaf].sum = result[elementId];
		tree[leaf].worstStreak = tree[leaf].sum == -1 ? -1 : 0;
		fixupTree(leaf);

		elementId = (elementId + n / gcd) % m;
		leaf++;
	}
	totalSum = tree[1].sum;
	totalStreak = tree[1].worstStreak;
}

long long calculateGcd(long long a, long long b) {
	if (a < b) {
		return calculateGcd(b, a);
	}

	while (b != 0) {
		long long temp = b;
		b = a % b;
		a = temp;
	}

	return a;
}

pair<long long, long long> getStreakForRangeFromTree(long long wantedLeft, long long wantedRight, int nodeId, long long currentLeft, long long currentRight) {
	long long leftSonRight = (currentRight + currentLeft) / 2;
	long long rightSonLeft = leftSonRight + 1;
	long long total = 0;

	pair<long long, long long> result;
	result.first = 0;
	result.second = 0;
	if (wantedLeft == currentLeft && wantedRight == currentRight) {
		result.first = tree[nodeId].sum;
		result.second = tree[nodeId].worstStreak;
		return result;
	}

	if (leftSonRight >= wantedLeft && rightSonLeft <= wantedRight) {
		pair<long long, long long> leftResult = getStreakForRangeFromTree(wantedLeft, min(wantedRight, leftSonRight), leftChild(nodeId), currentLeft, leftSonRight);
		pair<long long, long long> rightResult = getStreakForRangeFromTree(max(rightSonLeft, wantedLeft), wantedRight, rightChild(nodeId), rightSonLeft, currentRight);
		result.first = leftResult.first + rightResult.first;
		result.second = min(leftResult.first + rightResult.second, leftResult.second);
		return result;
	}
	else if (leftSonRight >= wantedLeft) {
		return getStreakForRangeFromTree(wantedLeft, min(wantedRight, leftSonRight), leftChild(nodeId), currentLeft, leftSonRight);
	}
	else {
		return getStreakForRangeFromTree(max(rightSonLeft, wantedLeft), wantedRight, rightChild(nodeId), rightSonLeft, currentRight);;
	}
}

long long getStreakForRange(long long left, long long right) {
	return getStreakForRangeFromTree(left, right, 1, 1, treeLength).second;
}

long long getStreakForMoment(long long moment, long long shift) {
	long long trueMoment = (moment + shift - 2) % sequenceLength + 1;
	if (trueMoment >= shift) {
		return getStreakForRange(shift, trueMoment);
	}

	return getStreakForRange(shift, sequenceLength) + getStreakForRange(1, trueMoment);
}

long long getLoseMoment(long long sum, long long left, long long right, long long shift) {

	while (left <= right) {
		long long middle = (right + left) / 2;
		long long streak = getStreakForMoment(middle, shift);
		if (streak <= sum) {
			right = middle - 1;
		}
		else {
			left = middle + 1;
		}
	}

	return right + 1;
}

long long getBoyShift(int boyId) {
	long long shift = (boyId / gcd) % (sequenceLength)+1;
	if (n < m) {
		shift = (sequenceLength - (shift - 1)) % sequenceLength + 1;
	}

	return shift;
}

long long getLoseMomentForSum(long long sum, int boyId) {
	long long shift = getBoyShift(boyId);
	long long moment = getLoseMoment(sum, 1, sequenceLength, shift);
	return (moment - 1) * n + 1;
}

long long getBoyStreak(int boyId) {
	long long shift = getBoyShift(boyId);
	if (shift == 1) {
		return totalStreak;
	}
	return getStreakForMoment(sequenceLength, shift);
}

long long getLoseTimeForBoy(int boyId) {
	long long boyStreak = getBoyStreak(boyId);
	if (totalSum >= 0 && (-boyStreak) < money[boyId]) {
		return -1;
	}

	long normalizedMoney = money[boyId] + boyStreak;
	long long cyclesCount = normalizedMoney / (-totalSum);
	long long borderMoney = money[boyId] + cyclesCount * totalSum;

	return getLoseMomentForSum(-borderMoney, boyId) + cyclesCount * lcm + boyId;
}

void handleEqualCase() {
	long long length = -1;
	for (int i = 0; i < n; ++i) {
		if (result[i] == -1) {
			long long boyLength = (money[i] - 1) * n + i + 1;
			if (length == -1) {
				length = boyLength;
			}
			else {
				length = min(boyLength, length);
			}
		}
	}

	printf("%lld", length);
}

int main() {
	scanf("%lld", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%lld", &money[i]);
	}
	scanf("%lld", &m);
	getchar();
	for (int i = 0; i < m; ++i) {
		char game = getchar();
		if (game == 'W') {
			result[i] = 1;
		}
		else {
			result[i] = -1;
		}
	}

	gcd = calculateGcd(n, m);
	lcm = (n / gcd) * (m);
	sequenceLength = m / gcd;
	treeLength = 1;
	while (treeLength <= sequenceLength) {
		treeLength *= 2;
	}

	if (n == m) {
		handleEqualCase();
		return 0;
	}

	for (int i = 0; i < gcd; ++i) {
		calculateTree(i);
		int boy = i;
		while (boy < n) {
			long long boyLoseTime = getLoseTimeForBoy(boy);
			if (loseTime == -1) {
				loseTime = boyLoseTime;
			}else if (boyLoseTime >= 0) {
				loseTime = min(loseTime, boyLoseTime);
			}

			boy += gcd;
		}
	}

	printf("%lld", loseTime);
	return 0;
}