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
#include <bits/stdc++.h>

using namespace std;

#define DB if (0)

typedef long long int ll;

const int MAXN = 1e6+10;
const int LG = 21;
const ll INF = 2000000000000000000LL;

int gcd(int a, int b) {
	while (b) {
		int c = a % b;
		a = b;
		b = c;
	}
	return a;
}

int n, m, money[MAXN], cycles, cycle_length, pos[MAXN];
char s[MAXN];

struct cycle {
	int beginning;
	int * gr;
	int * sgr;
	int * mgr;
	int * lo[LG];
	cycle() {}
	cycle(int _beginning) {
		beginning = _beginning;
		gr = new int[2*cycle_length+10];
		fill(gr, gr+2*cycle_length+10, 0);
		sgr = new int[2*cycle_length+10];
		fill(sgr, sgr+2*cycle_length+10, 0);
		mgr = new int[2*cycle_length+10];
		fill(mgr, mgr+2*cycle_length+10, 0);
		
		for (int i = 1; i <= 2 * cycle_length; i++) {
			if (i <= cycle_length) pos[(ll(beginning)+ll(i-1)*ll(n)-1LL)%m+1] = i;
			DB printf("pos[%lld] = %d\n", (ll(beginning)+ll(i-1)*ll(n)-1LL)%m+1, i);
			gr[i] = 1 - 2 * (s[(ll(beginning)+ll(i-1)*ll(n)-1LL)%m+1] == 'P');
			sgr[i] = sgr[i-1] + gr[i];
		}
		list <int> q;
		for (int i = 2 * cycle_length; i >= 1; i--) {
			while (!q.empty() && q.back() - i >= cycle_length) q.pop_back();
			while (!q.empty() && sgr[i] < sgr[q.front()]) q.pop_front();
			q.push_front(i);
			mgr[i] = sgr[i-1] - sgr[q.back()];
		}
		
		for (int l = 0; l < LG; l++) {
			lo[l] = new int[2*cycle_length+10];
			fill(lo[l], lo[l]+2*cycle_length+10, 0);
			//lo[l].reserve(2*cycle_length+10); lo[l].resize(2*cycle_length+10);
		}
		
		lo[0][2 * cycle_length + 1] = 2 * cycle_length + 1;
		lo[0][2 * cycle_length + 2] = 2 * cycle_length + 1;
		for (int i = 2 * cycle_length; i >= 1; i--) {
			if (gr[i] == -1) lo[0][i] = i;
			else lo[0][i] = lo[0][lo[0][i+1]+1];
		}
		
		for (int l = 1; l < LG; l++) {
			lo[l][2 * cycle_length+1] = 2 * cycle_length + 1;
			lo[l][2 * cycle_length+2] = 2 * cycle_length + 1;
			for (int i = 2 * cycle_length; i >= 1; i--) {
				lo[l][i] = lo[l-1][lo[l-1][i]+1];
			}
		}
		
		DB {printf("cycle(%d)\n", _beginning);
		for (int i = 1; i <= cycle_length; i++) {
			printf("%d %d %d %d %d\n", gr[i], sgr[i], mgr[i], lo[0][i], lo[1][i]);
		}}
		
	}
	int full_cycles_made(int cash, int start) {
		DB printf("full_cycles_made(cash = %d, start = %d)\n", cash, start);
		if (sgr[cycle_length] >= 0) return -300; //no cycles made
		//printf("%d %d %d %d\n", cash, start, cash-mgr[start], -sgr[cycle_length]);
		return (cash-mgr[start])/(-sgr[cycle_length]) + ((cash-mgr[start])%(-sgr[cycle_length])>0);
	}
	
	int finish_him(int cash, int start) { //da się
		int ori = start;
		bool made = false;
		//printf("_%d %d: ", cash, start);
		for (int l = LG-1; l >= 0; l--) if (cash&(1<<l)) {
			//printf("___%d\n", l);
			cash -= (1<<l);
			start = lo[l][start + made];
			made = true;
		}
		//printf("%d\n", start);
		return start-ori+1;
	}
	
	ll moves_until_bankruptcy(int cash, int start) {
		//printf("%d %d %d\n", cash, start, mgr[start]);
		ll a = full_cycles_made(cash, start);
		if (a < 0 && cash > mgr[start]) return INF;
		else a = max(0LL, a);
		ll b = finish_him(cash+sgr[cycle_length]*a, start);
		//printf("%d %d: %lld %lld\n", cash, start, a, b);
		return a * cycle_length + b;
	}
	
	void clear_memory() {
		delete [] gr;
		delete [] sgr;
		delete [] mgr;
		for (int l = 0; l < LG; l++) 
			delete [] lo[l];
	}
} C;

ll getTime(ll no, ll moves) {
	if (moves == INF) return INF;
	return no + (moves-1) * ll(n);
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &money[i]);
	}
	scanf("%d", &m);
	scanf("%s", s+1);
	
	ll kochamjulie = INF;
	
	cycles = gcd(n, m);
	cycle_length = m/gcd(n, m);
	//printf("%d %d %d %d\n", n, m, cycles, cycle_length);
	for (int i = 1; i <= cycles; i++) {
		C = cycle(i);
		for (int j = m+1; j <= n; j++) pos[j] = pos[j-m];
		for (int s = i; s <= n; s += m) {
			int ct = 0, j = (s-1)%n+1;
			while (ct < cycle_length) {
				//printf("%d\n", j);
				ll moves = C.moves_until_bankruptcy(money[j], pos[j]);
				kochamjulie = min(kochamjulie, getTime(j, moves));
				j = (j + cycles-1)%n+1;
				ct++;
			}
		}
		C.clear_memory();
	}
	if (kochamjulie == INF) printf("-1\n");
	else printf("%lld\n", kochamjulie);
	
	return 0;
}