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
#define debug_printf(fmt, ...)
//#define debug_printf(fmt, ...) do { printf(fmt, __VA_ARGS__); } while (0)

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cassert>
#include <vector>
#include <set>
#include <map>
#include <deque>
#include <algorithm>
#include <limits>
#include <iostream>

const long long LLMAX = (std::numeric_limits<long long>()).max();
const long long LLMIN = (std::numeric_limits<long long>()).min();

const int N_max = 1000000;
const int M_max = 1000000;

int amounts[N_max];
char cycle[M_max + 1];

bool unavailable_positions[M_max];

struct SubCycle {
    std::vector<int> cycle_indices;
    std::map<int, int> cycle_indices_map;
    std::vector<int> cum_neg_indices;
    int total;
};

SubCycle sub_cycles[M_max];

int main()
{
    std::ios_base::sync_with_stdio(false);

    int N; scanf("%d", &N);
    for (int i=0; i < N; ++i) {
        int a; scanf("%d", &a);
        amounts[i] = a;
    }
    int M; scanf("%d", &M);
    scanf("%s", cycle);
    for (int i=0; i < M; ++i) {
        cycle[i] = (cycle[i] == 'W') ? 1 : -1;
    }

    bool negative_found = false;
    int step = N % M;
    int sub_cycles_len = INT_MAX;
    for (int start=0; !unavailable_positions[start]; ++start) {
        SubCycle& sub_cycle = sub_cycles[start];
        int threshold = 0;
        for (int curr=-1; curr != start; curr=(curr + step) % M) {
            if (curr == -1) {
                curr = start;
            } else if (start == 0 && sub_cycles_len > curr) {
                sub_cycles_len = curr;
            }
            sub_cycle.cycle_indices.push_back(curr);
            if (curr < N) {
                sub_cycle.cycle_indices_map[curr] = sub_cycle.cycle_indices.size() - 1;
            }
            sub_cycle.total += cycle[curr];
            unavailable_positions[curr] = true;
            if (threshold > sub_cycle.total) {
                negative_found = true;
                sub_cycle.cum_neg_indices.push_back(sub_cycle.cycle_indices.size() - 1);
                threshold = sub_cycle.total;
            }
        }
    }

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

    long long lowest_game_count = LLMAX;

    for (int i=0; i < N; ++i) {
        bool done = false;

        int sub_cycle_idx = i % sub_cycles_len;
        SubCycle& sub_cycle = sub_cycles[sub_cycle_idx];

        if (sub_cycle.cum_neg_indices.size() == 0) {
            continue;
        }

        std::vector<int>::iterator cycle_indices_it = sub_cycle.cycle_indices.begin() + sub_cycle.cycle_indices_map[i % M];

        long long game_count = i + 1;
        long long amount = amounts[i];
        while (cycle_indices_it != sub_cycle.cycle_indices.begin() && !done) {
            amount += cycle[*cycle_indices_it];
            if (amount == 0) {
                done = true;
                break;
            }

            ++cycle_indices_it;
            if (cycle_indices_it == sub_cycle.cycle_indices.end()) { cycle_indices_it = sub_cycle.cycle_indices.begin(); }
            game_count += N;
        }

        if (!done) {
            if (amount > sub_cycle.cum_neg_indices.size()) {
                if (sub_cycle.total >= 0) {
                    continue;
                }

                int positive_total = -sub_cycle.total;
                int full_cycle_count = (amount - sub_cycle.cum_neg_indices.size()) / positive_total;
                int remainder = (amount - sub_cycle.cum_neg_indices.size()) % positive_total;
                if (remainder != 0) {
                    ++full_cycle_count;
                }

                amount -= ((long long) full_cycle_count) * positive_total;
                game_count += ((long long) full_cycle_count) * sub_cycle.cycle_indices.size() * N;
            }

            int idx = sub_cycle.cum_neg_indices[amount - 1];
            game_count += ((long long) idx) * N;
        }

        if (lowest_game_count > game_count) {
            lowest_game_count = game_count;
        }
    }

    if (lowest_game_count == LLMAX) {
        printf("-1\n");
        return 0;
    }

    printf("%lld\n", lowest_game_count);
}