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
//Tadrion
#include <cstdio>
#include <vector>
#include <iostream>
#include <deque>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
#include <utility>
using namespace std;
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define CLEAR(x) (memset(x,0,sizeof(x)))
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()
#define VAR(v, n) __typeof(n) v = (n)
#define FOR(x, b, e) for(int x = b; x <= (e); ++x)
#define FORD(x, b, e) for(int x = b; x >= (e); --x)
#define REP(x, n) for(int x = 0; x < (n); ++x)
#define FOREACH(i, c) for(VAR(i,(c).begin()); i != (c).end(); ++i)
#define DBG(v) cout<<#v<<" = "<<v<<endl;
#define IN(x,y) ((y).find(x)!=(y).end())
#define ST first
#define ND second
#define PB push_back
#define PF push_front
#define MP make_pair
#define CCC 42

typedef long long int LL;
typedef pair<int,int> PII;
typedef vector<int> VI;

int n,m;
int zysk[CCC][1000010];
int naStart[CCC][1000010];
int a[1000010];
char hh[1000010];
int nexxt[CCC][1000010];
int start[1000010];
LL kroki[1000010];
const LL INF2 = 1000000000001LL;
LL minnKroki;
VI v;


LL ileKrokow(int ind, int a) {
    int h=CCC-1;
    LL res=0;
    //ind = start[ind];
    while(1) {
        if(res > minnKroki) return MIN(res,INF2);
        while(h>0 && a+naStart[h][ind] <= 0) h--;
        if(h==0) {
            if(a+naStart[h][ind] == 0) return res+1;
            else return res+2;
        }
        else {
            res += (1LL<<h);
            if(res > INF2) return INF2;
            a += zysk[h][ind];
            ind = nexxt[h][ind];
            //h--;
        }
    }

    /*int h=0;
    LL res=0;
    while(h<=41 && a+zysk[h][ind] > -zysk[h][nexxt[h][ind]]) {
        h++;
    }
    if(h==0) return 1;
    if(h==42) return INF2;
    while(1) {
        while(h>0 && a+zysk[h][ind] <= 0) h--;
        if(h==0) {
            if(a+zysk[0][ind]==0) return 1+res;
            else return res;
        }
         else {
            res += (1LL<<h);
            a += zysk[h][ind];
            ind = nexxt[h][ind];
            h--;
        }
    }
    */
}

int main() {
  scanf("%d",&n);
  FOR(i,0,n-1) scanf("%d",&a[i]);
  FOR(i,0,n-1) v.PB(i);
  scanf("%d",&m);
  scanf("%s",hh);
  REP(i,m) {
    if(hh[i]=='W') {
        zysk[0][i]=1;
        naStart[0][i]=1;
    }
    else {
        zysk[0][i]=-1;
        naStart[0][i]=-1;
    }
  }
  int cnt = 0;

  FOR(i,0,n-1) {
    start[i] = cnt;
    cnt++;
    if(cnt == m) cnt=0;
  }
  FOR(i,0,m-1) nexxt[0][i] = (i+n)%m;

  FOR(h,1,CCC-1) {
    FOR(i,0,m-1) {
        nexxt[h][i] = nexxt[h-1][nexxt[h-1][i]];
    }
  }

  FOR(h,1,CCC-1) {
    FOR(i,0,m-1) {
        zysk[h][i] = zysk[h-1][i] + zysk[h-1][nexxt[h-1][i]];
        if(zysk[h][i] > 1000000002LL) zysk[h][i]=1000000002LL;
        if(zysk[h][i] < -1000000002LL) zysk[h][i]=-1000000002LL;
    }
  }
  FOR(h,1,CCC-1) {
    FOR(i,0,m-1) {
        if(naStart[h-1][i] >= 0) {
            naStart[h][i] = zysk[h-1][i] + naStart[h-1][nexxt[h-1][i]];
        }
        else {
        naStart[h][i] =
            MIN(naStart[h-1][i],
                zysk[h-1][i] + naStart[h-1][nexxt[h-1][i]]);
        }
        if(naStart[h][i] > 1000000002LL) naStart[h][i]=1000000002LL;
        if(naStart[h][i] < -1000000002LL) naStart[h][i]=-1000000002LL;
    }
  }
/*
  FOR(h,0,40) {
    printf("h = %d\n",h);

    FOR(i,0,m-1) {
        printf("%d ",nexxt[h][i]);
    }

    printf("\n");
    FOR(i,0,m-1) {
        printf("%d ",zysk[h][i]);
    }10
10 10 10 10 10 10 10 10 10 9
11
PPPPPPPPPPW
    printf("\n");
    FOR(i,0,m-1) {
        printf("%d ",naStart[h][i]);
    }
    printf("\n\n");
  }
*/

  /*FOR(i,0,n-1) {
    kroki[i] = ileKrokow(start[i],a[i]);
//    printf("%d %lld\n",i,kroki[i]);
  }*/

  int index=-1;
  minnKroki=INF2;

  FOR(i,1,3) {
    random_shuffle(v.begin(),v.end());
  }

  FOR(i,0,n-1) {
    LL rr = ileKrokow(start[v[i]],a[v[i]]);
    if(rr < minnKroki) {
        minnKroki = rr;
        index = v[i];
    }
    else if(rr == minnKroki && index > v[i]) index=v[i];
  }

  //printf("minnKroki: %lld w %d\n",minnKroki,index);
  LL res = 0;
  if(minnKroki==INF2) printf("-1\n");
  else {
    FOR(i,0,index) res += minnKroki;
    FOR(i,index+1,n-1) res += minnKroki-1;
    printf("%lld\n",res);
  }
  return 0;
}