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
#include <iostream>
#include <vector>
#include <algorithm>
#include <limits>

struct Cycle {
    std::vector<std::vector<int>> nextLower;
    int sum;
    int getCycleMin(int startPos) const {
        int cycleMin = 0, sumOfMoves = 0;
        int move;
        int lastPos = startPos;
        int cycleLen = nextLower.size();
        while (!nextLower[startPos].empty() && sumOfMoves < cycleLen) {
            move = std::min(cycleLen - sumOfMoves, static_cast<int>(nextLower[startPos].size() - 1));
            lastPos = startPos;
            startPos = nextLower[startPos][move];
            int moveLen = startPos - lastPos;
            if (moveLen <= 0) { moveLen += cycleLen; }
            sumOfMoves += moveLen;
            if (sumOfMoves > cycleLen) {
                break;
            }
            cycleMin += 1LL << move;
        }
        return cycleMin;
    }

    int getEndPos(int startPos, int height) const {
        int i = 0;
        while (height > 0) {
            if (height & 1) {
                startPos = nextLower[startPos][i];
            }
            ++i;
            height >>= 1;
        }
        return startPos;
    }
};

int gcd(int n1, int n2) {
    int tmp;
    while (n2 != 0) {
        tmp = n1;
        n1 = n2;
        n2 = tmp % n2;
    }
    return n1;
}

void readInput(int &n, int &m, std::vector<int> &savings, std::vector<int> &automatonCycle) {
    std::cin >> n;
    savings.reserve(n);
    int saving;
    for (int i = 0; i < n; ++i) {
        std::cin >> saving;
        savings.push_back(saving);
    }
    std::cin >> m;
    std::cin.ignore(256, '\n');
    automatonCycle.reserve(m);
    char result;
    for (int i = 0; i < m; ++i) {
        std::cin >> result;
        automatonCycle.push_back(result == 'W' ? 1 : -1);
    }
}

#define NEXT_INDEX(curr) ((curr) + interval - ((curr) + interval >= m) * m)

int main() {
    std::ios_base::sync_with_stdio(false);
    int n, m;
    std::vector<int> savings;
    std::vector<int> automatonCycle;
    readInput(n, m, savings, automatonCycle);
    std::vector<Cycle> cycles;
    int cyclesNum = gcd(n, m);
    int cycleSize = m / cyclesNum;
    cycles.resize(cyclesNum);
    for (auto &cycle : cycles) {
        cycle.nextLower.resize(cycleSize);
    }
    int interval = n % m;
    std::vector<std::vector<int>> _lastSeen;
    _lastSeen.resize(4 * cycleSize + 3);
    std::vector<int> *lastSeen = &_lastSeen[2 * cycleSize + 1];
    std::vector<int> currIndexToStartPos;
    currIndexToStartPos.resize(m);
    for (int i = 0; i < cyclesNum; ++i) {
        for (auto &vec : _lastSeen) { vec.clear(); }
        int currentSum = 0, minValue = 0, maxValue = 0;
        int currentIndex = i;
        bool repeat = false;
        for (int j = 0; j < cycleSize; ++j, currentIndex = NEXT_INDEX(currentIndex)) {
            currIndexToStartPos[currentIndex] = j;
            currentSum += automatonCycle[currentIndex];
            for (auto &oneLarger : lastSeen[currentSum + 1]) {
                cycles[i].nextLower[oneLarger].push_back(j);
            }
            lastSeen[currentSum + 1].clear();
            if (cycles[i].nextLower[j].empty()) {
                lastSeen[currentSum].push_back(j);
            }
            if (j == cycleSize - 1 && !repeat) {
                cycles[i].sum = currentSum;
                repeat = true;
                j = -1;
            }
        }
        bool fixed = false;
        for (int len = 2, logLen = 1; !fixed; len *= 2, ++logLen) {
            fixed = true;
            for (int j = 0; j < cycleSize; ++j) {
                Cycle &c = cycles[i];
                std::vector<int> &nlj = c.nextLower[j];
                if (static_cast<int>(nlj.size()) >= logLen && static_cast<int>(c.nextLower[nlj[logLen - 1]].size()) >= logLen) {
                    int next = nlj[logLen - 1];
                    int dist1 = next - j;
                    if (dist1 <= 0) { dist1 += cycleSize; }
                    int dist2 = c.nextLower[next][logLen - 1] - next;
                    if (dist2 <= 0) { dist2 += cycleSize; }
                    if (dist1 + dist2 <= cycleSize) {
                        nlj.push_back(c.nextLower[next][logLen - 1]);
                        fixed = false;
                    }
                }
            }
        }
    }

    std::vector<long long int> gameTimes;
    gameTimes.resize(n);
    for (int i = 0; i < n; ++i) {
        int cycleNum = i % cyclesNum;
        int startPos = currIndexToStartPos[i % m] - 1;
        if (startPos < 0) { startPos += cycleSize; }
        Cycle &cycle = cycles[cycleNum];
        if (startPos == 999768) {
            int x = 0;
        }
        int cycleMin = cycle.getCycleMin(startPos);
        if (cycle.sum >= 0 && savings[i] > cycleMin) {
            gameTimes[i] = std::numeric_limits<long long int>::max();
            continue;
        }
        if (cycle.sum < 0) {
            int roundsNumber = (savings[i] - cycleMin + (-cycle.sum - 1)) / (-cycle.sum);
            roundsNumber = std::max(roundsNumber, 0);
            gameTimes[i] = static_cast<long long int>(roundsNumber) * cycleSize * n;
            savings[i] += static_cast<long long int>(roundsNumber) * cycle.sum;
        }
        int lastRound = cycle.getEndPos(startPos, savings[i]) - startPos;
        if (lastRound <= 0) { lastRound += cycleSize; }
        gameTimes[i] += i + 1 + n * (static_cast<long long int>(lastRound) - 1);
    }
    long long int minGameTime = *std::min_element(gameTimes.cbegin(), gameTimes.cend());
    if (minGameTime == std::numeric_limits<long long int>::max()) {
        std::cout << -1;
    } else {
        std::cout << minGameTime;
    }
}