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
// PA 2015, miodziu@poczta.fm

#include <cstdio>
#include <cstdlib>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <cmath>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int,int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef unsigned long long ULL;
#define mp make_pair

int read() { int n; scanf("%d", &n); return n; }
char readc() { char c; scanf("%c", &c); return c; }
LL readl() { LL n; scanf("%lld", &n); return n; }

#define REP(i,n) for (int i=0,_=(n); i<_; ++i)
#define FOR(i,a,b) for (int i=(a),_=(b); i<=_; ++i)
#define FORD(i,a,b) for (int i=(a),_=(b); i>=_; --i)

#define st first
#define nd second

char s[1000009];

class Cycle {
    VI v, m;
    VVI n;
public:
    Cycle() : v(1, 0) {}
    void add(bool val) {
        v.push_back(v.back() + (val ? 1 : -1));
    }
    void prepare() {
        m.resize(v.size());
        m.back() = v.back();
        FORD(i, m.size()-2, 0)
            m[i] = std::min(m[i+1], v[i]);
        n.resize(v.size());

        VI st(1, v.size()-1);
        FORD(i, v.size()-2, 0) {
            while ((!st.empty()) && v[i] <= v[st.back()])
                st.pop_back();
            if (!st.empty()) {
                n[i].push_back(st.back());
            }
            st.push_back(i);
        }

        FOR(x, 1, 100) {
            bool change = false;
            FORD(i, v.size()-(1<<x), 0) {
                if (n[i].size() < x) continue;
                int j = n[i][x-1];
                if (n[j].size() < x) continue;

                n[i].push_back(n[j][x-1]);
                change = true;
            }
            if (change == false) break;
        }
    }
    LL res(int a, int p) { // a > 0
        if (p == 0) return res0(a);
        if (a > v[p] - m[p]) { //v[p] == m[p] || a > v[p] - m[p]
            a += v.back() - v[p];
            LL r = res0(a);
            if (r < 0) return r;
            return r + v.size() - 1 - p;
        }
        int pp = p;
        FOR(x, 0, 100) {
            if (a == 0) break;
            if (a % 2 == 1)
                pp = n[pp][x];
            a /= 2;
        }
        return pp - p;
    }
    LL res0(int a) {
        if (a + m[0] <= 0) {
            int pp = 0;
            FOR(x, 0, 100) {
                if (a == 0) break;
                if (a % 2 == 1)
                    pp = n[pp][x];
                a /= 2;
            }
            return pp;
        }
        if (v.back() >= 0) return -1;
        int d = -v.back();
        int rr = (a + m[0] + d - 1) / d;
        LL res = rr;
        res *= v.size() - 1;
        return res + res0(a - rr * d);
    }
};

LL solve() {
    int n = read();
    VI a(n);
    REP(i, n) a[i] = read();
    int m = read();
    scanf("%s", s);

    int d = n % m;

    if (d == 0) {
        int r = 999999999;
        int id = -1;
        REP(i, n) {
            if (s[i%m] == 'P') if (a[i] < r) {
                r = a[i];
                id = i;
            }
        }
        if (id < 0) return -1;
        LL res = n;
        res *= r - 1;
        res += id + 1;
        return res;
    }

    vector<Cycle> cycles;
    VI cid(m);
    VI pos(m);
    REP(i, m) if (s[i] != '*') {
        int id = cycles.size();
        cycles.push_back(Cycle());
        for (int x=i, p=0; s[x]!='*'; ++p, x+=d, x-=(x>=m?m:0)) {
            cycles.back().add(s[x] == 'W');
            s[x] = '*';
            cid[x] = id;
            pos[x] = p;
        }
        cycles.back().prepare();
    }

    LL r;
    int id = -1;
    REP(i, n) {
        int x = i % m;
        LL cur = cycles[cid[x]].res(a[i], pos[x]);
        if (cur <= 0) continue;
        if (id < 0 || cur < r) {
            r = cur;
            id = i;
        }
    }
    if (id < 0) return -1;
    LL res = n;
    res *= r-1;
    res += id + 1;
    return res;
}

int main() {
    printf("%lld\n", solve());
    return 0;
}