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
#include <cstdio>
#include <cassert>
#include <vector>
#include <algorithm>
typedef long long int LL;
const LL INF = 1e18;
const int MAXN = 1000005;

int T[MAXN];
char W[MAXN];
bool visited[MAXN];
int resolved[MAXN];

template<class T>
struct Array {
    int a;
    std::vector<T> data;

    Array(int a, int b, T init = T()): a(a), data(b-a+1, init) {}
    T& operator[](int i) { return data[i-a]; }
};

int GetValue(char c) {
    return c == 'W' ? 1 : -1;
}

int main() {
    int n;
    scanf("%d", &n);

    for (int i = 0; i < n; i++)
        scanf("%d", &T[i]);
    
    int m;
    scanf("%d%s", &m, W);
    
    LL result = INF;
    for (int i = 0; i < m; i++) {
        if (!visited[i]) {
            visited[i] = true;

            int s = GetValue(W[i]);
            std::vector<int> sum(1, s);
            int it = i, min = s, max = s;
            while ((it + n) % m != i) {
                it = (it + n) % m;
                visited[it] = true;
                s += GetValue(W[it]);
                min = std::min(min, s);
                max = std::max(max, s);
                sum.push_back(s);
            }

            int size = sum.size();
            for (int t = 0; t < size; t++) {
                sum.push_back(sum.back() + sum[t] - (t > 0 ? sum[t - 1] : 0));
                min = std::min(min, sum.back());
                max = std::max(max, sum.back());
            }
            for (int t = 0; t < 2 * size; t++)
                sum.push_back(sum[t]);

            /*printf("cykl: ");
            for (int t = 0; t < size; t++)
                printf("%d ", sum[t]);
            printf("\n");*/

            std::vector<int> next(sum.size());
            Array<int> first(min, max, -1), last(min, max, -1);
            for (int t = 0; t < sum.size(); t++) {
                if (first[sum[t]] == -1) {
                    first[sum[t]] = t;
                    last[sum[t]] = t;
                }
                if (last[sum[t]] != -1) {
                    next[last[sum[t]]] = t;
                    last[sum[t]] = t;
                }
            }

            int p = 0, q = i;
            it = i;
            for (int p = 0; p < size; p++) {
                it = q % n;
                if (resolved[it] != i + 1) {
                    resolved[it] = i + 1;
                    //printf("resolved %d\n", it);

                    //printf("poczatek cyklu w %d, q = %d\n", p, q);
                    for (int t = it; t < n; t += m) {
                        //printf("solving %d\n", t);
                        int v = (p > 0 ? sum[p - 1] : 0) - T[t];
                        int whole_sum = sum[size - 1];
                        //printf("whole sum = %d, v = %d\n", whole_sum, v);
                        if (v >= min) {
                            assert(first[v] != -1);
                            LL turn_count = first[v] - p + 1;
                            //printf("latwo, wyszlo %d, v na pozycji %d, p = %d\n", turn_count, first[v], p);
                            result = std::min(result, (LL)(turn_count - 1) * n + t + 1);
                        }
                        else if (whole_sum < 0) {
                            whole_sum *= -1;
                            int gap = min - v;
                            int k = (gap + whole_sum - 1) / whole_sum;
                            int nv = v + k * whole_sum;
                            LL turn_count = (LL)k * size + first[nv] - p + 1;
                            LL res = (turn_count - 1) * n + t + 1;
                            //printf("krece %d razy, starczy %d tur\n", k, turn_count);
                            result = std::min(result, (LL)(turn_count - 1) * n + t + 1);
                        }
                        //printf("result = %Ld\n", result);
                    }
                }
                first[sum[p]] = next[p];
                q = (q + n) % m;
            }
        }
    }

    if (result == INF)
        printf("-1\n");
    else
        printf("%Ld\n", result);
}