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
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
#include <iostream>

using namespace std;

int MAX=1;
int n,m;
int gdz[1000007];
long long tab[1000007];
int dol[1000007];
int ile[1000007];
bool wyg[1000007];
long long kop[1000007];
int kie[1000007];
vector< vector<int> >v;
vector< vector<int> >sp;
vector< vector<int> >tree;
vector< vector<int> >maks;

long long min2(long long a, long long b)
{
    if(a<=b) return a;
    return b;
}

void update(int id, int e, int v)
{
    int ve=e+MAX;
    tree[id][ve]=v;
    maks[id][ve]=v;
    ve/=2;
    while(ve>=1)
    {
        maks[id][ve]=max(maks[id][2*ve],maks[id][2*ve+1]);
        tree[id][ve]=min(tree[id][2*ve],tree[id][2*ve+1]);
        ve/=2;
    }
}

int query(int id, int a, int b)
{
    int va=a+MAX, vb=b+MAX;
    int wyn=min(tree[id][va],tree[id][vb]);
    while(va/2!=vb/2)
    {
        if(va%2==0) wyn=min(wyn,tree[id][va+1]);
        if(vb%2==1) wyn=min(wyn,tree[id][vb-1]);
        va/=2; vb/=2;
    }
    return wyn;
}

int znajdz(int id,int node, int i, int j, int a, int b, int v)
{
    if(j<a) return -1;
    if(i==j) return i;
    else{
        int y=-1;
        if(tree[id][2*node]<=v&&v<=maks[id][2*node])
            y = znajdz(id,2*node,i,(i+j)/2,a,b,v);
        if(y==-1&&tree[id][2*node+1]<=v&&v<=maks[id][2*node+1])
            y = znajdz(id,2*node+1,(i+j)/2+1,j,a,b,v);
        return y;
    }
}

int nwd(int a,int b)
{
    if(b==0) return a;
    return nwd(b,a%b);
}

int main()
{
    scanf("%d",&n);
    for(int i=0; i<n; i++)
        scanf("%lld",&tab[i]);
    scanf("%d",&m);
    int nw=nwd(n,m);
    long long nww=(long long)n*m/nw;
    int l=m/nw;
    for(int i=0; i<m; i++)
    {
        char s=getchar();
        while(isspace(s)) s=getchar();
        if(s=='W') wyg[i]=1;
        //printf("%d %d\n",wyg[i],i%nw);
    }
    //printf("ok\n");
    v.resize(nw);
    for(int i=0; i<nw; i++)
    {
        v[i].push_back(-1);
        int li=1;
        for(long long j=i; j<nww; j+=n)
        {
            v[i].push_back(j%m);
            gdz[j%m]=li++;
        }
        int vv=v[i].size();
        for(int j=1; j<vv; j++)
            v[i].push_back(v[i][j]);
    }
    //printf("ok\n");
    sp.resize(nw);
    tree.resize(nw);
    maks.resize(nw);
    while(MAX<=2*l+1) MAX<<=1;
    //printf("%d\n",MAX);
    for(int i=0; i<v.size(); i++)
    {
        sp[i].resize(2*l+1);
        tree[i].resize(2*MAX+7);
        maks[i].resize(2*MAX+7);
        for(int j=1; j<v[i].size(); j++)
        {
            sp[i][j]=sp[i][j-1];
            if(wyg[v[i][j]]) sp[i][j]++;
            else sp[i][j]--;
            update(i,j,sp[i][j]);
            //printf("%d ",v[i][j]+1);
        }
        //printf("\n");
    }
    for(int i=0; i<n; i++)
    {
        //printf("%d %d %d\n",i%nw,gdz[i%m],gdz[i%m]+l-1);
        dol[i]=query(i%nw,gdz[i%m]-1,gdz[i%m]+l-1)-sp[i%nw][gdz[i%m]-1];
        //printf("%d %d\n",i+1,dol[i]);
        kop[i]=tab[i]+dol[i];
        ile[i]=sp[i%nw][gdz[i%m]+l-1]-sp[i%nw][gdz[i%m]-1];
        //printf("%d %d\n",kop[i],ile[i]);
    }
    long long wyn=0;
    int mini=1000000007;
    for(int i=0; i<n; i++)
    {
        if(kop[i]<=0) mini=min(mini,0);
        else
        {
            if(ile[i]>=0) kie[i]=-1;
            else kie[i]=(int)ceil((long double)kop[i]/(-ile[i]));
        }
        if(kie[i]!=-1) mini=min(mini,kie[i]);
    }
    if(mini==1000000007)
    {
        printf("-1\n");
        return 0;
    }
    wyn+=(long long)mini*nww;
    long long mii=nww+2;
    for(int i=0; i<n; i++)
    {
        tab[i]=tab[i]+mini*ile[i];
        kop[i]=kop[i]+mini*ile[i];
        if(kop[i]<=0)
        {
            int pos=znajdz(i%nw,1,0,MAX-1,gdz[i%m], gdz[i%m]+l-1,sp[i%nw][gdz[i%m]-1]-tab[i]);
            //printf("%d %d\n",gdz[i%m],pos);
            mii=min2(mii,i+1+(long long)(pos-gdz[i%m])*n);
        }
    }
    printf("%lld\n",wyn+mii);

	return 0;
}