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
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
#include <queue>
#include <bitset>
#include <utility>
#include <stack>

using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define MP make_pair
#define FOR(v,p,k) for(auto v=(p);v<=(k);++v)
#define FORD(v,p,k) for(auto v=(p);v>=(k);--v)
#define REP(i,n) for(auto i=0;i<(n);++i)
#define VAR(v,i) __typeof(i) v=(i)
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define PB push_back
#define ST first
#define ND second
#define SIZE(x) (int)x.size()
#define ALL(c) c.begin(),c.end()

#define ODD(x) ((x)%2)
#define EVEN(x) (!(ODD(x)))

template <typename T>
inline void eraseElement(std::vector<T> &vec, const T &val) {
  vec.erase(std::remove(vec.begin(), vec.end(), val), vec.end());
}

template <typename T>
inline int sgn(T val) {
      return (T{} < val) - (val < T{});
}

template <typename T>
inline T ceil(T x, T y) {
      return x/y + sgn(x%y);
}



#define MAXN 1000009


int n, m;
LL n_big;

struct Cycle {
    int id;
    int sum;
    VI chart;
    VI chart_acc;
    vector<PII> depthPos;
    vector<PII> chart_acc_min_right;
};

int main() {
  ios_base::sync_with_stdio(0);

  cin >> n;
  n_big = n;
  VI budget(n);

  REP(i, n) {
    cin >> budget[i];
  }
  cin >> m;
  VI automat(m);
  REP(i, m) {
    char x;
    cin >> x;
    //assert( x=='W' || x=='P');
    automat[i] = (x=='W' ? 1 : -1);
  }
  if (m==1) {
    //jest przynajmniej jedno W
    cout << -1 << endl;
    return 0;
  }

  vector<PII> where(m, MP(-1,-1));
  int cycle_cnt = 0;
  vector<Cycle> cycles(m);
  REP(i, m) {
      if (where[i].first != -1) continue;

      int cycle_num = cycle_cnt;
      Cycle &c = cycles[cycle_num];
      c.id = cycle_num;
      cycle_cnt++;
      c.sum = 0;

      int j=i;
      int k=0;
      do {
        c.sum += automat[j];
        c.chart.PB(automat[j]);
        where[j].first=cycle_num;
        where[j].second=k;
        k++;

        j = (j+n)%m;
      } while (j != i);

  }

  REP(cycle_num, cycle_cnt) {
    Cycle &c = cycles[cycle_num];
    int old_size = c.chart.size();
    c.chart.resize(old_size * 2);
    copy(c.chart.begin(), c.chart.begin()+old_size, c.chart.begin()+old_size);
    //c.sum *= 2;
    int new_size = c.chart.size();
    c.chart_acc.reserve(new_size);
    partial_sum(ALL(c.chart), back_inserter(c.chart_acc));

    c.depthPos.reserve(new_size);
    REP(i, new_size) {
      c.depthPos.PB(MP(c.chart_acc[i], i));
    }

    sort(ALL(c.depthPos));

    c.chart_acc_min_right.resize(new_size);
    auto best_right = MP(100000000, 100000000);
    FORD(i, new_size-1, 0) {
      best_right = min(best_right, MP(c.chart_acc[i], i));
      c.chart_acc_min_right[i]=best_right;
    }
  }

  LL MX = 1000000000000000007LL;
  LL res = MX;
  REP(i, n) {
    int cycle_id = where[i%m].first;
    int cycle_pos = where[i%m].second;
    Cycle const &c = cycles[cycle_id];
    int bd = budget[i];
    LL cand = i;

    int curr = c.chart_acc[cycle_pos] - c.chart[cycle_pos];
    auto it = lower_bound(ALL(c.depthPos), MP(curr - bd, cycle_pos));
    if (it == c.depthPos.end() || it->first != (curr - bd) || it->second < cycle_pos) {
      if (c.sum >= 0) continue;
      int old_size = c.chart.size()/2;

      auto best_low = curr - c.chart_acc_min_right[cycle_pos].first;
      //assert(best_low >= 0);
      //assert(bd - best_low > 0);

      auto hm = ceil(bd - best_low, -c.sum);
      cand += (n_big * hm ) * old_size;
      bd -= hm * (-c.sum);

      if (bd > 0) {
        it = lower_bound(ALL(c.depthPos), MP(curr - bd, cycle_pos));
        //assert(it != c.depthPos.end());
        //assert(it->first == (curr - bd));
        //assert(it->second > cycle_pos);

        cand += n_big * (it->second - cycle_pos) + 1;
      }

    } else {
      cand += n_big * (it->second - cycle_pos) + 1;
    }
    res = min(res, cand);

  }

  cout << ((res == MX) ? -1 : res) << endl;

  return 0;
}