Niestety, nie byliśmy w stanie w pełni poprawnie wyświetlić tego pliku, ponieważ nie jest zakodowany w UTF-8. Możesz pobrać ten plik i spróbować otworzyć go samodzielnie.
  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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//Jakub Staro�
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <stdexcept>
#include <unordered_map>
#include <unordered_set>

#ifdef COMPILING_HOME
#define DEBUG_MODE
#endif

using namespace std;

typedef char int8;
typedef unsigned char uint8;
typedef short int int16;
typedef unsigned short int uint16;
typedef int int32;
typedef unsigned int uint32;
typedef long long int64;
typedef unsigned long long uint64;

typedef std::pair<int32,int32> int32_pair;
typedef std::pair<uint32, uint32> uint32_pair;
typedef std::pair<int64,int64> int64_pair;
typedef std::pair<uint64,uint64> uint64_pair;

typedef std::vector<bool> bit_vector;

#ifdef DEBUG_MODE
#define debug_print(x) cerr << #x << " = " << x << endl
#define print_line cerr << "Line " << __LINE__ << endl
#include <cassert>
#else
#define debug_print(x)
#define print_line
#define assert(x)
#endif

#define rep(i, x) for(uint32 i = 0 ; i < (x) ; i++)
#define for_range(i, begin, end) for(auto i = (begin) ; i != (end) ; ++i )
#define all(c) (c).begin(),(c).end()
#define sort_all(x) sort( all(x) )
#define divide(a, b) ( ( (b)%(a) ) == 0 )
#define mp(x, y) make_pair(x,y)
#define pb(x) push_back(x)

#define sig(x) ( (x) == 0 ? 0 : ( (x) < 0 ? -1 : 1 ) )

const double epsilon = 1e-5;

template<class T>
void unique(std::vector<T>& v) {
	sort_all(v);
	v.resize( std::unique(all(v)) - v.begin() );
}

ostream& newline(ostream& str) {
	str.put('\n');
	return str;
}

template<typename T1, typename T2>
istream& operator>>(istream& stream, std::pair<T1, T2>& pair) {
	stream >> pair.first >> pair.second;
	return stream;
}

template<typename T1, typename T2>
ostream& operator<<(ostream& stream, const std::pair<T1, T2>& pair) {
#ifdef DEBUG_MODE
	stream << "(" << pair.first << ", " << pair.second << ")";
#else
	stream << pair.first << ' ' << pair.second;
#endif
	return stream;
}

template<class T>
inline pair<T,T> operator+(const pair<T,T>& a, const pair<T,T>& b) {
	return pair<T,T>(a.first + b.first, a.second + b.second);
}

template<class T>
inline pair<T,T> operator-(const pair<T,T>& a, const pair<T,T>& b) {
	return pair<T,T>(a.first - b.first, a.second - b.second);
}

template<class T>
ostream& operator<<(ostream& str, const vector<T>& v) {
	if(!v.empty())	{
		for(int32 i = 0 ; i + 1 < v.size() ; i++)		
			str << v[i] << ' ';		
		str << v.back();
	}
	return str;
}

struct jump_data {
	uint32 index;
	uint64 steps;
};

struct vertex {
	int32 delta;
	vector<jump_data> jumps;
};

uint32 GCD(uint32 a, uint32 b) {
	while (a > 0) {
		uint32 c = b % a;
		b = a;
		a = c;
	}
	return b;
}

int64 infinity = 1000000000000000000LL;

uint32 IntegerLogarithm(uint32 n) {
	uint32 result = 0;
	while ((1uLL << (result + 1)) <= n)
		result++;
	return result;
}

class Application {
public:
	Application() { 
	}

	void Run() {
		WczytajDane();
		cycle_length = M / GCD(M, N);

		const uint32 max_log = 20;
		rep (index, M)
			verticles[index].jumps.reserve(max_log + 1);

		rep (index, M) {
			if (!visited[index])
				SolveOneStepJumps(index);
		}

		for_range (jump_length, 1, max_log + 1) {
			rep (index, M) {
				// ju� mniejsze pot�gi 2 pad�y
				if (verticles[index].jumps.size() < jump_length)
					continue;

				uint32 next_index = verticles[index].jumps[jump_length - 1].index;
				// w naszym nast�pniku pad�y mniejsze pot�gi...
				if (verticles[next_index].jumps.size() < jump_length)
					continue;

				uint32 new_index = verticles[next_index].jumps[jump_length - 1].index;
				uint64 new_steps = verticles[index].jumps[jump_length - 1].steps +
														verticles[next_index].jumps[jump_length - 1].steps;

				verticles[index].jumps.push_back({new_index, new_steps});
			}
		}
/*
		rep (index, M) {
			debug_print(index);
			debug_print(verticles[index].jumps.size());
			if (!verticles[index].jumps.empty()) {
				rep(i, verticles[index].jumps.size()) {
					debug_print(i);
					debug_print(verticles[index].jumps[i].index);
					debug_print(verticles[index].jumps[i].steps);
				}
			}
		}
*/
		uint64 minimal_result = infinity;

		rep (i, N) {
			int64 days = HowMuchDays(i % M, finanse[i]);
			if (days == infinity)
				continue;

			assert(days >= 1);
			days--;

			uint64 result = 1 + (uint64)i + (uint64)N * days;
			//debug_print(i);
			//debug_print(days);
			//debug_print(result);
			if (result < minimal_result)
				minimal_result = result;
		}

		if (minimal_result >= infinity) {
			cout << -1 << newline;
		}
		else {
			cout << minimal_result << newline;
		}
	}

	int64 HowMuchDays(uint32 index, uint32 finanse) {
		int64 result = 0;
		while (finanse > 0) {
			uint32 log = IntegerLogarithm(finanse);
			assert((1u<<log) <= finanse);

			if (verticles[index].jumps.size() <= log)
				return infinity;

			finanse -= (1u<<log);
			jump_data jump = verticles[index].jumps[log];
			result += jump.steps;
			index = jump.index;
		}
		return result;
	}

	void SolveOneStepJumps(uint32 index) {
		int32 sum = 0;

		// sum, index, start day
		typedef tuple<int32, int32, int32> entry_type;
		priority_queue<entry_type> Q;
		Q.emplace(0, index, 0);
		visited[index] = true;

		rep(days, 2 * cycle_length) {
			while (!Q.empty() && get<0>(Q.top()) > sum) {
				entry_type top = Q.top();
				Q.pop();
				int32 top_sum = get<0>(top);
				int32 top_index = get<1>(top);
				int32 top_day = get<2>(top);
				assert(top_sum == 1 + sum);
				verticles[top_index].jumps.push_back({index, days - top_day});
			}

			sum += verticles[index].delta;
			index = (index + N) % M;
			if (!visited[index])
				Q.emplace(sum, index, days + 1);
				visited[index] = true;
		}
	}

	void WczytajDane() {
		cin >> N;
		finanse.resize(N);
		rep (i, N) {
			cin >> finanse[i];
		}

		cin >> M;
		verticles.resize(M);
		rep (i, M) {
			char c;
			cin >> c;

			verticles[i].delta = (c == 'W')? 1 : -1;
		}

		visited.assign(M, false);
	}

	uint32 N, M;
	uint32 cycle_length;
	vector<int32> finanse;
	vector<vertex> verticles;
	bit_vector visited;
};


int main() {
	ios::sync_with_stdio(0);
	Application application;
	application.Run();
	return 0;
}