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
//package pa2015;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class haz {
	private static class Person implements Comparable<Person> {
		public Person(int id, int nval) {
			this.id = id;
			money = nval;
			oldMoney = money;
			start = id;
		}

		final long id;

		long money;

		long oldMoney;

		long start;

		@Override
		public int compareTo(Person p) {
			if (money > p.money)
				return 1;
			if (money < p.money)
				return -1;

			if (id < p.id)
				return -1;
			return 1;
		}
	}

	private static long result() {
		long res = 0;
		int n = 1;
		int m = 1;
		Person[] people = null;
		int[] profitLost = null;

		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
		try {
			n = Integer.parseInt(bufferedReader.readLine());
			people = new Person[n];
			String[] playersStr = bufferedReader.readLine().split(" ");
			for (int i = 0; i < n; i++) {
				people[i] = new Person(i, Integer.parseInt(playersStr[i]));
			}

			m = Integer.parseInt(bufferedReader.readLine());
			profitLost = new int[m];

			for (int i = 0; i < m; i++) {
				if (bufferedReader.read() == 'W') {
					profitLost[i] = 1;
				} else {
					profitLost[i] = -1;
				}
			}
			bufferedReader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		int gcd = BigInteger.valueOf(n).gcd(BigInteger.valueOf(m)).intValue();
		int period = m / gcd;
		int min = Math.min(n, m);
		Person[] leadersTable = new Person[min];
		int remainder = 0;
		for (int i = 0; i < n; i++) {
			remainder = i % m;
			if ((leadersTable[remainder] == null) || (people[i].money < leadersTable[remainder].money)) {
				leadersTable[remainder] = people[i];
			}
		}

		int cycles = 0;
		List<Person> leaders = Arrays.asList(leadersTable);
		Person purest = Collections.min(leaders);
		remainder = n % m;
		while (true) {
			long tmp = Math.min(period - cycles, purest.money);
			for (Person person : leaders) {
				for (int i = 0; i < tmp; i++)
					person.money += profitLost[(int)((person.start + (long)i * remainder) % m)];
				person.start = (person.start + tmp * remainder) % m;
			}
			cycles += tmp;
			purest = Collections.min(leaders);

			if (purest.money == 0) {
				res += ((long) (cycles - 1)) * n + (long) (purest.id + 1);
				return res;
			}

			if (cycles == period) { // end of story
				boolean downing = false;
				for (Person person : leaders) {
					if (person.oldMoney > person.money)
						downing = true;
				}
				if (downing == false)
					return -1;

				res += ((long) cycles) * n;

				Person winner = null;
				long delta = 1;
				for (Person person : leaders) { // nie jest to oczywiste
					delta = person.oldMoney - person.money;
					if (delta > 0) {
						if (winner == null) {
							winner = person;
						} else {
							long cycleWinner = winner.oldMoney / delta;
							long cyclePerson = person.oldMoney / delta;

							if (cycleWinner > cyclePerson) {
								winner = person;
							} else if (cycleWinner == cyclePerson) {
								if ((winner.oldMoney % delta) > (person.oldMoney % delta))
									winner = person;
								if ((winner.oldMoney % delta) == (person.oldMoney % delta)) {
									if (person.id < winner.id)
										winner = person;
								}

							} else {

							}

						}
					}
				}
				long cycleWinner = winner.oldMoney / delta;
				if (winner.oldMoney % delta == 0) {
					res += ((long) Math.max(0, (cycleWinner - 1))) * n;
				} else {
					res += ((long) Math.max(0, cycleWinner)) * n;
				}

				res += (long) (winner.id + 1);

				return res;
			}
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		
/*		  InputStream in = System.in; try { System.setIn(new
		  FileInputStream("src/main/resources/0.in")); } catch
		  (FileNotFoundException e) { e.printStackTrace(); }
	*/	 
		System.out.println(result());
		// System.setIn(in);
		System.exit(0);
	}

}