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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Uzylem struktury danych RMQ ze strony: http://codeforces.com/blog/entry/15169
#include <bits/stdc++.h>

using namespace std;

#define PB push_back
#define FORE(i, t) for(__typeof(t.begin())i=t.begin();i!=t.end();++i)
#define SZ(x) int((x).size())
#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)

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

const ll LLINF = (ll) 3e18 + 9;
const int INF = 1e9 + 9;
const int MAX_N = 1e6 + 3;
const int MAX_M = 1e6 + 3;

struct RMQ {
    int N;
    vi a;

    void init(int n) {
        int lgn = 0;
        for (int x = n; x > 0; x /= 2) {
            ++lgn;
        }
        N = 1 << (lgn + 1);
        a.resize(N * 2);
        REP(i, N * 2) {
            a[i] = INF;
        }
    }

    void init(const vi &v) {
        this->init(SZ(v));
        REP(i, SZ(v)) {
            this->SetMin(i, v[i]);
        }
    }

    void SetMin(int pos, int x) {
        for (int i = pos + N; i; i >>= 1)
            a[i] = min(a[i], x);
    }

    int GetMin(int L, int R) const // [L, R) i.e. L <= i < R
    {
        int res = INF;
        for (L += N, R += N; L < R; L >>= 1, R >>= 1) {
            if (L & 1) {
                res = min(res, a[L]);
                L++;
            }
            if (R & 1) {
                R--;
                res = min(res, a[R]);
            }
        }
        return res;
    }
};

int n, m;
int money[MAX_N];
char deltas[MAX_M];

vi cycles[MAX_M];
int cycle_id[MAX_M];
int cycle_start[MAX_M];
vi cycles_sums[MAX_M];
RMQ rmqs[MAX_M];
int cycle_min_sum_pos[MAX_M];
int cycles_count = 0;

void generate_cycles() {
    REP (i, m) {
        if (cycle_id[i] == -1) {
            ++cycles_count;
            for (int j = i, pos = 0; ; j = (j + n) % m, ++pos) {
                if (!cycles[i].empty()) {
                    if (cycles[i][0] == j) {
                        break;
                    }
                }
                cycles[i].PB(j);
                cycle_id[j] = i;
                cycle_start[j] = pos;
            }
        }
    }
    FOR (i, m, n) {
        cycle_id[i] = cycle_id[i % m];
        cycle_start[i] = cycle_start[i % m];
    }
    REP (i, cycles_count) {
//        printf("icc=%d\n", i);
        int cycle_size = SZ(cycles[i]);
        int sum = 0;
        cycles_sums[i].PB(sum);
//        puts("aaa");
        REP (j, 2 * cycle_size) {
            int delta = deltas[cycles[i][j % cycle_size]] == 'W' ? 1 : -1;
            sum += delta;
            cycles_sums[i].PB(sum);
        }
//        puts("bbb");
//        printf("szcycles=%d\n", SZ(cycles_sums[i]));
        int min_el = *min_element(cycles_sums[i].begin(),
                                  cycles_sums[i].begin() + cycle_size + 1);
        REP (j, cycle_size + 1) {
            if (cycles_sums[i][j] == min_el) {
                cycle_min_sum_pos[i] = j;
                break;
            }
        }
//        puts("ccc");
        rmqs[i].init(cycles_sums[i]);
//        puts("ddd");
    }
}

ll compute_when(int x) {
    int cid = cycle_id[x];
//    printf("a=%d\n", x);
//    printf("  cid=%d cstart=%d cs_at_start=%d\n", cid, cycle_start[x], cycles_sums[cid][cycle_start[x]]);
    int money_from_min_pos = money[x] - cycles_sums[cid][cycle_start[x]] + cycles_sums[cid][cycle_min_sum_pos[cid]];
    int cycle_delta = cycles_sums[cid][SZ(cycles[cid])];
//    printf("  cycle_delta=%d\n", cycle_delta);
    int how_many_cycles = -1;
    if (money_from_min_pos > 0) {
        if (cycle_delta >= 0) {
            //TODO
            return LLINF;
        } else {
            how_many_cycles = money_from_min_pos / (-cycle_delta);
            if (money_from_min_pos % (-cycle_delta) == 0) {
                --how_many_cycles;
            }
        }
    }
    int last_safe_pos;
    int money_at_last_safe_post;
    if (how_many_cycles >= 0) {
        last_safe_pos = cycle_min_sum_pos[cid];
        money_at_last_safe_post = money_from_min_pos + cycle_delta * how_many_cycles;
    } else {
        last_safe_pos = cycle_start[x];
        money_at_last_safe_post = money[x];
    }
//    printf("  last_safe_pos=%d\n", last_safe_pos);
    int a = last_safe_pos, b = SZ(cycles_sums[cid]) * 2;
    while (a <= b) {
        int d = (a + b) / 2;
        int spend = cycles_sums[cid][last_safe_pos] - rmqs[cid].GetMin(last_safe_pos, d + 1);
        if (spend < money_at_last_safe_post) {
            a = d + 1;
        } else {
            b = d - 1;
        }
    }
    ll bankrupt_relative_pos = a - last_safe_pos - 1;
    ll bankrupt_abs_pos = -1;
    if (how_many_cycles >= 0) {
//        printf("%d - %d + %d * %d + %lld\n", cycle_min_sum_pos[cid], cycle_start[x], how_many_cycles, SZ(cycles[cid]), bankrupt_relative_pos);
        bankrupt_abs_pos =
                cycle_min_sum_pos[cid] - cycle_start[x] + (ll) how_many_cycles * (ll) SZ(cycles[cid]) +
                bankrupt_relative_pos;
    } else {
        bankrupt_abs_pos = bankrupt_relative_pos;
    }

//    printf("  mfmp=%d hmc=%d bankrupt_rel=%d bankrupt_abs=%d\n",
//           money_from_min_pos, how_many_cycles, bankrupt_relative_pos, bankrupt_abs_pos);
//    printf("x+1=%lld n=%lld bank=%lld\n", (ll) (x + 1), (ll) n, (ll) bankrupt_abs_pos);
    return (ll) (x + 1) + (ll) n * (ll) bankrupt_abs_pos;
//    printf("  -> when=%lld\n", when[x]);
}

void inline one() {
    scanf("%d", &n);
    REP (i, n) {
        scanf("%d", money + i);
        cycle_id[i] = -1;
        cycle_start[i] = -1;
    }
    scanf("%d", &m);
    scanf("%s", deltas);
    generate_cycles();

//    printf("cycles_count=%d\n", cycles_count);
//    REP(j, n) {
//        printf("j=%2d (cid=%2d, cstart=%2d): ", j, cycle_id[j], cycle_start[j]);
//        FORE(ct, cycles[j]) {
//            printf("%2d ", *ct);
//        }
//        puts("");
//    }
//    REP (i, cycles_count) {
//        printf("%d:", i);
//        FORE(jt, cycles_sums[i]) {
//            printf(" %d", *jt);
//        }
//        puts("");
//        printf("minpos=%d minpos2=%d cmp=%d\n",
//               int(min_element(cycles_sums[i].begin(),
//                               cycles_sums[i].begin() + n + 1) - cycles_sums[i].begin()),
//               int(min_element(cycles_sums[i].begin(),
//                               cycles_sums[i].begin() + 2 * n + 1) - cycles_sums[i].begin()),
//               cycle_min_sum_pos[i]
//        );
//    }

    ll result = LLINF;
    REP (a, n) {
        ll when = compute_when(a);
//        printf("w=%lld\n", when[a]);
        result = min(result, when);
//        printf("r=%lld\n", result);
    }
    if (result == LLINF) {
        puts("-1");
    } else {
        printf("%lld\n", result);
    }
}

int main() {
    //int z; scanf("%d", &z); while(z--)
    one();
}