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
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;

const int MAX_BOYS = 1000001;

int n;	//number of boys
int budget[MAX_BOYS];	//amount of money each boy owns
int m;	//machine cycle length
char cycle[MAX_BOYS];	//machine cycle

int gc_div;

int small_cycle_sum;
char small_cycle[MAX_BOYS];
int small_cycle_cumulative[MAX_BOYS];
int index_in_small_cycle[MAX_BOYS];
int cycle_no_for_loss[MAX_BOYS];
int min_up_to[MAX_BOYS];
int index_min_up_to[MAX_BOYS];

int min_in_cycle;
int index_min_in_cycle;

int cumulative_sum;

int max_from[MAX_BOYS];
int index_max_from[MAX_BOYS];

int max_in_cycle;
int index_max_in_cycle;

inline int gcd(int a, int b) {
	int t;
	while (b != 0) {
		t = b;
		b = a % b;
		a = t;
	}
	return a;
}

void count_cycle(int s) {
	int k = m / gc_div;
	for (int i = 1; i <= k; i++) cycle_no_for_loss[i] = -1;
	int r = s % m;
	int t = cycle[r];
	int sum_cum = t;
	small_cycle_sum = t;
	small_cycle[0] = t;
	small_cycle_cumulative[0] = sum_cum;
	index_in_small_cycle[r] = 0;
	if (sum_cum < 0) cycle_no_for_loss[-sum_cum] = 0;
	min_up_to[0] = t;
	index_min_up_to[0] = 0;
	min_in_cycle = t;
	index_min_in_cycle = 0;
	for (int i = 1; i < k; i++) {
		r = (r + n) % m;
		t = cycle[r];
		small_cycle_sum += t;
		small_cycle[i] = t;
		sum_cum += t;
		small_cycle_cumulative[i] = sum_cum;
		index_in_small_cycle[r] = i;
		if ((sum_cum < 0) && (cycle_no_for_loss[-sum_cum] < 0)) cycle_no_for_loss[-sum_cum] = i;
		if (sum_cum < min_in_cycle) {
			min_up_to[i] = sum_cum;
			index_min_up_to[i] = i;
			min_in_cycle = sum_cum;
			index_min_in_cycle = i;
		} else {
			min_up_to[i] = min_up_to[i-1];
			index_min_up_to[i] = index_min_up_to[i-1];
		}
	}
	cumulative_sum = small_cycle_cumulative[k-1];
	sum_cum = small_cycle[k-1];
	max_from[k-1] = sum_cum;
	index_max_from[k-1] = k-1;
	max_in_cycle = sum_cum;
	index_max_in_cycle = k-1;
	for (int i = k-2; i >= 0; i--) {
		sum_cum += small_cycle[i];
		if (sum_cum > max_in_cycle) {
			max_from[i] = sum_cum;
			index_max_from[i] = i;
			max_in_cycle = sum_cum;
			index_max_in_cycle = i;
		} else {
			max_from[i] = max_from[i+1];
			index_max_from[i] = index_max_from[i+1];
		}
	}
}

inline int min_in_cycle_from(int f) {
	if (f == 0) {
		return min_in_cycle;
	}
	int min_a;
	int min_b;
	if (f <= index_min_in_cycle) {
		min_a = min_in_cycle - small_cycle_cumulative[f-1];
		min_b = cumulative_sum - small_cycle_cumulative[f-1] + min_up_to[f-1];
	} else {
		min_a = min_in_cycle + cumulative_sum - small_cycle_cumulative[f-1];
		min_b = cumulative_sum - small_cycle_cumulative[f-1] - max_from[f];
	}
	return min_a <= min_b ? min_a : min_b;
}

inline int number_of_games(int f, int b) {
	int k =  m / gc_div;
	if (f == 0) return cycle_no_for_loss[b];
	int ind = b - small_cycle_cumulative[f-1];
	if ((ind > 0) && (ind <= k)) {
		int num = cycle_no_for_loss[ind] - f;
		if (num >= 0) return num;
	}
	return cycle_no_for_loss[b - small_cycle_cumulative[f-1] + cumulative_sum] + k - f;
}

int main(int argc, char *argv[]) {
	scanf("%d\n", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &budget[i]);
	}
	scanf("\n%d\n", &m);
	scanf("%s", cycle);
	for (int i = 0; i < m; i++) {
		if (cycle[i] == 'W') 
			cycle[i] = 1;
		else 
			cycle[i] = -1;
	}
	gc_div = gcd(n, m);
	bool not_found = true;
	long no_of_games = -1;
	int nd = n / gc_div;
	for (int i = 0; i < gc_div; i++) {
		count_cycle(i);
		for (int j = 0; j < nd; j++) {
			int p = j * gc_div + i;
			int sp = index_in_small_cycle[p % m];
			int max_loss = -min_in_cycle_from(sp);
			if (max_loss >= budget[p]) {
				long nog = number_of_games(sp, budget[p]);
				nog *= n;
				nog += p;
				nog += 1;
				if (not_found) {
					no_of_games = nog;
					not_found = false;
				} else {
					if (nog < no_of_games) no_of_games = nog;
				}
			} else if (cumulative_sum < 0) {
				int nb = budget[p] - max_loss;
				int noc = (nb - cumulative_sum - 1) / (-cumulative_sum);
				int b_decl = noc * (-cumulative_sum);
				nb = budget[p] - b_decl;
				long nog = noc;
				nog *= m;
				nog += number_of_games(sp, nb);
				nog *= n;
				nog += p;
				nog += 1;
				if (not_found) {
					no_of_games = nog;
					not_found = false;
				} else {
					if (nog < no_of_games) no_of_games = nog;
				}
			}
		}
	}
	if (not_found) {
		printf("-1\n");
	} else {
		printf("%ld\n", no_of_games);
	}
	return 0;
}