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
#include <algorithm>
#include <iostream>
#include <cassert>
#include <cstdint>
#include <limits>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <deque>

#ifdef MYDEBUG
#define AND_FALSE
#else
#define AND_FALSE && false
#endif

#define CHECK(cond) do \
    if (!(cond) AND_FALSE) { \
      cerr << "CHECK failed in line " << __LINE__ << ": " << #cond << endl; \
      exit(1); \
    } \
  while (false)

using namespace std;

using i64 = int64_t;

namespace std {
template <typename T>
ostream& operator<< (ostream& out, const vector<T>& v) {
  out << "{";
  const char* sep = "";
  for (const auto& x : v) {
    out << sep << x;
    sep = ", ";
  }
  out << "}";
  return out;
}
}

int GameValue(char g) {
  return (g == 'W') ? 1 : -1;
}

struct Cycle {
  explicit Cycle(const string& s) : s_(s) {
    // cout << s << s << endl;
    cycle_length_ = s.size();
    running_sum_.resize(cycle_length_ * 2 + 1);
    running_sum_[0] = 0;
    for (int i = 1; i <= cycle_length_ * 2; ++i) {
      running_sum_[i] = running_sum_[i-1] + GameValue(s[(i-1) % cycle_length_]);
    }

    vector<int> first_smaller;
    {
      vector<int> stk;
      first_smaller.resize(cycle_length_ * 2 + 1);
      stk.push_back(0);
      for (int i = 1; i <= cycle_length_ * 2; ++i) {
        while (!stk.empty() && running_sum_[i] == running_sum_[stk.back()] - 1) {
          first_smaller[stk.back()] = i;
          stk.pop_back();
        }
        stk.push_back(i);
      }
      for (int i : stk) {
        first_smaller[i] = -1;
      }
      // cout << "running_sum_: " << running_sum_ << endl;
      // cout << "first_smaller: " << first_smaller << endl;
    }
    jump_table.resize(first_smaller.size());
    for (vector<int>& v : jump_table) {
      v.reserve(11);
    }

    for (size_t i = 0; i < first_smaller.size(); ++i) {
      if (first_smaller[i] >= 0) {
        jump_table[i].push_back(first_smaller[i]);
      }
    }

    bool go = true;
    size_t level = 0;
    while (go) {
      go = false;
      for (size_t i = 0; i < jump_table.size(); ++i) {
        if (jump_table[i].size() > level &&
            jump_table[jump_table[i][level]].size() > level) {
          jump_table[i].push_back(jump_table[jump_table[i][level]][level]);
          go = true;
        }
      }
      ++level;
    }

    /*
    cout << jump_table.size() << endl;
    int elts = 0;
    size_t maxc = 0;
    for (const auto& v : jump_table) {
      elts += v.capacity();
      maxc = max(maxc, v.capacity());
    }
    cout << elts << endl;
    cout << maxc << endl;
    */
  }

  Cycle(const Cycle&) = delete;

  ~Cycle() {
  }

  int BiggestDecreaseFromPos(int pos) const {
    int decrease = 0;
    const int end_pos = pos + cycle_length_ + 1;
    while (true) {
      const vector<int>& jumps = jump_table[pos];
      int p = jumps.size() - 1;
      for (; p >= 0; --p) {
        if (jumps[p] < end_pos) {
          pos = jumps[p];
          decrease += 1 << p;
          break;
        }
      }
      if (p < 0) break;
    }
    return decrease;
  }

  int Sum() const {
    return running_sum_[cycle_length_] - running_sum_[0];
  }

  int FindDecreaseFromPos(const int start_pos, int decrease) const {
    int pos = start_pos;
    size_t level = 0;
    while (decrease != 0) {
      if (decrease & 1) {
        CHECK(jump_table[pos].size() > level);
        pos = jump_table[pos][level];
      }
      ++level;
      decrease >>= 1;
    }
    return pos - start_pos;
  }

  string s_;
  int cycle_length_;
  vector<int> running_sum_;
  vector<vector<int>> jump_table;
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin >> n;
  CHECK(1 <= n && n <= 1000000);
  
  vector<int> boys;
  boys.resize(n);
  for (int& boy : boys) {
    cin >> boy;
    CHECK(1 <= boy && boy <= 1000000);
  }

  int m;
  cin >> m;
  CHECK(1 <= m && m <= 1000000);
  string games_cycle;
  cin >> games_cycle;
  CHECK(static_cast<int>(games_cycle.size()) == m);

  deque<Cycle> cycles;
  vector<int> which_cycle(m, -1);
  vector<int> pos_in_cycle(m, -1);

  for (int j0 = 0; j0 < m; ++j0) {
    if (which_cycle[j0] >= 0)
      break;
    string cycle;
    int j = j0;
    int pos = 0;
    while (true) {
      // cout << "j=" << j << endl;
      which_cycle[j] = cycles.size();
      pos_in_cycle[j] = pos;
      cycle += games_cycle[j];
      j = (j + n) % m;
      ++pos;
      if (j == j0) break;
    }
    cycles.emplace_back(cycle);
    // cout << endl;
  }

  /*
  for (const Cycle& c : cycles) {
    cout << "cycle: " << c.s_ << endl;
  }
  cout << "which_cycle: " << which_cycle << endl;
  cout << "pos_in_cycle: " << pos_in_cycle << endl;

  cout << endl;
  */

  i64 best = numeric_limits<i64>::max();

  for (int i = 0; i < n; ++i) {
    int money = boys[i];
    // cout << "boy: " << i << " money: " << money << endl;
    const int cycle_num = which_cycle[i % m];
    const int start_pos = pos_in_cycle[i % m];
    const Cycle& cycle = cycles[cycle_num];
    // cout << "cycle_num: " << cycle_num << " s:" << cycle.s_ << endl;
    // cout << "start_pos: " << start_pos << endl;
    // cout << "cycle_sum: " << cycle.Sum() << endl;
    const int biggest_decrease_from_pos = cycle.BiggestDecreaseFromPos(start_pos);
    // cout << "biggest_decrease_from_pos: " << biggest_decrease_from_pos << endl;
    i64 moves = i;
    // cout << "moves=" << moves << endl;
    if (money >= biggest_decrease_from_pos && cycle.Sum() < 0) {
      int full_cycles = -((money - biggest_decrease_from_pos) / cycle.Sum());
      money += cycle.Sum() * full_cycles;
      // cout << "full_cycles: " << full_cycles << endl;
      if (money > biggest_decrease_from_pos) {
        money += cycle.Sum();
        ++full_cycles;
      }
      CHECK(money <= biggest_decrease_from_pos);
      CHECK(money > 0);
      // cout << "full_cycles: " << full_cycles << endl;
      moves += static_cast<i64>(cycle.cycle_length_) * n
             * full_cycles;
    }
    // cout << "money=" << money << endl;
    // cout << "moves=" << moves << endl;
    if (money <= biggest_decrease_from_pos) {
      CHECK(money > 0);
      moves += static_cast<i64>(n) * (cycle.FindDecreaseFromPos(start_pos, money) - 1) + 1;
      // cout << "moves=" << moves << endl;
      // cout << endl;
      best = min(best, moves);
    }
  }
  if (best == numeric_limits<i64>::max()) {
    best = -1;
  }
  cout << best << endl;
}