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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//============================================================================
// Name        : .cpp
// Author      : Grzegorz Gajoch
// Description : 
//============================================================================

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <ctype.h>
#include <algorithm>
#include <string>
#include <string.h>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <list>

using namespace std;

#define FOR(i,a,b) for(int i = a; i <= b; ++i)
#define FORE(i,a,b) for(int i = a; i < b; ++i)
#define FORD(i,a,b) for(int i = a; i >= b; --i)
#define REP(x, n) for(int x=0; x < (n); ++x)
#define VAR(v,i) __typeof(i) v=(i)
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define SIZE(n) (int)n.size()
#define ALL(c) c.begin(),c.end()
#define PB(n) push_back(n)
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<bool> VB;
#define MP make_pair
#define ST first
#define ND second
const int INF = 1000000001;

#undef BRUT

#undef DEBUG
#define debug2 //printf
#ifdef DEBUG
    #define debug printf
#else
    #define debug //
#endif

class MinSegmentTree_ {
private:
    inline void update(int k, int val) {
        A[k] = val;
        while (k > 0) {
            k /= 2;
            A[k] = min(A[2 * k], A[2 * k + 1]);
        }
    }
    inline int mini(int p, int q) {
        int tmp1 = INF, tmp2 = INF;
        if (p == q) return A[p];
        if (q < p) return INF;
        if ((p) % 2 != 0) {
            tmp1 = A[p];
            ++p;
        }
        if ((q) % 2 == 0) {
            tmp2 = A[q];
            --q;
        }
        return min(mini((p / 2), (q / 2)), min(tmp1, tmp2));
    }
public:
    int A[8*1048576];
    int length;
    void len(int n) {
        length = 1;
        while( length < n )
            length *= 2;
        FOR(i, 0, 2*length) {
            A[i] = INF;
        }
    }
    void update_val(int pos, int val) {
        update(pos + this->length, val);
    }
    void updateAll() {
        for(int i = length-1; i >= 0; --i) {
            A[i] = min(A[2 * i], A[2 * i + 1]);
        }
    }
    int mini_val(int start, int end) {
        return mini(start + this->length, end + this->length);
    }
    int findFirstLessEqual(int start, int end, int val) {
        debug("find: (%d %d) | %d\n",start,end, val);
        while( start != end ) {
            int c = (start + end) / 2;
            debug("find: (%d %d) | %d\n",start,end, val);
            if (this->mini_val(start, c) <= val) {
                end = c;
            } else {
                start = c+1;
            }
        }
        return start;
    }
};
static MinSegmentTree_ MinSegmentTree;

int N, M, C_LEN;
char machine[1000001];
vector<int> persons, personsIn;
vector<bool> machineIn; //false - -1; true - +1


int MachineIndexToValue(int nr) {
    nr %= M;
    if( machineIn[nr] ) {
        return 1;
    } else {
        return -1;
    }
}
unsigned int gcd (unsigned int n1, unsigned int n2) {
    unsigned int tmp;
    while (n2 != 0) {
        tmp = n1;
        n1 = n2;
        n2 = tmp % n2;
    }
    return n1;
}

void read() {
    scanf("%d", &N);
    //personsIn.resize(N);
    int val;
    REP(i, N) {
        scanf("%d", &val);
        personsIn.push_back(val);
    }
    scanf("%d", &M);
    C_LEN = M/gcd((unsigned)N, (unsigned)M);
    scanf("%s", machine);
    REP(i, M) {
        if( machine[i] == 'W' ) {
            machineIn.push_back(true);
        } else {
            machineIn.push_back(false);
        }
    }
}

struct Node {
    int number;
    int value;
    int min_cyc;
    Node() {
        number = -1;
        value = -1;
        min_cyc = -INF;
    }
};

class Cycle {
public:
    vector<Node> vec;
    vector<int> PersonsBelongs;
    int sum;
    Cycle() {
        vec.clear();
        sum = 0;
    }
    int add(int number) {
        Node now;
        now.number = number;
        this->vec.push_back(now);
        return ((int)this->vec.size()-1);
    }
    void print() {
#ifdef DEBUG
        debug("cycle: ");
        FOREACH(it, this->vec) {
            debug("(%d %d %d) ", it->number, it->value, it->min_cyc);
        }
        debug("\n");
#endif
    }

    void calc_values() {
        vec[0].value = 0;
        Node tmp;
        REP(i, C_LEN) {
            tmp.number = vec[i].number;
            vec.push_back(tmp);
        }
        tmp.number = vec[0].number;
        vec.push_back(tmp);
        FORE(i, 1, 2*C_LEN+1) {
            vec[i].value = vec[i-1].value + MachineIndexToValue(vec[i-1].number);
            debug("%d (%d): %d -> | %d | -> %d\n",i, vec[i-1].number, vec[i-1].value, MachineIndexToValue(vec[i-1].number), vec[i].value);
        }

        this->sum = vec[C_LEN].value;
    }
    void calc_mins_running() {
        debug("calculate running min!\n");
        multiset<int> tmp;
        FOR(i, 0, C_LEN) {
            tmp.insert(vec[i].value);
        }
#ifdef DEBUG
        debug("multiset contents: ");
        FOREACH(it, tmp) {
            debug("%d ", *it);
        }
        debug("\n");
#endif
        REP(i, C_LEN) {
            vec[i].min_cyc = vec[i].value - *(tmp.begin());
            debug("now: %d, min: %d -> %d\n", vec[i].value, *(tmp.begin()), vec[i].min_cyc);
            debug("multiset contents: ");
#ifdef DEBUG
            FOREACH(it, tmp) {
                debug("%d ", *it);
            }
#endif
            debug("\n");
            debug("removing %d, adding %d\n",vec[i].value, vec[i+C_LEN+1].value);
            debug("\n\n");
            //tmp.erase(vec[i].value);
            tmp.erase(tmp.find(vec[i].value));
            tmp.insert(vec[i+C_LEN+1].value);
        }
    }
};


vector<Cycle> cyc_list;

struct PointerToPosInCycle {
    int cyc_nr;
    int position;
};


vector<PointerToPosInCycle> pointers;
vector<int> vis;

PointerToPosInCycle get_position_in_cycle(int n_) {
    return (pointers[n_ % M]);
}

void resize() {
    vis.resize(M, false);
    pointers.resize(M);
}

int ceil(int x, int y) { //ceil(x/y)
    return ((x % y) ? ((int)(x / y + 1)) : ((int)(x / y)));
}
int floor(int x, int y) { //ceil(x/y)
    return (x/y);//((x % y) ? ((int)(x / y - 1)) : ((int)(x / y)));
}

priority_queue< pair<int, int> > N_sort_by_cycle;
int main(int argc, char **argv) {
    read();
    debug("machine: ");
#ifdef DEBUG
    REP(i, M) {
        debug("%d, ", (machineIn[i])?1:-1);
    }
    debug("\n");
#endif
    resize();
    debug("C_LEN = %d\n",C_LEN);

    int cnt = 0;
    Cycle cycle_tmp;
    cyc_list.push_back(cycle_tmp);
    REP(now, M) {
        int it = now;
        debug("---%d--\n",it);

        bool visited = false;
        while( vis[it] == false ) {
            visited = true;
            int nr = cyc_list[cnt].add(it);
            debug("visit %d\n",it);
            vis[it] = true;

            debug("adding %d -> (%d %d)\n",it, cnt, nr);
            PointerToPosInCycle tmp;
            tmp.cyc_nr = cnt;
            tmp.position = nr;
            pointers[it] = tmp;

            it += N;
            it %= M;
        }
        if( visited ) {//cyc_list[cnt].vec.size() > 0 ) {
            cyc_list.push_back(cycle_tmp);
            cnt++;
        }
    }
    cyc_list.pop_back();

    FOREACH(it, cyc_list) {
        it->calc_values();
        it->calc_mins_running();
    }
#ifdef DEBUG
    FOREACH(it, cyc_list) {

        it->print();
        debug("sum: %d\n", it->sum);
    }
    REP(i,N) {
        debug("%d -> (%d %d)\n",i, get_position_in_cycle(i).cyc_nr, get_position_in_cycle(i).position);
    }
#endif

    int full_cycles = INF;
    bool all_positive = true;
    REP(i,N) {
        int val = personsIn[i];
        PointerToPosInCycle ptr = get_position_in_cycle(i);
        int minv = abs(cyc_list[ptr.cyc_nr].vec[ptr.position].min_cyc);
        int sum = cyc_list[ptr.cyc_nr].sum;
        debug("person %d: ",i);
        debug("start %d, sum %d, min_val %d\n",personsIn[i], sum, minv);
        N_sort_by_cycle.push(MP(get_position_in_cycle(i).cyc_nr, i));
        if( val <= minv ) {
            full_cycles = 0;
            all_positive = false;
        } else if( sum < 0 ) {
            all_positive = false;
            int up = val-minv;
            int down = -sum;
            int tmp;
            if( up % down == 0 ) {
                tmp = up/down;
            } else {
                tmp = (int)(up / down + 1);
            }
            full_cycles = min(full_cycles, max(0, tmp));
        }
    }
    if( full_cycles == INF ) {
        full_cycles = 0;
    }

    if( all_positive ) {
        printf("-1\n");
        return 0;
    }

    ULL counter = (ULL)full_cycles*(ULL)N*(ULL)C_LEN;
    debug2("doing %d full cycles! -> %d moves, counter %llu\n", full_cycles, N*C_LEN, counter );
    REP(i, N) {
        int sum = cyc_list[get_position_in_cycle(i).cyc_nr].sum;
        personsIn[i] += full_cycles*sum;
    }
#ifdef DEBUG
    debug("afer changes: ");
    REP(i, N) {
        debug("%d ", personsIn[i]);
    }
    debug("\n");
#endif


// BRUT------------------------------------------------------------------
#ifdef BRUT
    persons = personsIn;


    int pers = 0;
    int position = 0;
    unsigned last_count = N*M/gcd(N, M);
    bool increasing = false;
    while(true) {
        counter++;
        if( machineIn[position] ) {
            persons[pers]++;
        } else {
            persons[pers]--;
            if( persons[pers] == 0 ) {
                break;
            }
        }

        if( counter == last_count ) {
            printf("FULL_CYCLE!\n");
            increasing = true;
            REP(i, N) {
                if( persons[i] < personsIn[i] ) {
                    increasing = false;
                }
            }
            if( increasing ) {
                break;
            }
        }
        pers++; pers %= N;
        position++; position %= M;
    }
    if( increasing ) {
        printf("-1\n");
    } else {
        printf("%d\n", counter);
    }
    return 0;
#else
// BRUT------------------------------------------------------------------
    /*REP(i, N) {
        //N_sort_by_cycle.insert(MP(get_position_in_cycle(i).cyc_nr, i));
        N_sort_by_cycle.push(MP(get_position_in_cycle(i).cyc_nr, i));
    }*/
#ifdef DEBUG
    debug("SORTED\n");
    FOREACH(it, N_sort_by_cycle) {
        printf("(%d in cycle %d)\n",it->second,it->first);
    }
#endif
    int last_cycle = -1;
    pair<int, int> min_finisz = MP(INF, -1);
    while( !N_sort_by_cycle.empty() ) {
        pair<int, int> now = N_sort_by_cycle.top(); //*N_sort_by_cycle.begin();
        //N_sort_by_cycle.erase(N_sort_by_cycle.begin());
        N_sort_by_cycle.pop();

        int cyc_nr = now.first;
        int pers = now.second;
        if( last_cycle != cyc_nr ) {
            last_cycle = cyc_nr;
            debug2("update tree, len: %lu\n", cyc_list[cyc_nr].vec.size());
            MinSegmentTree.len(cyc_list[cyc_nr].vec.size());
            int i = 0;
            /*FOREACH(it, cyc_list[cyc_nr].vec) {
                debug("update %d = %d\n", i, it->value);
                MinSegmentTree.update_val(i++, it->value);
            }*/
            FOREACH(it, cyc_list[cyc_nr].vec) {
                MinSegmentTree.A[MinSegmentTree.length+(i++)] = it->value;
            }
            MinSegmentTree.updateAll();
        }
        int pos = get_position_in_cycle(pers).position;
        debug("person %d, position %d\n",pers,pos);

        int search = -personsIn[pers]+cyc_list[cyc_nr].vec[pos].value;
        //if( MinSegmentTree.mini_val(pos, pos+C_LEN) )

        int finisz = MinSegmentTree.findFirstLessEqual(pos, pos+C_LEN+1, search);
        if( finisz == pos+C_LEN+1 ) { //not found
            debug("%d not finiszes !\n",pers);
        } else {
            finisz -= pos;
            min_finisz = min(min_finisz, MP(finisz, pers));
            debug("%d finiszes in %d moves\n", pers, finisz);
        }
    }
    debug("min_finisz: pers: %d, moves: %d\n",min_finisz.second, min_finisz.first);

    counter += (ULL)(min_finisz.first-1) * (ULL)N;
    counter += min_finisz.second+1;
    printf("%llu\n",counter);

#endif
    return 0;
}