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

const int maxn=1000005;
const int maxm=1000005;
int pieniadze[maxn];
int strategia[maxm];

unsigned long long f_nww(int a, int b){
	unsigned long long ab=a*b;
	while(b!=0){
		int akt=b;
		b=a%b;
		a=akt;
	}
	ab/=a;
	return ab;
}

int main(){
	int n,m;
	
	cin >> n;
	for(int i=0; i<n; i++) cin >> pieniadze[i];
	cin >> m;
	string ciag;
	cin >> ciag;
	long long nww=f_nww(n,m);
	if(nww!=n && nww!=m){ //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		int ileP=0;
		int ileW=0;
		int min=9999999;
		for(int i=0; i<ciag.size(); i++) 
			if(ciag[i]=='P') ileP++;
			else ileW++;
		for(int i=0; i<n; i++) if(pieniadze[i]<min) min=pieniadze[i];
		if(ileP==0) cout << "-1" << endl;
		else if(ileP<min&&ileP<=ileW) cout << "-1" << endl;
		else {
			int ile;
			if(min%ileP==0) ile=min/ileP-1;
			else ile=min/ileP;
			unsigned long long wynik=nww*ile+1;
			unsigned long long krok=0;
			for(int i=0; i<n; i++) pieniadze[i]-=(ileP-ileW)*(ile);
			int aktn=0;
			int aktm=0;
			while(true){
				if(ciag[aktm]=='W') pieniadze[aktn]++;
				if(ciag[aktm]=='P'){
					 pieniadze[aktn]--;
					 if(pieniadze[aktn]==0) {
					 	cout << wynik << endl;	
					 	break;
					 }
				}
				aktn++;
				aktm++;
				wynik++;
				krok++;
				if(krok>nww){
					cout << "-1" << endl;
					break;
				}
				if(aktm%m==0) aktm=0;
				if(aktn%n==0) aktn=0;
			}
				
		}
	
		
	}
	else if(nww==n && nww!=m) {  // n wieksza
		int aktm=0;
		int min=9999999;
		int mini=9999999;
		for(int i=0; i<n; i++){
			if(ciag[aktm]=='P'){
				if(pieniadze[i]<min){
					min=pieniadze[i];
					mini=i;
					}
				}
			aktm++;
			if(aktm%m==0) aktm=0;
			}
		if(min==9999999) cout << "-1" << endl;
		else{
			unsigned long long wynik=mini+1;
			pieniadze[mini]--;
			wynik+=n*pieniadze[mini];
			cout << wynik << endl;
			}
		}   
	else if(nww==m && nww!=n) { // m wieksza    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		int aktn=0;
		for(int i=0; i<nww; i++){
		if(ciag[i]=='P') strategia[aktn]--;
		else strategia[aktn]++;
		aktn++;
		if(aktn%n==0) aktn=0;	
		}
		int ile=9999999;
		for(int i=0; i<n; i++){
			if(strategia[i]<0){
				if(pieniadze[i]<=(-1*strategia[i])) ile=0;
				if(pieniadze[i]%(-1*strategia[i])==0) ile=pieniadze[i]/(-1*strategia[i])-1;
				else ile=pieniadze[i]/(-1*strategia[i]);
			}
		}
		if(ile==9999999) {
			cout << "-1" << endl;
			
		}
		else{
			unsigned long long wynik=n*ile+1;
			for(int i=0; i<n; i++) pieniadze[i]+=ile*strategia[i];
		    aktn=0;
			int aktm=0;
			unsigned long long krok=0;
			while(true){
				if(ciag[aktm]=='W') pieniadze[aktn]++;
				if(ciag[aktm]=='P'){
					 pieniadze[aktn]--;
					 if(pieniadze[aktn]==0) {
					 	cout << wynik << endl;	
					 	break;
					 }
				}
				aktn++;
				aktm++;
				wynik++;
				krok++;
				if(krok>nww){
					cout << "-1" << endl;
					break;
				}
				if(aktm%m==0) aktm=0;
				if(aktn%n==0) aktn=0;
			}
		
		}
		
		
	}
	else{   //n==m
		int min=9999999;
		int mini=9999999;
		for(int i=0; i<n; i++){
			if(ciag[i]=='P'){
				if(pieniadze[i]<min){
					min=pieniadze[i];
					mini=i;
					}
				}
			}
		if(min==9999999) cout << "-1" << endl;
		else{
			unsigned long long wynik=mini+1;
			pieniadze[mini]--;
			wynik+=n*pieniadze[mini];
			cout << wynik << endl;
			}
	}
	return 0;
}