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
#include <iostream>
#include <cstdio>
using namespace std;

int n, m;
char c;
int ptr = 0;
long long *v, *diff, *mdiff, *curr;
long long game = 0;
long long *w;

bool isPossible() {
	bool all_positive = true;
	for (int i = 0; i < n; i++) {
		if (diff[i] < 0) {
			all_positive = false;
		}
	}
	if (all_positive) {
		return false;
	}
	return true;
}

void readInput() {
	scanf("%d\n", &n);

	v = new long long[n];
	curr = new long long[n];
	diff = new long long[n];
	mdiff = new long long[n];

	for (int i = 0; i < n; i++) {
		scanf("%lld", v + i);
		curr[i] = v[i];
	}

	scanf("%d\n", &m);
	w = new long long[m];

	for (int i = 0; i < n; i++) {
		scanf("%c", &c);
		if (c == 'W') {
			w[i] = 1;
		} else {
			w[i] = -1;
		}
	}
}

int computeDiffs() {
	bool first_round = true;
	while (ptr != 0 || first_round) {
		for (int j = 0; j < m; j++) {
			int index = (ptr + j) % n;
			diff[index] += w[j];
			mdiff[index] = min(mdiff[index], diff[index]);

			if (v[index] + diff[index] <= 0) {
				return game;
			}
			game += 1;
		}
		ptr = (ptr + m) % n;
		first_round = false;
	}
	return -1;
}

int remainingRounds() {
	long long k = -1;
	for (int i = 0; i < n; i++) {
		if (diff[i] < 0) {
			if (v[i] + diff[i] + mdiff[i] <= 0) {
				return 0;
			} else {
				if (k == -1) {
					k = (v[i] + diff[i] + mdiff[i]) / (-diff[i]);
				} else {
					k = min(k, (v[i] + diff[i] + mdiff[i]) / (-diff[i]));
				}
			}
		}
	}
	return k;
}

int simulateLastRound() {
	while (true) {
		for (int j = 0; j < m; j++) {
			int index = (ptr + j) % n;

			if (curr[index] + diff[index] <= 0) {
				return game;
			}
			game += 1;
		}
		ptr = (ptr + m) % n;
	}
	return -1;
}

void printDiff() {
	for (int j = 0; j < n; j++) {
		printf("%lld ", diff[j]);
	}
	printf("\n");
}

int main() {

	readInput();

	if (computeDiffs() > 0) {
		printf("%lld\n", game + 1);
		return 0;
	}

	if (!isPossible()) {
		printf("-1\n");
		return 0;
	}

	//printDiff();

	//printf("game: %lld\n", game);
	long long k = remainingRounds();
	//printf("rem: %lld\n", k);

	for (int i = 0; i < n; i++) {
		curr[i] += diff[i] * (k + 1);
	}

	game = game * (k + 1);

	simulateLastRound();
	printf("%lld\n", game + 1);
	return 0;
}