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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class SectionStats{
public:
	int sum;
	int worst;
	int start;
	int end;
};

class Cycle{
public:
	vector<int> slots;
	vector<vector<SectionStats> > sectionStats;

	void calculateSectionStats(bool *winningSlot) {
		int acc = 1;
		int num = 1;
		sectionStats.push_back(vector<SectionStats>());
		sectionStats[0].reserve(1);
		while (acc < slots.size()){
			++num;
			acc *= 2;
			sectionStats.push_back(vector<SectionStats>());
			sectionStats[sectionStats.size() - 1].reserve(acc);
		}
		calculateSection(0, 0, slots.size(),winningSlot);
	}

	int exactLossPosition(int money,int lvl,int pos) {
		bool found = false;
		int res = 0;
		while (!found){
			while (lvl < sectionStats.size() && money + sectionStats[lvl][pos].worst <= 0){
				lvl++;
				pos *= 2;
			}
			if (lvl >= sectionStats.size()){
				res = sectionStats[lvl - 1][pos / 2].end-1;
				found = true;
			} else {
				money += sectionStats[lvl][pos].sum;
				++pos;
			}
		}
		return res;
	}

	//zwraca ilość gier, które przetrwa gracz w jednym cyklu
	int howManyGamesCanPlay(int money, int position) {
		//cout << "howManyGamesCanPlay(" << money << "," << position << ")\n";
		int pos = slotsPositions[position];
		int lvl = sectionStats.size() - 1;

		//od srodka do konca
		bool ended = false;
		while (!ended){
			//cout << " lvl " << lvl << " , pos " << pos << endl;
			if (pos == 0) ended = true;
			if (pos % 2 == 0){
				//jest pierwszy w swoim bloku, moze przejsc do rodzica od razu
				// nie moze byc na 0 poziomie tutaj
				pos /= 2;
				--lvl;
			} else {
				if (money + sectionStats[lvl][pos].worst <= 0){
					//przegral w tym zakresie
					return exactLossPosition(money, lvl, pos) - position;
				}
				money += sectionStats[lvl][pos].sum;
				//cout << " po money = " << money << endl;
				lvl--;
				pos /= 2;
				++pos;
			}
			if (pos >= sectionStats[lvl].size()) ended = true;
		}

		//od początku do środka
		if (position == 0) return slots.size();
		ended = false;
		pos = 0;
		lvl = 0;
		while (!ended){
			//cout << " lvl " << lvl << " , pos " << pos << endl;
			if (sectionStats[lvl][pos].end == position) ended = true;
			if (sectionStats[lvl][pos].end > position){
				//musimy zejść do poziomu niżej
				++lvl;
				pos *= 2;
			} else {
				if (money + sectionStats[lvl][pos].worst <= 0){
					//przegral w tym zakresie
					return exactLossPosition(money, lvl, pos) + slots.size() - position;
				}
				//cout << " po money = " << money << endl;
				//dodajemy wartość i przechodzimy w prawo
				money += sectionStats[lvl][pos].sum;
				++pos;
			}
		}
		return slots.size();
	}

	//zwraca najwieksza strate w ciagu calego cyklu
	int worstLoss(int position) {
		//cout << "worstLoss\n";
		int worst = 0;
		int sum = 0;
		int pos = slotsPositions[position];
		int lvl = sectionStats.size() - 1;

		//od srodka do konca
		bool ended = false;
		while (!ended){
			//cout << " lvl " << lvl << " , pos " << pos << endl;
			if (pos == 0) ended = true;
			if (pos % 2 == 0){
				//jest pierwszy w swoim bloku, moze przejsc do rodzica od razu
				// nie moze byc na 0 poziomie tutaj
				pos /= 2;
				--lvl;
			} else {
				worst = min(worst,sum+sectionStats[lvl][pos].worst);
				//cout << " po worst = " << worst <<  ", sum = " << sum << endl;
				sum += sectionStats[lvl][pos].sum;
				lvl--;
				pos /= 2;
				++pos;
			}
			if (pos >= sectionStats[lvl].size()) ended = true;
		}

		//od początku do środka
		if (position == 0) return worst;
		ended = false;
		pos = 0;
		lvl = 0;
		while (!ended){
			//cout << " lvl " << lvl << " , pos " << pos << endl;
			if (sectionStats[lvl][pos].end == position) ended = true;
			if (sectionStats[lvl][pos].end > position){
				//musimy zejść do poziomu niżej
				++lvl;
				pos *= 2;
			} else {
				//dodajemy wartości i przechodzimy w prawo
				worst = min(worst, sum + sectionStats[lvl][pos].worst);
				//cout << " po worst = " << worst << ", sum = " << sum << endl;
				sum += sectionStats[lvl][pos].sum;
				++pos;
			}
		}
		return worst;
	}

private:
	vector<int> slotsPositions;
	void calculateSection(int level, int start, int end, bool* winningSlot) {
		SectionStats stats;
		stats.sum = 0;
		stats.worst = 0;
		stats.start = start;
		stats.end = end;

		for (int i = start; i < end; ++i){
			if (winningSlot[slots[i]]){
				stats.sum++;
			} else {
				--stats.sum;
				if (stats.sum < stats.worst){
					stats.worst = stats.sum;
				}
			}
		}

		//cout << "wrzucam na level " << level << endl;
		sectionStats[level].push_back(stats);
		if (level < sectionStats.size() - 1){
			int mid = (start + end) / 2;
			calculateSection(level + 1, start, mid,winningSlot);
			calculateSection(level + 1, mid, end,winningSlot);
		} else if (end - start > 0){
			//ostatni poziom - wrzucamy wartosci do slotsPositions
			slotsPositions.push_back(sectionStats[level].size() - 1);
		}
	}
};

int main(){

	ios_base::sync_with_stdio(false);

	int playersC, slotsC;

	cin >> playersC;

	int* playersMoney = new int[playersC];

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

	cin >> slotsC;

	bool* winningSlot = new bool[slotsC];

	for (int i = 0; i < slotsC; ++i){
		char x;
		cin >> x;
		if (x == 'W'){
			winningSlot[i] = true;
		} else winningSlot[i] = false;
	}

	/* WYLICZAMY CYKLE */
	vector<Cycle> cycles;
	bool* visited = new bool[slotsC];
	for (int i = 0; i < slotsC; ++i) visited[i] = false;
	int* cycleMembership = new int[slotsC];
	int* positionInCycle = new int[slotsC];

	int nextSlot = 0;

	while (nextSlot < slotsC){

		int currentSlot = nextSlot%slotsC;
		Cycle cycle;
		do {
			visited[currentSlot] = true;
			cycleMembership[currentSlot] = cycles.size();
			positionInCycle[currentSlot] = cycle.slots.size();
			cycle.slots.push_back(currentSlot);
			currentSlot += playersC;
			currentSlot %= slotsC;
		} while (currentSlot != nextSlot);

		cycles.push_back(cycle);

		while (nextSlot < slotsC && visited[nextSlot]) ++nextSlot;

	}

	//cout << "Znaleziono " << cycles.size() << " cykli.\n";
	//for (int i = 0; i < cycles.size(); ++i){
		//cout << " Cykl o dlugosci " << cycles[i].slots.size() << " : ";
		//for (int j = 0; j < cycles[i].slots.size(); ++j){
			//cout << cycles[i].slots[j]+1 << " ";
		//}
		//cout << endl;
	//}

	//for (int i = 0; i < slotsC; ++i){
		//cout << "positionInCycle[" << i << "] = " << positionInCycle[i] << endl;
	//}

	/* ... */

	for (int i = 0; i < cycles.size(); ++i){
		//cout << " cykl " << i+1 << ":\n";
		cycles[i].calculateSectionStats(winningSlot);
		//for (int j = 0; j < cycles[i].sectionStats.size(); ++j){
			//cout << " lvl " << j+1 << " (size=" << cycles[i].sectionStats[j].size() << ") : ";
			//for (int k = 0; k < cycles[i].sectionStats[j].size(); ++k){
				//cout << "(" << cycles[i].sectionStats[j][k].sum << "," << cycles[i].sectionStats[j][k].worst << ") ";
			//}
			//cout << endl;
		//}
	}


	/* SPRAWDZAMY KAZDEGO GRACZA */

	unsigned long long int shortestPlay = 1000000000000000000;
	int shortestPlayer = 0;
	int shortestPlayerCycleLength = 0;
	for (int p = 0; p < playersC; ++p){
		int slot = p%slotsC;
		int cycle = cycleMembership[slot];
		//cout << "Gracz " << p << " nalezy do cyklu nr " << cycle + 1 << endl;
		if (cycles[cycle].sectionStats[0][0].sum < 0){
			//cykl jest ujemny
			//cout << " Ujemny cykl\n";
			int worstLoss = cycles[cycle].worstLoss(positionInCycle[slot]);
			//cout << " najgorsza strata : " << worstLoss << endl;
			int howManyCycles = -(playersMoney[p] + worstLoss - cycles[cycle].sectionStats[0][0].sum - 1) / cycles[cycle].sectionStats[0][0].sum;
			if (howManyCycles > 0){
				playersMoney[p] += howManyCycles*cycles[cycle].sectionStats[0][0].sum;
				int gamesPlayed = cycles[cycle].howManyGamesCanPlay(playersMoney[p], positionInCycle[slot]);
				//cout << " Gracz moze grac " << howManyCycles << " cykli i " << gamesPlayed << " gier.";
				//cout << " Czyli lacznie : " << howManyCycles*cycles[cycle].slots.size() + gamesPlayed << " gier.\n";
				if (shortestPlay > howManyCycles*cycles[cycle].slots.size() + gamesPlayed){
					shortestPlay = howManyCycles*cycles[cycle].slots.size() + gamesPlayed;
					shortestPlayer = p;
					shortestPlayerCycleLength = cycles[cycle].slots.size();
				}
			} else {
				int gamesPlayed = cycles[cycle].howManyGamesCanPlay(playersMoney[p], positionInCycle[slot]);
				//cout << " Gracz moze grac tylko " << gamesPlayed << endl;
				if (shortestPlay > gamesPlayed){
					shortestPlay = gamesPlayed;
					shortestPlayer = p;
					shortestPlayerCycleLength = cycles[cycle].slots.size();
				}
			}
		} else {
			//cout << "Cykl jest dodatni\n";
			if ((cycles[cycle].slots.size()-cycles[cycle].sectionStats[0][0].sum)/2 >= playersMoney[p]){
				//cout << "Ale jest szansa ze straci wszystko w jednym cyklu\n";
				//jest szansa ze straci wszystko w jednym cyklu
				//cout << "slot : " << slot << endl;
				int gamesPlayed = cycles[cycle].howManyGamesCanPlay(playersMoney[p], positionInCycle[slot]);
				if (gamesPlayed < cycles[cycle].slots.size()){
					//cout << " Gracz przegrywa po " << gamesPlayed + 1 << " rundach";
					if (gamesPlayed < shortestPlay){
						shortestPlay = gamesPlayed;
						shortestPlayer = p;
						shortestPlayerCycleLength = cycles[cycle].slots.size();
					}
				} else {
					//cout << "Gracz nigdy nie przegra\n";
				}
			} else {
				//cout << "Gracz nigdy nie przegra\n";
			}
		}
	}

	if (shortestPlay == 1000000000000000000){
		cout << "-1" << endl;
	} else {
		cout << shortestPlayer+shortestPlay*playersC+1 << endl;
	}

	return 0;

}