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
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

typedef long long LL;

const int MAX = 1000000;
const LL INF = 1LL<<62;

int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }

struct node {
  int pref, suf, in, sum;
  node(int v = 0) { pref = suf = in = max(v, 0); sum = v; }

};

node operator+(const node &a, const node &b) {
  node res;
  res.pref = max(a.pref, a.sum + b.pref);
  res.suf = max(a.suf + b.sum, b.suf);
  res.in = max(max(a.in, b.in), a.suf + b.pref);
  res.sum = a.sum + b.sum;
  return res;
}

class tree {
public:
  void init(int n) {
    for (size=1;size<n;size*=2);
    V.assign(2*size, node());
  }

  void set(int ind, int v) {
    V[ind+size] = node(v);
  }

  void build() {
    for (int i=size-1;i>0;i--)
      V[i] = V[2*i] + V[2*i+1];
  }

  node get(int p, int q) { // [p; q)
    return _get(size+p, size+q);
  }

  int query(int p, int q, int s) {
    return _query(1, 0, size, p, q, s).second - p;
  }

private:
  int size;
  vector<node> V;

  node _get(int p, int q) {
    if (p + 1 == q) return V[p];
    if (p&1) return V[p] + _get(p+1, q);
    if (q&1) return _get(p, q-1) + V[q-1];
    return _get(p/2, q/2);
  }

  pair<bool, int> _query(int v, int k, int len, int p, int q, int s) {
    if (len == 1) {
      if (V[v].sum == s)
        return {true, v-size};
      else
        return {false, V[v].sum};
    }
    if(p <= k && k+len <= q && V[v].pref < s) {
      return {false, V[v].sum};
    }
    auto lft = (k+len/2 <= p) ? pair<bool, int>{false, 0} : _query(2*v, k, len/2, p, q, s);
    if (lft.first) return lft;
    auto rght = _query(2*v+1, k+len/2, len/2, p, q, s - lft.second);
    if (!rght.first) rght.second += lft.second;
    return rght;
  }
};

int arr[MAX];
char S[MAX+1];
tree T;
LL res;

int n, m;
int cyc_len;
int group_size;

pair<int, int> compute_whole_cycles(int boy_index, int cyc_start) {
  node t = T.get(cyc_start, cyc_start + cyc_len);
  if (t.pref >= arr[boy_index]) return {0, t.sum};
  if (t.sum <= 0) return {-1, t.sum};
  int money_left = arr[boy_index];
  int ret = (money_left - t.pref) / t.sum;
  money_left -= ret * t.sum;
  if (money_left > t.pref) {
    ret++;
    money_left -= t.sum;
  }
  return {ret, t.sum};
}

void solve(int boy_index, int cyc_start) {
  auto ret = compute_whole_cycles(boy_index, cyc_start);
  int whole_cycles = ret.first;
  if (whole_cycles == -1) {
    return;
  }
  int money_left = arr[boy_index] - whole_cycles * ret.second;
  // money_left <= t.pref

  int cyc_index = T.query(cyc_start, cyc_start + cyc_len, money_left);
  LL cand = (LL)boy_index + (LL)n * whole_cycles * cyc_len + (LL)cyc_index * n + 1;
  if (cand < res) res = cand;
}

int pos[MAX];
void solve(int start) {
  int index = start;
  for (int i=0;i<cyc_len;i++)
  {
    T.set(i, S[index] == 'P' ? 1 : -1);
    T.set(i+cyc_len, S[index] == 'P' ? 1 : -1);
    pos[index] = i;
    index = (index + n) % m;
  }
  T.build();

  index = start;
  for (int i=0;i<group_size;i++)
  {
    solve(index, pos[index % m]);
    index = (index + m) % n;
  }
}

int main() {
  scanf("%d", &n);
  for (int i=0;i<n;i++)
    scanf("%d", arr+i);
  scanf("%d", &m);
  scanf("%s", S);
  int d = gcd(n, m);
  cyc_len = m / d;
  group_size = n / d;
  T.init(2 * cyc_len);
  res = INF;
  for (int i=0;i<d;i++)
    solve(i);
  printf("%lld\n", res == INF ? -1 : res);
  return 0;
}