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
#include <cstdio>
//#include <iostream>
//#include <algorithm>
//#include <string>
#include <vector>
//#include <complex>
//#include <iterator>
//#include <set>
//#include <bitset>
//#include <map>
//#include <stack>
//#include <list>
#include <queue>
//#include <deque>
using namespace std;
typedef vector<int> VI;
typedef long long LL;
#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 VAR(v, n) typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int)(x).size())
#define FOREACH(i, c) for(i=(c).begin(); i !=(c).end(); ++i)
//#define FORE(i, c) for(VAR(i, (c).begin()), KS=(c).end(); i !=KS; ++i)
#define PB push_back
#define ST first
#define ND second
const int INF = 1000000000;
const int MAX=1000001;
const LL INFL=(LL)INF*INF;
const double EPS = 10e-9;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<double> VD;
//typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
VI::iterator iti;
#define PF push_front
#define MP make_pair
int nwd(int a,int b)
{
  while(b)
  {
    a%=b;
    if(a)
      b%=a;
    else
      break;
  }
  return a+b;
}
struct los
{
  int k;
  int n;
  char c;
  int p;
};
int nd;
void podzial(const VI &k,const char c[],const int m,int i,vector<los>& l)
{
  int j,i2=i;
  for(j=0;j<m/nd;++j)
  {
    l[j].c=c[i];
    l[j].k=INF;
    for(i2=i;i2<SIZE(k);i2+=m)
      if(k[i2]<l[j].k)
      {
        l[j].k=k[i2];
        l[j].n=i2;
      }
    i+=SIZE(k);
    i%=m;
  }
}
int f(int k,int p,int mn,int r)
{
  int z=p-k;
  if(z>=mn)
    return z;
  z=(p-k-mn)%r;
  if(z<0)
    z+=r;
  return z+mn;
}
LL utrata (vector<los>& l,const int n,const int roz)
{
  l.resize(2*roz);
  int mn=0,mx=0,r,ba,i;
  LL w=INFL,wt;
  for(i=0;i<roz;++i)
    l[i+roz]=l[i];
  l[0].p=0;
  for(i=1;i<2*roz;++i)
  {
    l[i].p=l[i-1].p+l[i-1].c;
    if(l[i].p<mn)
      mn=l[i].p;
    else if(l[i].p>mx)
      mx=l[i].p;
  }
  for(i=0;i<2*roz;++i)
    l[i].p-=mn;
  mx-=mn;
  ++mx;
  vector<queue<int> > vq(mx);
//  VI vw(mx);
  for(i=0;i<2*roz;++i)
  {
    vq[l[i].p].push(i);
 //   printf("li %d %d\n",l[i].p,i);
  }
  for(i=0;i<mx;++i)
    vq[i].push(INF);
  r=l[0].p-l[roz].p;
 // printf("r=%d\n",r);
  mn=0;
/*  for(i=0;i<mx;++i)
  {
    vw[i]=vq[i].front();
    vq[i].pop();
  }*/
  if(r>0)
    for(i=0;i<roz;++i)
    {
      ba=vq[f(l[i].k,l[i].p,mn,r)].front();
      wt=(l[i].k+l[ba].p-l[i].p)/r;
      wt*=roz;
      wt+=ba-i;
      --wt;
      wt*=n;
      wt+=l[i].n;
      if(wt<w && wt>=0)
        w=wt;
      vq[l[i].p].pop();
      while(vq[mn].size()==1)
        ++mn;
    }
  else
    for(i=0;i<roz;++i)
    {
      ba=l[i].p-l[i].k;
      if(ba<mn)
      {
        vq[l[i].p].pop();
        while(vq[mn].size()==1)
          ++mn;
        continue;
      }
      wt=vq[ba].front()-i;
      --wt;
      wt*=n;
      wt+=l[i].n;
      if(wt<w && wt>=0)
        w=wt;
      vq[l[i].p].pop();
      while(vq[mn].size()==1)
        ++mn;
    }
  l.resize(roz);
  return w;
}
int main()
{
  int n,m,i;
  char c[MAX];
  scanf("%d",&n);
  VI k(n);
  LL w=INFL,wt;
  FOREACH(iti,k)
    scanf("%d",&*iti);
  scanf("%d",&m);
  scanf("%s",c);
  for(i=0;i<m;++i)
    if(c[i]=='W')
      c[i]=1;
    else
      c[i]=-1;
  nd=nwd(n,m);
  vector<los> l(m/nd);
  for(i=0;i<nd;++i)
  {
    podzial(k,c,m,i,l);
//    for(j=0;j<m/nd;++j)
//      printf("%d %d %d\n",l[j].k,l[j].n,(int)l[j].c);
    wt=utrata(l,n,m/nd);
    if(wt<w)
      w=wt;
//    printf("%I64d\n",wt);
  }
  if(w==INFL)
    printf("-1");
  else
    printf("%lld",w+1);
  return 0;
}