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
#include <cstdio>
#include <algorithm>
using std::min;
using std::exit;
#define ll long long
const int maxN = 1e6 + 10;
const int inf = 2e9;
const ll infll = 9e18;
int money[maxN];
int min_t[maxN];
// int where_min[maxN];
int val_cycle[maxN];
char result[maxN];
int lowest_boy[maxN];
int his_time[maxN];
int n, m, cycles, cycle_size;

int gcd(int a, int b) { return a ? gcd(b % a, a) : b; }

struct P {
    int val, i;

    P(int Val = 0, int I = 0) : val(Val), i(I) {}
};

struct Q {
    static P t[];

    int beg, end;
    Q() : beg(0), end(0) {}

    void push(int val, int i) {
        while(beg != end) {
            if(t[end - 1].val >= val) end = (end + maxN - 1) % maxN;
            else break;
        }
        t[end++] = { val, i };
        end %= maxN;
    }

    P& min() {
        return t[beg];
    }

    void reduce(int i) {
//         printf("reducing to %d...\n", i);
        while(beg != end) {
            if(t[beg].i < i) beg = (beg + 1) % maxN;
            else break;
        }
    }

    void print() {
        printf("queue:");
        for(int i = beg; i < end; ++i)
            printf(" { %d, %d }", t[i].val, t[i].i);
        printf("\n");
    }

    P& to_zero(int val, int val0) {
//         if(val + min().val - val0 > 0 || beg == end) {
//             printf("sorry...\n");
//             printf("try zero %d -%d\n", val, val0);
//             print();
//             exit(0);
//         }
        int a = beg, b = end, avg;
        while(b - a - 1) {
            avg = (a + b) >> 1;
            if(val + t[avg].val - val0 <= 0)
                a = avg;
            else b = avg;
        }
        return t[a];
    }
};
P Q::t[maxN];

ll res = infll;
void count(int xx) {
    Q q;
    int step = m - n % m;
    int time = 0, pos = xx, val = 0;
    for(int i = 0; i < cycle_size; ++i) {
        q.push(val, time);
        val_cycle[pos] = val;
//         q.print();
        
        pos = (pos + step) % m;
        val += result[pos] == 'W' ? -1 : 1;
        time++;
    }
    for(int i = 0; i < cycle_size; ++i) {
        q.push(val, time);
//         q.print();
        q.reduce(time - cycle_size);
//         q.print();
        min_t[pos] = q.min().val - val;
//         where_min[pos] = time - q.min().i;
        val_cycle[pos] -= val;
//         printf("\033[32m%d: pos: %d val: %d min: %d where: %d val_cycle: %d\033[39m\n", time, pos, val, min_t[pos], where_min[pos], val_cycle[pos]);

        if(lowest_boy[pos] != inf) {
            if(val_cycle[pos] >= 0) {
                if(lowest_boy[pos] + min_t[pos] <= 0) {
                    res = min(res, his_time[pos] + 1 + (time - q.to_zero(lowest_boy[pos], val).i - 1) * (ll) n);
                }
            } else {
//             printf("lowest_boy: %d his_time: %d\n", lowest_boy[pos], his_time[pos]);
                int boy_cycles = 0;
                if(lowest_boy[pos] + min_t[pos] > 0)
                    boy_cycles = (lowest_boy[pos] + min_t[pos]) / (-val_cycle[pos]);
                lowest_boy[pos] += boy_cycles * val_cycle[pos];
                while(lowest_boy[pos] + min_t[pos] > 0) {
                    boy_cycles++;
                    lowest_boy[pos] += val_cycle[pos];
                }
//             printf("to 0 needs %d cycles\n", boy_cycles);
//             printf("and %d steps\n", time - q.to_zero(lowest_boy[pos], val).i);
//             printf("time: %lld\n", his_time[pos] + 1 + (time - q.to_zero(lowest_boy[pos], val).i -1 + boy_cycles * (ll) cycle_size) * (ll)n);
                res = min(res, his_time[pos] + 1 + (time - q.to_zero(lowest_boy[pos], val).i -1 + boy_cycles * (ll) cycle_size) * (ll)n);
            }
        }
        pos = (pos + step) % m;
        val += result[pos] == 'W' ? -1 : 1;
        time++;
    }
}


int main() {
    scanf("%d", &n);
    for(int i = 0; i < n; ++i)
        scanf("%d", money + i);
    scanf("%d%s", &m, result);
    cycles = gcd(n, m);
    cycle_size = m / cycles;

    for(int i = 0; i < m; ++i)
        lowest_boy[i] = inf;
    for(int i = 0; i < n; ++i) {
        if(lowest_boy[i % m] > money[i]) {
            lowest_boy[i % m] = money[i];
            his_time[i % m] = i;
        }
    }

    for(int i = 0; i < cycles; ++i)
        count(i);

    if(res == infll)
        res = -1;
    printf("%lld\n", res);
    return 0;
}