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
//Piotr Golda
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

#define UD unsigned int
#define ULLD unsigned long long int
#define LLD long long int
#define LIMIT 1000000
#define FLAG 2147483647

class SkipArray;

UD N, M;
UD Cash[LIMIT];
vector< SkipArray* > SubCycles;
vector< vector<int> > SubCyclesSums;
UD PosInSCycle[LIMIT][2];	//[0] sub-cycle number, [1] position in sub-cycle
char MainCycle[LIMIT];
bool Used[LIMIT];



class SkipArray
{

private:
	vector<int> m_sums;
	vector<UD>* m_pointers;
	UD* m_previous;
	UD* m_greaterCount;
	int m_maxVal;

public:
	SkipArray(vector<int>& p_args)
	{
		m_sums.push_back(p_args[0]);

		m_maxVal = p_args[0];
		for (UD i = 1; i < p_args.size(); i++)
		{
			m_sums.push_back( m_sums[i - 1] + p_args[i] );
			if (m_sums[i] > m_maxVal)
				m_maxVal = m_sums[i];
		}

		m_sums.push_back(FLAG);
		m_sums.push_back(FLAG);
		m_previous = new UD[m_sums.size()];
		m_pointers = new vector<UD>[m_sums.size()];
		m_greaterCount = new UD[m_sums.size()];

		m_previous[0] = FLAG;
		m_previous[m_sums.size()-1] = m_sums.size()-2;
		m_pointers[m_sums.size() - 2].push_back(m_sums.size() - 1);
		m_greaterCount[m_sums.size() - 1] = 0;
		m_greaterCount[m_sums.size() - 2] = 1;

		int powTwo = 2;
		int limit = 2;
		int ind;

		//setting pointers to next, 2nd, 4th, 8th bigger number;
		for (int i = m_sums.size() - 3; i >= 0; --i)
		{
			ind = this->upper_bound(m_sums[i], i + 1); 
			m_previous[ind] = i;
			m_pointers[i].push_back(ind);
			m_greaterCount[i] = m_greaterCount[ind] + 1;
			powTwo = 1;
			for (UD j = 1; j < m_pointers[ind].size(); ++j)
			{
				powTwo *= 2;
				m_pointers[i].push_back(m_previous[m_pointers[ind][j]]);
			}
			if (powTwo*2 <= m_greaterCount[i])
				m_pointers[i].push_back(m_sums.size() - 1);
		}


	}
	~SkipArray()
	{
		if (m_pointers)
			delete[] m_pointers;
		if (m_previous)
			delete[] m_previous;
		if (m_greaterCount)
			delete[] m_greaterCount;
	}

	UD upper_bound(int p_value, UD p_start)
	{
		if (m_sums[p_start] >= p_value)
			return p_start;
		UD pos = p_start;
		UD finish;
		UD f_ind;

		f_ind = m_pointers[pos].size() - 1;
		finish = m_pointers[pos][f_ind];

		while (m_sums[finish] < p_value)
		{
			pos = finish;
			f_ind = m_pointers[pos].size() - 1;
			finish = m_pointers[pos][f_ind];
		}

		while (f_ind > 0)
		{
			--f_ind;
			if (m_sums[ m_pointers[pos][f_ind] ] < p_value)
			{
				pos = m_pointers[pos][f_ind];
				f_ind = m_pointers[pos].size() - 1;
			}
			else
			{
				finish = m_pointers[pos][f_ind];
			}
		}
		return finish;
	}

	int maxValue()
	{
		return
			m_maxVal;
	}
};



void scan()
{
	cin >> N;
	string s;
	for (UD i = 0; i < N; ++i)
	{
		cin >> Cash[i];
	}
	cin >> M;
	cin >> s;
	for (UD i = 0; i < M; ++i)
	{
		if (s[i] == 'W')
			MainCycle[i] = 1;
		else
			MainCycle[i] = -1;
		Used[i] = false;
	}
}



void initialProcess()
{
	UD k;
	int sum;
	for (UD i = 0; i < M; ++i)
	{
		if (!Used[i])
		{
			vector<int> V;
			vector<int> vals;
			sum = 0;
			k = i;
			do
			{
				Used[k] = true;
				if (V.size() > 0)
				{
					V.push_back(V[V.size() - 1] - MainCycle[k]);
				}
				else
					V.push_back(-MainCycle[k]);
				vals.push_back(-MainCycle[k]);
				PosInSCycle[k][0] = SubCyclesSums.size();
				PosInSCycle[k][1] = V.size() - 1;

				k = (k + N) % M;
			} while (k != i);
			SkipArray* sa = new SkipArray(vals);
			SubCycles.push_back(sa);
			SubCyclesSums.push_back(V);

			//SubCycles.push_back(V);
			//sum = max(0, -sum);
			//MaxCycleLoss.push_back((UD)sum);
		}
	}
}

LLD rounds(UD p_player)
{
	int cash = Cash[p_player];
	UD cycle = PosInSCycle[p_player%M][0];
	UD cyclePos = PosInSCycle[p_player%M][1];

	int prevSum = 0;
	if (cyclePos > 0)
		prevSum = SubCyclesSums[cycle][cyclePos - 1];
	UD posUpBnd = SubCycles[cycle]->upper_bound(cash + prevSum, cyclePos);

	UD finishCyclePos;
	LLD ans = 0;
	if (posUpBnd >= SubCyclesSums[cycle].size()) // starts with money on beginning of cycle
	{

		LLD cyclesNumber = (LLD)SubCycles.size();
		LLD elementsInCycle = (LLD)SubCyclesSums[cycle].size();
		LLD cycleSum = (LLD)(SubCyclesSums[cycle][SubCyclesSums[cycle].size() - 1]);
		LLD maxCycleLoss = (LLD)(SubCycles[cycle]->maxValue());

		ans += (LLD)p_player + (LLD)N*(LLD)(elementsInCycle - cyclePos); // moves to end of cycle
		cash -= (SubCyclesSums[cycle][elementsInCycle - 1] - prevSum);
		if (cash > 0 && cycleSum > 0)
		{
			//LLD times = (LLD)cash / cycleSum;
			LLD times = ((LLD)cash - maxCycleLoss + cycleSum -1) / cycleSum;
			if (times > 0&& cash > maxCycleLoss)
			{
				//while (times > 0 && (LLD)cash - (times*cycleSum) < maxCycleLoss)
				//	times-=(maxCycleLoss/cycleSum);
				ans += times* elementsInCycle * (LLD)N; // moves through whole cycle max times
				cash = cash - times*cycleSum;
			}
		}
		while (cash > 0)
		{
			finishCyclePos = SubCycles[cycle]->upper_bound(cash, 0); // finds final position
			if (finishCyclePos >= SubCyclesSums[cycle].size())
			{
				if (cycleSum <= 0)
					return FLAG;
				ans += (LLD)(finishCyclePos)*(LLD)N;
				cash = cash - (LLD)(SubCyclesSums[cycle][finishCyclePos-1]);
				continue;
			}
			ans += (LLD)(finishCyclePos)*(LLD)N;
			cash = cash - (LLD)(SubCyclesSums[cycle][finishCyclePos]);
		}
	}
	else
	{
		finishCyclePos = posUpBnd;
		ans = (LLD)p_player + (LLD)(N)*(LLD)(finishCyclePos - cyclePos);
	}
	ans++;
	return ans;
}

LLD mainLoop()
{
	LLD min = rounds(0);
	LLD rnds;
	for (UD i = 1; i < N; ++i)
	{
		rnds = rounds(i);
		if (rnds < min)
			min = rnds;
	}
	if (min == FLAG)
		return -1;
	return min;
}


int main()
{
	ios_base::sync_with_stdio(0);
	scan();
	initialProcess();
	LLD ans;
	ans = mainLoop();
	cout << ans << endl;


	srand(time(0));
	//int T[] = { -1, 3, -1, -6, 8, 1, -5, 6 };
	//vector<int> v(T, T + sizeof(T) / sizeof(int));
	//SkipArray sa( v );
	//checkSA();

	return 0;
}

/*
4
2 3 2 1
3
WPP

4
1 3 2 1
10
WPPWPWPPWW

*/