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
#include <cassert>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

class Tree {
 public:
  Tree(const vector<int>& v) : n_(RoundUp(v.size())), v_(2 * n_) {
    for (int i = 0; i < v.size(); ++i) v_[n_ + i] = v[i];
    for (int i = n_ - 1; i >= 0; --i) v_[i] = min(v_[2 * i], v_[2 * i + 1]);
  }

  bool Absorb(
      const pair<int, int>& node, int* position, int* balance,
      long long* moves) const {
    const int dip = ValueAt(*position) - v_[node.first];
    if (dip < *balance) {
      *balance -= ValueAt(*position);
      *position += node.second;
      *moves += node.second;
      *balance += ValueAt(*position);
      return true;
    }
    if (node.first >= n_) {
      ++*position;
      ++*moves;
      --*balance;
      return false;
    }
    if (!Absorb(make_pair(2 * node.first, node.second / 2), position,
                          balance, moves)) return false;
    if (!Absorb(make_pair(2 * node.first + 1, node.second / 2), position,
                          balance, moves)) return false;
    assert(false);
  }

  int Delta(const int left, const int right) const {
    return ValueAt(right) - ValueAt(left);
  }

  bool Dies(int left, int right, int* balance, long long* moves) const {
    // cerr << left << ' ' << right << ' ' << right - left << endl;
    int position = left;
    left += n_;
    right += n_;
    int step = 1;
    static vector<pair<int, int> > pre, post;
    pre.clear();
    post.clear();
    while (left < right) {
      // cerr << left << ' ' << right << ' ' << right - left << ' ' << step << ' ' << (right - left) * step << endl;
      if (left % 2) {
        pre.push_back(make_pair(left++, step));
        // cerr << step << endl;
      }
      if (right % 2) {
        post.push_back(make_pair(--right, step));
        // cerr << step << endl;
      }
      left /= 2;
      right /= 2;
      step *= 2;
    }

    for (int i = 0; i < pre.size(); ++i) {
      // cerr << position << ' ' << pre[i].second << endl;
      if (!Absorb(pre[i], &position, balance, moves)) return true;
      // cerr << position << endl;
    }
    for (int i = post.size() - 1; i >= 0; --i) {
      // cerr << position << ' ' << post[i].second << endl;
      if (!Absorb(post[i], &position, balance, moves)) return true;
      // cerr << position << endl;
    }
    // assert(position == right);
    return false;
  }

  int Dip(int left, int right) const {
    const int start = ValueAt(left);
    int dip = start;
    left += n_;
    right += n_;
    while (left < right) {
      if (left % 2) dip = min(dip, v_[left++]);
      if (right % 2) dip = min(dip, v_[--right]);
      left /= 2;
      right /= 2;
    }
    return start - dip;
  }

 private:
  static int RoundUp(int n) {
    int ret = 1;
    while (ret <= n) ret *= 2;
    return ret;
  }

  int ValueAt(const int i) const {
    return i ? v_[n_ + i - 1] : 0;
  }

  int n_;
  vector<int> v_;
};

int Gcd(int n, int m) {
  while (true) {
    if (!n) return m;
    m %= n;
    if (!m) return n;
    n %= m;
  }
}

int main() {
  int n;
  scanf("%d", &n);
  vector<int> money(n);
  for (int i = 0; i < n; ++i) scanf("%d", &money[i]);
  int m;
  scanf("%d", &m);
  vector<bool> win(m);
  int total_delta = 0;
  for (int i = 0; i < m; ++i) {
    char c;
    do {
      scanf("%c", &c);
    } while (c != 'W' && c != 'P');
    win[i] = c == 'W';
    total_delta += win[i] ? 1 : -1;
  }
  // cerr << "Total delta: " << total_delta << endl;

  // There are this many cyclic subgames.
  const int gcd = Gcd(n, m);
  // cerr << "Subgames: " << gcd << endl;
  // Cycle length (single subgame).
  const int cycle = m / gcd;
  // cerr << "Cycle: " << cycle << endl;
  // Players per cycle.
  const int players = n / gcd;
  // cerr << "Players: " << players << endl;
  // Player goes this much down the win list every time they reach the machine.
  const int step = n % m;
  // cerr << "Step: " << step << endl;
  long long games = -1;

  int step_gcd = -1;
  for (int i = 0; i < gcd; ++i) {
    // cerr << "Subgame " << i << endl;
    int current = 0;
    vector<int> balance;
    int manual_dip = 0;
    for (int j = 0, k = i; j < cycle; ++j, k = (k + step) % m) {
      current += win[k] ? 1 : -1;
      balance.push_back(current);
      if (current < manual_dip) manual_dip = current;
      if (i == 0 && k == gcd) {
        // cerr << "Seems that " << j << " * " << step << " == " << gcd << " mod " << m << endl;
        step_gcd = j;
      }
    }
    // cerr << "Step to GCD is " << step_gcd << endl;
    // cerr << "Manual delta is " << current << endl;
    // cerr << "Manual dip is " << manual_dip << endl;

    Tree t(balance);
    for (int j = 0, k = i; k < n; k += gcd, j = (j + step_gcd) % cycle) {
      /*
      {
        // Sanity check.  Is player k starting at jth spot in the cycle?
        long long ll = j;
        ll *= step;
        ll += i;
        ll %= m;
        if (k % m != ll) cerr << j << ' ' << k % m << ' ' << ll << endl;
      }
      cerr << "Player "<< j << " at " << k << endl;
      */
      long long moves = 0;
      int balance = money[k];
      /*
      cerr << "Start " << moves << " " << balance << endl;
      // Slow sanity check.
      {
        int balance_sanity = balance;
        for (int l = j; l < cycle; ++l) {
          long long ll = l;
          ll *= step;
          ll += i;
          ll %= m;
          if (l == j) cerr << "First game at " << ll << endl;
          balance_sanity += win[ll] ? 1 : -1;
          assert(balance_sanity > 0);
        }
        cerr << "Next round should be " << moves + cycle - (j % cycle) << ' ' << balance_sanity << endl;
      }
      */
      // Do we live to the beginning of a round?
      if (!t.Dies(j % cycle, cycle, &balance, &moves)) {
        // cerr << "Next round " << moves << " " << balance << endl;
        const int delta = t.Delta(0, cycle);
        // cerr << "Delta " << delta << endl;
        if (delta < 0) {
          // Advance time, so that our fate is decided in the next round.
          const int dip = t.Dip(0, cycle);
          // cerr << "Dip " << dip << endl;
          if (balance > dip) {
            long long rounds = 1 + ((balance - dip - 1) / -delta);
            moves += rounds * cycle;
            balance += rounds * delta;
          }
          // cerr << "Pre death " << moves << ' ' << balance << endl;
        }
        /*
        // Slow sanity check - comment out once we find the bug.
        {
          int balance_sanity = balance;
          for (int l = 0, ll = i; l < cycle; ++l, ll = (ll + step) % m) {
            balance_sanity += win[ll] ? 1 : -1;
            if (balance_sanity == 0) {
              cerr << "Sanity death at " << l << ' ' << ll << endl;
              cerr << "Moves should be " << moves + l + 1 << endl;
              break;
            }
          }
        }
        */
        // Can we play forever?
        if (!t.Dies(0, cycle, &balance, &moves)) continue;
      }
      // cerr << "Death " << moves << " " << balance << endl;
      // Convert per-player moves to global move counter.
      --moves;
      moves *= n;
      moves += k;
      ++moves;
      // cerr << "Converted moves " << moves << endl;
      // Update the max.
      if (games == -1 || moves < games) games = moves;
    }
  }

  printf("%lld\n", games);

  return 0;
}