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
#include<cstdio>
#include<iostream>
using namespace std;
int tab[1000100];
int bat[1000100];
int tab1[1000100];
long long int n,m;
long long int nwd(long int n,long int m){
    long long int jwp;
    while(m!=0){
        jwp = m;
        m = n%m;
        n = jwp;
    }
    return n;
}
int trele(long long int g){
    bool spr=false;
    int wyn=0;
    int w=0;
    while(g!=0){
        for(int i=0;i<n;i++){
            wyn++;
            tab[i] += bat[w];
            if(tab[i] == 0){
                spr = true;
                break;
            }
            w++;
            if(w==m){
                w=0;
                g = g-1;
            }
        }
        if(spr==true)
            break;
    }
    if(spr)
        return wyn;
    else
        return -1;

}
int  morele(long long int g){
    bool spr=false;
    int wyn=0;
    int w=0;
    while(1){
        for(int i=0;i<n;i++){
            wyn++;
            tab[i] += bat[w];
            if(tab[i] == 0){
                spr = true;
                break;
            }
            w++;
            if(w==m)
                w=0;
        }
        if(spr==true)
            break;
    }
    return wyn;
}
int main(){

    cin >> n;
    for(int i=0;i<n;i++){
        cin >> tab[i];
        tab1[i] = tab[i];
    }
    cin >> m;
    char c;
    for(int i=0;i<m;i++){
        cin >> c;
        if(c == 'W')
            bat[i] = 1;
        else
            bat[i] = -1;

    }

    long long int d;
    d = nwd(n,m);
    d = n*m/d;

    long long int g=d/m;
    int dd=0;
    dd=trele(g);
    if(dd != -1)
        cout << dd ;
    else{
        bool sprr=false;
        for(int i=0;i<n;i++){
            if(tab1[i] > tab[i]){
                sprr = true;
                break;
            }
        }

        if(!sprr)
            cout << "-1";
        else
            cout << morele(g) + d;
    }
    return 0;
}