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
/*
 *  Copyright (C) 2015  Paweł Widera
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details:
 *  http://www.gnu.org/licenses/gpl.html
 */
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int n, b;
	cin >> n;

	vector<int> coins;
	coins.reserve(n);

	for (int i = 0; i < n; ++i) {
		cin >> b;
		coins.push_back(b);
	}

	vector<int> start(coins);
	vector<int> change(n);
	vector<int> minimum(n, 1000000);

	int m;
	cin >> m;

	vector<int> cycle;
	cycle.reserve(m);

	char c;
	for (int i = 0; i < m; ++i) {
		cin >> c;
		if ('W' == c) {
			cycle.push_back(1);
		} else {
			cycle.push_back(-1);
		}
	}

	int player = 0;
	unsigned long long int i = 0;
	while (true) {
		coins[player] += cycle[i++ % m];
		if (coins[player] == 0) {
			cout << i << endl;
			break;
		}
		minimum[player] = min(minimum[player], coins[player]);
		player = (player + 1) % n;

		// new cycle starts and we are back to the first player
		if (i % m == 0 && player == 0) {
			for (int j = 0; j < n; ++j) {
				change[j] = coins[j] - start[j];
			}
			// stop if none of the players is loosing coins
			auto lowest = min_element(begin(change), end(change));
			if (*lowest >= 0) {
				cout << -1 << endl;
				break;
			}
			// find critical drop
			/*int critical = distance(begin(change), lowest);
			for (int j = 0; j < n; ++j) {
				if (change[j] == *lowest and minimum[j] < minimum[critical]) {
					critical = j;
				}
			}
			// accelerate the game
			int rounds = minimum[critical] / (-change[critical]);
			for (int j = 0; j < n; ++j) {
				coins[j] += rounds * change[j];
			}
			cout << "i " << i << " " << endl;
			cout << "rounds " << rounds << " " << endl;

			i += rounds * i;

			//for (auto m: coins) cout << m << " "; cout << endl;
			cout << "i " << i << " " << endl;*/
		}
	}

	return 0;
}