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

//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::| HEAP
inline int parent(int i) { return (i >> 1); }
inline int left(int i)   { return (i << 1); }
inline int right(int i)  { return (i << 1) + 1; }
inline int count_size(int i){--i; register int n=1; while(i) {i>>=1; n<<=1;} return n;}
//inline int min(int a, int b) { return (a < b ? a : b); }

#define min(a,b) ((a)<(b)?(a):(b))

const long long INF = 9223372036854775806L;

struct State {
  int max_depth;
  int final_heigth;

  State(int max_depth=0, int final_heigth=0) 
    : max_depth(max_depth), final_heigth(final_heigth) {}
};

State operator+(const State& s1, const State& s2) {
  long long l_md = s1.max_depth;
  long long l_fh = s1.final_heigth;
  long long r_md = s2.max_depth;
  long long r_fh = s2.final_heigth;
  long long result_md = min(l_md, l_fh+r_md);
  long long result_fh = l_fh+r_fh;
  //fprintf(stderr, "Laczymy przedzialy [%2d:%2d] + [%2d:%2d] = [%2d:%2d]\n", l_md, l_fh, r_md, r_fh, result_md, result_fh);

  return State(result_md, result_fh);
}


struct Group {
  long long grade;
  long long heap_size;
  long long cycle_length;
  State* heap;

  Group(vector<char>& cycle, long long length) {
    cycle_length = length;
    fprintf(stderr, "Cykl: dlugosc = %d\n", length);
    heap_size = count_size(2 * length);
    //printf("  rozmiar kopca = %d\n", heap_size);
    
    
    heap = new State[2*heap_size];
    for(int i=0; i<2*heap_size; ++i) heap[i].max_depth = heap[i].final_heigth = 0;
    
    grade = 0;
    for(int i=0; i<length; ++i) {
      char c = cycle[i];
      fprintf(stderr, "%c: ", c);
      switch(c) {
        case 'P':
          --grade;
          heap[heap_size+i].max_depth = heap[heap_size+length+i].max_depth = heap[heap_size+i].final_heigth = heap[heap_size+length+i].final_heigth = -1;
          fprintf(stderr, "grade-1 = %d\n", grade);
          break;
        case 'W':
          ++grade;
          heap[heap_size+i].max_depth = heap[heap_size+length+i].max_depth = 0;
          heap[heap_size+i].final_heigth = heap[heap_size+length+i].final_heigth = 1;
          fprintf(stderr, "grade+1 = %d\n", grade);
          break;
        default:
          break;
          fprintf(stderr, "ERROR!!!!!!!!!\n");
      }
    }

    for(int i=heap_size-1; i>0; --i) {
      heap[i] = heap[left(i)] + heap[right(i)];
    }
  }

  int max_depth(int i, int j) {
    fprintf(stderr, "Sprawdzamy najwieksza glebokosc miedzy %d a %d:\n", i,j);
    if(j<i) j += cycle_length;
    State left, right;
    int hi = heap_size+i, hj = heap_size+j;
    left = heap[hi];
    //fprintf(stderr, "  lewe skrajne polozenie: [%2d:%2d]\n", left.max_depth, left.final_heigth);
    if(hi != hj) {
      right = heap[hj];
      //fprintf(stderr, "  inne prawe skrajne polozenie: [%2d:%2d]\n", right.max_depth, right.final_heigth);
    }
    while(parent(hi) != parent(hj)) {
      //fprintf(stderr, "%d, %d\n", hi&1==0, hj&1==1);
      if((hi&1) == 0) {
        //fprintf(stderr, "  dolaczamy prawa bombke do lewego przedzialu: [%2d:%2d]\n", heap[hi+1].max_depth, heap[hi+1].final_heigth);
        left = left + heap[hi+1];
      }
      if((hj&1) == 1) {
        //fprintf(stderr, "  dolaczamy lewa bombke do prawego przedzialu: [%2d:%2d]\n", heap[hj-1].max_depth, heap[hj-1].final_heigth);
        right = heap[hj-1] + right;
      }
      //fprintf(stderr, "  przechodzimy do parentow\n");
      hi = parent(hi);
      hj = parent(hj);
    }
    long long result = (left + right).max_depth;
    fprintf(stderr, "  '---> %d\n", result);
    return result;
  }

  long long check(int i, long long money, long long children_counter) {
    fprintf(stderr, "----------------------------------\n");
    long long m, begin = i, end = i + cycle_length - 1;
    while(begin<end) {
      m = (begin+end)>>1;
      if(money+max_depth(i, m) <= 0) {
        fprintf(stderr, "Przegra! sprawdzmy czy przegra wczesniej!\n");
        end = m;
      } else {
        fprintf(stderr, "Nie przegra! sprawdzmy czy przegra pozniej!\n");
        begin = m+1;
      }
    }
    fprintf(stderr, "Przegra od %d do %d! \n", i, begin);


    fprintf(stderr, "----------------------------------\n");
    return (begin - i) * children_counter;
  }



// int query(int a, int b) {
//   int va = M + a, vb = M + b;
//   /* Skrajne przedziały do rozkładu. */
//   int wyn = w[va];
//   if (va != vb) wyn += w[vb];
//   /* Spacer aż do momentu spotkania. */
//   while (va / 2 != vb / 2) {
//    if (va % 2 == 0) wyn += w[va + 1]; /* prawa bombka na lewej ścieżce */
//    if (vb % 2 == 1) wyn += w[vb - 1]; /* lewa bombka na prawej ścieżce */
//    va /= 2; vb /= 2;
//   }
//   return wyn;
// }

  void print() {
    fprintf(stderr, "GROUP! grade = %d\n", grade);
    for(int i=1, j=2; i<2*heap_size; ++i) {
      fprintf(stderr, "[%2d:%2d]", heap[i].max_depth, heap[i].final_heigth);
      if(i==j-1) {
        fprintf(stderr, "\n");
        j <<= 1;
      }
    }
  }
};

long long NWD(long long a, long long b)
{
    if(b) return NWD(b, a % b);
    return a;
}


int main() {
  long long min_fail = INF;

  int n;
  scanf("%d", &n);

  vector<int> money;
  for(int i=0; i<n; ++i) {
    int x;
    scanf("%d", &x);
    money.push_back(x);
  }

  int m;
  scanf("%d\n", &m);

  vector<char> automata;
  for(int i=0; i<m; ++i) {
    automata.push_back(getchar());
  }

  /*for(int i=0; i<m; ++i)
    printf("%c", automata[i]);
  printf("\n");
*/
  vector<bool> visited(m, false);
  for(int i=0; i<m; ++i) {
    if(!visited[i]) {
      int j = i;
      vector<char> buff;
      vector<int> children;
      vector<int> nr;
      while(!visited[j]) {
        if(j<n) {
          children.push_back(j);
          nr.push_back(buff.size());
        }

        buff.push_back(automata[j]);
        visited[j] = true;
        j = (j + n) % m;
      }
      Group g(buff, buff.size());
      g.print();
      for(int k=0; k<children.size(); ++k) {
        long long child = children[k];
        fprintf(stderr, "Sprawdzamy dziecko %d (ilosc piniazkow: %d)\n", child, money[child]);
        fprintf(stderr, "  zaczyna od %d miejsca w cyklu\n", nr[k]);

        long long nr_k = nr[k];
        long long md = g.max_depth(nr_k, nr_k+g.cycle_length-1);
        if(money[child]+md <= 0) {
          fprintf(stderr, "Dziecko %d przegra podczas tego cyklu. Kiedy? Sprawdzmy!\n", child);
          long long when = g.check(nr_k, money[child], n);
          when = when + child + 1;
          min_fail = min(min_fail, when);
          fprintf(stderr, "Dziecko %d przegra podczas %d gry!\n", child, when);
        } else if(g.grade >= 0) {
          fprintf(stderr, "To dziecko nigdy nie przegra!\n");
        } else {
          fprintf(stderr, "Kiedys przegra ale nie podczas tego cyklu\n");
          long long howmuch = (money[child]+md);
          long long when = (-(howmuch/g.grade))*g.cycle_length*n+g.check(nr_k, money[child]-(-(howmuch/g.grade))*g.cycle_length, n)+child+1;
          min_fail = min(min_fail, when);
        }
        
      }
    }
  }

  if(min_fail == INF) {
    printf("-1\n");
  } else {
    printf("%lld\n", min_fail);
  }


  return 0;
}