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
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <cmath>
#include <cstring>
#include <string>
#include <iostream>
#include <complex>
#include <sstream>
#include <cassert>
using namespace std;
 
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef vector<int> VI;
typedef pair<int,int> PII;
 
#define REP(i,n) for(int i=0;i<(n);++i)
#define SIZE(c) ((int)((c).size()))
#define FOR(i,a,b) for (int i=(a); i<(b); ++i)
#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i)
#define ALL(v) (v).begin(), (v).end()
 
#define pb push_back
#define mp make_pair
#define st first
#define nd second

LL INF = 2000000LL * 2000000LL * 2000000LL;

int S[2000000];
char C[2000000];

bool visited[2000000];
int cycle[2000000];
int inv[2000000];

LL rounds[2000000];

int main() {
  int N, M;
  scanf("%d", &N);
  REP(i,N) scanf("%d", &S[i]);
  scanf("%d%s", &M, C);
  REP(i,M) C[i] = (C[i] == 'W');

  int M2 = M;
  while (M < N) {
    REP(i,M2) C[M+i] = C[i];
    M += M2;    
  }
  
  REP(p,N) {
    if (visited[p]) continue;

    int v = p;
    int K = 0;
    do {
      inv[v] = K;
      cycle[K++] = v;
      visited[v] = true;
      v = (v + N) % M;
    } while (v != p);
    
    int cur = 0;
    int mini = 0;
    priority_queue<PII> Q;
    REP(i, K) {
      int v = cycle[i];
      if (v < N) Q.push(mp(cur - S[v], v));     
      if (C[v]) ++cur; else --cur;
      
      while (!Q.empty() && Q.top().st >= cur) {
        int w = Q.top().nd;
        Q.pop();
        rounds[w] = i - inv[w];
      }
      
      mini = min(mini, cur);
    }
    
    priority_queue<PII> Q2;
    while (!Q.empty()) {
      int w = Q.top().nd;      
      S[w] = cur - Q.top().st;      
      rounds[w] = K - inv[w];
 
      int drop = -cur;     
      if ((S[w] + mini) <= 0) Q2.push(mp(-S[w], w));
      else if (drop > 0) {
        int cycles = (S[w] + mini + (drop - 1)) / drop;
        S[w] -= drop * cycles;
        rounds[w] += K * (LL)cycles;
        Q2.push(mp(-S[w], w));  
      } else {
        rounds[w] = INF;
      }
      
      Q.pop();      
    }
    
    cur = 0;
    REP(i, K) {
      int v = cycle[i];
      if (C[v]) ++cur; else --cur;
      while (!Q2.empty() && Q2.top().st >= cur) {
        int w = Q2.top().nd;
        Q2.pop();
        rounds[w] += i;
      }
    }
  }  
  
  LL result = INF;
  REP(i,N) if (rounds[i] < INF) result = min(result, rounds[i] * N + i);
  if (result == INF) {
    printf("-1\n");
  } else {
    printf("%lld\n", result + 1);
  }
}