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
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <deque>
#include <vector>

// #define TEST

static const unsigned int MAX_N = 1000 * 1000;
static const unsigned int MAX_M = 1000 * 1000;

static const long long VERY_MUCH = 9e18;

int n, m, cycleCount;
int savings[MAX_N];
char outcomes[MAX_M + 2];

static int gcd(int a, int b)
{
	if (a < b)
		std::swap(a, b);

	while (b != 0)
	{
		int tmp = b;
		b = a % b;
		a = tmp;
	}

	return a;
}

static void loadData()
{
	// Read savings per colleague
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
		scanf("%d", &savings[i]);

	// Read automaton behavior
	scanf("%d", &m);
	gets(outcomes); // Skip newline
	fread(outcomes, sizeof(char), m, stdin);
}

static long long solveCycle(int cycleOffset)
{
	const int cycleSize = m / cycleCount;
	const int guyCount = n / cycleCount;

#ifdef TEST
	printf("CYCLE %d\n", cycleOffset);
	printf("  outcomes: %d\n", cycleSize);
	printf("  guys: %d\n", guyCount);
#endif

	// Copy the cycle to simplify things
	static std::vector<char> outcomesCycle;
	outcomesCycle.clear();
	outcomesCycle.reserve(cycleSize);

	int pc = cycleOffset;
	do
	{
		outcomesCycle.push_back(outcomes[pc]);
		pc = (pc + n) % m;
	} while (pc != cycleOffset);

#ifdef TEST
	printf("  outcomesCycle: ");
	for (char c : outcomesCycle)
		putchar(c);
	putchar('\n');
#endif

	int cycleStart = cycleOffset;
#ifdef TEST
	for (int i = 0; i < cycleSize; i++)
	{
		printf("  Guys for %d:", i);
		for (int j = cycleStart; j < n; j += m)
			printf(" %d", savings[j]);
		putchar('\n');

		cycleStart = (cycleStart + n) % m;
	}
#endif

	// Calculate height and difference of cycle
	int height = 0;
	int minHeight = 0;
	for (char c : outcomesCycle)
	{
		if (c == 'W')
			height++;
		else
		{
			height--;
			// minHeight = std::min(minHeight, height);
		}
	}

	// Build stack
	static std::deque<int> stack;
	stack.clear();

	int timestamp = 0;
	stack.push_back(timestamp);

	for (auto it = outcomesCycle.rbegin(); it != outcomesCycle.rend(); it++)
	{
		timestamp--;

#ifdef TEST
		printf("  stack:");
		for (int s : stack)
			printf(" %d", s);
		putchar('\n');
#endif

		if (*it == 'W')
		{
			if (stack.size() > 1)
				stack.pop_back();
			stack.pop_back();
		}
		
		stack.push_back(timestamp);
	}

#ifdef TEST
	printf("  stack:");
	for (int s : stack)
		printf(" %d", s);
	putchar('\n');
#endif

	long long minTime = VERY_MUCH;

#ifdef TEST
	printf("  minHeight: %d\n", minHeight);
#endif

	int guyChecker = 0;
	int pos = outcomesCycle.size() - 1;

	// Repeat this procedure, but now handle colleagues
	cycleStart = cycleOffset;
	for (auto it = outcomesCycle.rbegin(); it != outcomesCycle.rend(); it++)
	{
		timestamp--;

#ifdef TEST
		printf("    shifted: ");
		for (int i = 0; i < outcomesCycle.size(); i++)
			putchar(outcomesCycle[(pos + i) % outcomesCycle.size()]);
		putchar('\n');
		pos--;
#endif

#ifdef TEST
		printf("    %c\n", *it);
#endif

		// Pop old values from front
		while (stack.front() - timestamp > cycleSize)
			stack.pop_front();

		if (*it == 'W')
		{
			if (stack.size() > 1)
				stack.pop_back();
			stack.pop_back();
		}
		stack.push_back(timestamp);

		minHeight = -stack.size() + 1;

#ifdef TEST
		printf("    stack:");
		for (int s : stack)
			printf(" %d", s);
		putchar('\n');

		printf("    height: %d\n", height);
		printf("    minHeight: %d\n", minHeight);
		printf("    stack size: %d\n", (int)stack.size());
#endif

		int negN = m - (n % m);
		cycleStart = (cycleStart + negN) % m;
		for (int j = cycleStart; j < n; j += m)
		{
			int cash = savings[j];
			int guyOffset = j;
			int roundsToBankruptcy = 0;

#ifdef TEST
			printf("    Guy: %d (%d)\n", j, cash);
#endif
			guyChecker++;

			// Calculate how many full rounds the guy will make before
			// he runs out of cash
			if (cash > -minHeight)
			{
				if (height < 0)
				{
					// He will eventually lose
					int distance = cash + minHeight;
					roundsToBankruptcy = ((distance - 1) / -height) + 1;
#ifdef TEST
					puts("POOTIS");
#endif
				}
				else
				{
#ifdef TEST
					puts("POOP");
#endif
					continue; // He never loses
				}
			}
			else
			{
#ifdef TEST
				puts("LEL");
#endif
				roundsToBankruptcy = 0; // He loses in this turn
			}

#ifdef TEST
			printf("      Bankrupt after %d rounds\n", roundsToBankruptcy);
			printf("      Cash: %d\n", cash);
#endif

			// Calculate how fast he will lose in the last round
			cash += roundsToBankruptcy * height;
#ifdef TEST
			printf("      Cash after rounds: %d\n", cash);
#endif
			assert(cash >= 0);
			assert(stack.size() - 1 >= cash);
			int lastRound = stack[stack.size() - 1 - cash] - timestamp;
#ifdef TEST
			printf("      Steps in last round: %d\n", lastRound);
#endif

			const long long stepsPerRound = 
				(long long)m * (long long)guyCount;

#ifdef TEST
			printf("      stepsPerRound: %lld\n", stepsPerRound);
			printf("      stepsPerStep: %d\n", guyCount);
#endif

			long long timeToBankruptcy = 
				(long long)roundsToBankruptcy * stepsPerRound +
				(long long)lastRound * (long long)n +
				(long long)guyOffset - (long long)n + 1;

#ifdef TEST
			printf("      LOSES AFTER: %lld\n", timeToBankruptcy);
#endif

			minTime = std::min(minTime, timeToBankruptcy);
		}
	}

#ifdef TEST
	printf("%d ?= %d\n", guyChecker, guyCount);
	assert(guyChecker == guyCount);
#endif

	return minTime;
}

void solve()
{
	cycleCount = gcd(n, m);

	long long minTime = VERY_MUCH;
	for (int i = 0; i < cycleCount; i++)
		minTime = std::min(minTime, solveCycle(i));

	if (minTime == VERY_MUCH)
		minTime = -1;

#ifdef TEST
	printf("cycle: %d\n", cycleCount);
	printf("s: %d\n", n / cycleCount);
	printf("s: %d\n", m / cycleCount);
#endif

	printf("%lld\n", minTime);
}

int main()
{
	loadData();
	solve();

	return 0;
}