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
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
using namespace std;

inline int clp2(int x) {
  x = x - 1;
  x |= (x >> 1);
  x |= (x >> 2);
  x |= (x >> 4);
  x |= (x >> 8);
  x |= (x >> 16);
  return (x + 1);
}

struct Node {
  Node *left_;
  Node *right_;
  int x_, y_; 
  int w_, W_;

  Node(int x, int y) {
    y = x + clp2(y - x + 1) - 1;
  
    x_ = x;
    y_ = y;
    w_ = W_ = 0;

    if (x != y) {
      left_  = new Node(x, (x + y - 1) / 2);
      right_ = new Node((x + y + 1) / 2, y);
    } else {
      left_  = nullptr;
      right_ = nullptr;
    }
  }

  ~Node() {
    if (left_ != nullptr) {
      delete left_;
    }
    if (right_ != nullptr) {
      delete right_;
    }
  }

  void Insert(int x, int y, int v) {
    x = max(x, x_);
    y = min(y, y_);

    if ((x_ == x) && (y_ == y)) {
      w_ += v;
      return;
    }

    if ((left_ != nullptr) && (left_->y_ >= x)) {
      left_->Insert(x, y, v);
    }
    if ((right_ != nullptr) && (right_->x_ <= y)) {
      right_->Insert(x, y, v);
    }

    W_ = min(left_->W_ + left_->w_, right_->W_ + right_->w_);
  }

  int Query(int x, int y) {
    x = max(x, x_);
    y = min(y, y_);

    if ((x_ == x) && (y_ == y)) {
      return (W_ + w_);
    }

    int m = INT_MAX;
    if ((left_ != nullptr) && (left_->y_ >= x)) {
      m = min(m, left_->Query(x, y));
    }
    if ((right_ != nullptr) && (right_->x_ <= y)) {
      m = min(m, right_->Query(x, y));
    }

    return (m + w_);
  }

  int QueryIndex(int x, int y, int v) {
    x = max(x, x_);
    y = min(y, y_);

    if (Query(x_, y_) > v) {
      return -1;
    } else if (x_ == y_) {
      return x;
    }

    int m = -1;
    if ((left_ != nullptr) && (left_->y_ >= x)) {
      m = left_->QueryIndex(x, y, v - w_);
    }
    if ((m == -1) && (right_ != nullptr) && (right_->x_ <= y)) {
      m = right_->QueryIndex(x, y, v - w_);
    }
  
    return m;
  }
};

struct player {
  int idx;   // Player index.
  int cash;  // Initial cash.
  int cash_left;  // How much cash is left after playing the maximum number of rounds without
                  // going bankrupt.
  int rounds;  // Number of whole cycles the player will be in game.
};
player pl[1000000 + 2];

struct item {
  item(int idx_) : idx(idx_), min_value(0) { }

  int idx;  // Index in the global period.
  int min_value;  // The minimum value in the cycle, starting at this item.
  vector<player *> players;  // Pointers to descriptors of players which start at this offset.
};

struct cycle {
  cycle(int sum_) : sum(sum_) { }

  int sum;
  vector<item> items;
};

char period[1000000 + 2];
bool visited[1000000 + 2];
vector<int> buckets[1000000 + 2];

vector<cycle> cycles;

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    pl[i].idx = i;
    scanf("%d", &pl[i].cash);
  }

  int m;
  scanf("%d", &m);
  scanf("%s", period);

  for (int i = 0; i < n; i++) {
    buckets[i % m].push_back(i);
  }

  long long wyn = LLONG_MAX;

  memset(visited, false, sizeof(visited));
  // For each cycle...
  for (int i = 0; i < m; i++) {
    if (visited[i]) {
      continue;
    }

    cycles.emplace_back(0);
    auto& cycle = cycles.back();

    // Calculate the overall cycle.
    int v = i;
    while (!visited[v]) {
      cycle.items.emplace_back(v);
      auto &it = cycle.items.back();

      // Calculate all players corresponding to this item.
      for (int j = 0; j < buckets[v].size(); j++) {
        it.players.push_back(&pl[buckets[v][j]]);
      }

      if (period[v] == 'W') {
        cycle.sum++;
      } else {
        cycle.sum--;
      }

      visited[v] = true;
      v = ((v + (n % m)) % m);
    }

    // Calculate minimum values in the cycle for each offset.
    int partial_sum = 0;
    Node *tree = new Node(0, cycle.items.size() - 1);

    for (int j = 0; j < cycle.items.size(); j++) {
      if (period[cycle.items[j].idx] == 'W') {
        partial_sum++;
      } else {
        partial_sum--;
      }

      tree->Insert(j, j, partial_sum);
    }

    for (int j = 0; j < cycle.items.size(); j++) {
      cycle.items[j].min_value = tree->Query(0, cycle.items.size() - 1);

      // Calculate the number of full rounds for each player starting at this offset.
      for (player *pp : cycle.items[j].players) {
        if (pp->cash + cycle.items[j].min_value <= 0) {
          // Not enough cash for even a single round.
          pp->rounds = 0;
          pp->cash_left = pp->cash;
        } else if (cycle.sum >= 0) {
          // We can survive a full round and the overall sum is non-negative,
          // so we'll never run out of money.
          pp->rounds = -1;
          pp->cash_left = -1;
        } else {
          // rounds = ceil((cash - min_value) / sum)
          pp->rounds = (pp->cash + cycle.items[j].min_value + ((-cycle.sum) - 1)) / (-cycle.sum);
          pp->cash_left = pp->cash + (pp->rounds * cycle.sum);
        }

        if (pp->rounds != -1) {
          int turns = tree->QueryIndex(j, cycle.items.size() - 1, -pp->cash_left);
          if (turns != -1) {
            turns -= j;
          } else if (j > 0) {
            turns = tree->QueryIndex(0, j - 1, -pp->cash_left);
            if (turns != -1) {
              turns += ((cycle.items.size() - 1) - j + 1);
            }
          }

          // Make absolutely sure that all parts of the expression are casted to long long. :-)
          wyn = min(wyn, (long long)pp->idx +
                         ((long long)pp->rounds * (long long)n * (long long)cycle.items.size()) +
                         ((long long)turns * (long long)n) +
                         1LL);
        }
      }

      int value;
      if (period[cycle.items[j].idx] == 'W') {
        value = 1;
      } else {
        value = -1;
      }
     
      if (j != cycle.items.size() - 1) {
        int prev_idx;
        if (j == 0) {
          prev_idx = cycle.items.size() - 1;
        } else {
          prev_idx = j - 1;
        }

        tree->Insert(j + 1, cycle.items.size() - 1, -value);
        if (j > 0) {
          tree->Insert(0, j - 1, -value);
        }
        tree->Insert(j, j, (tree->Query(prev_idx, prev_idx) + value) - tree->Query(j, j));
      }
    }
    
    delete tree;
  }

  if (wyn == LLONG_MAX) {
    printf("-1\n");
  } else {
    printf("%lld\n", wyn);
  }
  
  return 0;
}