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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <iostream>
#include <vector>
#include <deque>

//#define debug

const int MAX = 1000000 + 10;

int NWD(int a, int b)
{
	return b == 0 ? a : NWD(b, a%b);
}

int U[MAX];
char P[MAX];
int p[MAX];
int n, m;
int nwd;
long long nww;
int nn;
int mm;

long long updateResult(long long old, long long c)
{
	if (c == -1)
		return old;
	else if (old == -1)
		return c;
	else
		return std::min(old, c);
}

class Solver
{
public:
	long long Solve(int offset)
	{
		this->offset = offset;
		int result = -1;
		
#ifdef debug		
		std::clog << ":: offset " << offset << std::endl;
#endif

		int sum = 0;
		for (int i=0;i<mm;++i)
			p[i] = P[(i*n+offset)%m];
		
		for (int i=0;i<mm;++i)
			sum -= p[i];
		
#ifdef debug	
		std::clog << ":: sum " << sum << std::endl;
#endif	
		
		std::deque<int> l,r;
		std::deque<int> plus, minus;
		std::deque<int> freeMinus;
		int ls = 0, rs = 0;
		
		for (int i=0;i<mm;++i)
		{
			r.push_back(i);
			rs += p[i];
			if (rs <= 0)
			{
				ls += rs;
				rs = 0;
				while (!r.empty())
				{
					int index = r.front();
					if (p[index % mm] == 1)
						plus.push_back(index);
					else
						minus.push_back(index);
					l.push_back(index);
					r.pop_front();
				}
				while (minus.size() > plus.size())
				{
					int index = minus.back();
					minus.pop_back();
					freeMinus.push_back(index);
				}
			}
		}
		
		for (int i=0;i<nn;++i)
		{
			int ui = i*nwd + offset;
			
#ifdef debug	
			std::clog << ":: user " << ui << std::endl;
			std::clog << "::    max loss " << ls << std::endl;
#endif	
			
			int full_cycles = 0;
			if (sum > 0)
				full_cycles = std::max((U[ui]+ls+sum-1) / sum, 0);
			int coins = U[ui] - full_cycles*sum;
			long long nr = -1;
			
#ifdef debug				
			std::clog << "::    full cycles " << full_cycles << std::endl;
#endif

			if (-ls >= coins)
			{
#ifdef debug
				std::clog << "::    steps in last cycle " << freeMinus[coins-1] - i << std::endl;
#endif
				long long steps = freeMinus[coins-1] - i;
				
				nr = full_cycles*nww + steps*n + ui + 1;
			}
			
			result = updateResult(result, nr);
			
			if (l.empty())
			{
				ls += p[r.front()];
				rs -= p[r.front()];
				l.push_back(r.front());
				r.pop_front();
			}
			
			int rindex = l.front();
			if (!plus.empty() && plus.front() == rindex)
				plus.pop_front();
			if (!freeMinus.empty() && freeMinus.front() == rindex)
				freeMinus.pop_front();
			
			while (minus.size() > plus.size())
			{
				int index = minus.back();
				minus.pop_back();
				freeMinus.push_front(index);
			}
			
			ls -= p[l.front()%mm];
			l.pop_front();
			r.push_back(i+mm);
			rs += p[i%mm];
			
			if (rs <= 0)
			{
				ls += rs;
				rs = 0;
				while (!r.empty())
				{
					int index = r.front();
					if (p[index % mm] == 1)
						plus.push_back(index);
					else
						minus.push_back(index);
					l.push_back(index);
					r.pop_front();
				}
				while (minus.size() > plus.size())
				{
					int index = minus.back();
					minus.pop_back();
					freeMinus.push_back(index);
				}
			}
		}
#ifdef debug	
		std::clog << ":: ---------------------" << std::endl << std::endl;
#endif	
		return result;
	}

private:
	int offset;
};

Solver S;


int main(int argc, char **argv)
{	
	std::ios_base::sync_with_stdio(0);
	
	
	std::cin >> n;
	for (int i=0;i<n;++i)
		std::cin >> U[i];
	std::cin >> m;
	for (int i=0;i<m;++i)
		std::cin >> P[i];
	for (int i=0;i<m;++i)
		P[i] = P[i]=='W' ? 1 : -1;
	
	nwd = NWD(n,m);
	nww = (long long)n*(long long)m/(long long)nwd;
	nn = n/nwd;
	mm = m/nwd;
	long long result = -1;
	
	for (int i=0;i<nwd;++i)
		result = updateResult(result, S.Solve(i));
	
	std::cout << result << std::endl;
	
	return 0;
}