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

unsigned long long solve(std::vector<unsigned>&& coins, std::string&& wins, std::vector<unsigned> const& idx, unsigned real_n) {
    unsigned n = coins.size();
    wins += wins;

    // step 1
    std::vector<int> step {0};
    for(unsigned i = 0; i < 2 * n; i++)
        step.push_back(step.back() + (wins[i] == 'W' ? +1 : -1));

    //std::cerr << "STEP: ";
    //for(auto x: step) std::cerr << x << " ";
    //std::cerr << std::endl;

    // step 2
    std::vector<int> suf_min {std::numeric_limits<int>::max()};
    std::vector<int> pref_min = suf_min;

    for(unsigned i = 0; i < n; i++)
        suf_min.push_back(std::min(suf_min.back(), step[n - i]));
    for(unsigned i = 0; i < n; i++)
        pref_min.push_back(std::min(pref_min.back(), step[n + 1 + i]));

    //step 3
    std::vector<unsigned> need(n);
    for(unsigned i = 0; i < n; i++) {
        int tmp = step[i] - std::min(suf_min[n-i], pref_min[i]);
        if(tmp < 0) tmp = 0;

        need[i] = tmp;
    }

    //std::cerr << "NEED: ";
    //for(auto x: need) std::cerr << x << " ";
    //std::cerr << std::endl;

    //step 3.14
    int one_cycle = step.back() / 2;
    //std::cerr << "one " << one_cycle << std::endl;
    //step 4
    std::vector<unsigned long long> res(n, std::numeric_limits<unsigned long long>::max());
    std::vector<bool> valid(n);
    for(unsigned i = 0; i < n; i++) {
        if(coins[i] == 0) continue;
        valid[i] = true;
        unsigned & c = coins[i];

        if(one_cycle >= 0 && c > need[i]) {
            valid[i] = false;
            continue;
        }
        res[i] = 0;

        if(one_cycle < 0 && c > need[i]) {
            unsigned long long cycles = (c - need[i] + -one_cycle - 1) / -one_cycle;
            res[i] += cycles * n;
            c -= cycles * (-one_cycle);
        }

        //std::cerr << "S4 " << i << " " << c << " " << res[i] << std::endl;
    }

    //step 5
    int minimum = *min_element(std::begin(step), std::end(step));
    for(auto& x : step) x -= minimum;
    int maximum = *max_element(std::begin(step), std::end(step));

    //step 6
    std::vector<std::vector<unsigned>>indices(maximum+1);

    for(unsigned i=0; i<=2*n; i++) {
        if(i < n && valid[i]) indices[step[i] - coins[i]].push_back(i);
        std::vector<unsigned> & vec = indices[step[i]];
        while(!vec.empty()) {
            unsigned x = vec.back();

            res[x] += i - x;
            //std::cerr << "REST " << x << " is " << i - x << std::endl;

            vec.pop_back();
        }
    }

    //step 7

    unsigned long long result = std::numeric_limits<unsigned long long>::max();
    for(unsigned i=0; i<n; i++) {
        if(!valid[i]) continue;

        result = std::min(result, (res[i]-1) * real_n + idx[i] + 1);
        //std::cerr << i << ": " << res[i] << " " << idx[i] << " " <<  (res[i]-1) * real_n + idx[i] + 1 << std::endl;
    }

    return result;
}

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

    unsigned n, m;
    std::vector<unsigned> coins;

    std::cin >> n;
    coins.reserve(n);
    for(unsigned i = 1; i <= n; i++) {
        unsigned x;
        std::cin >> x;

        coins.push_back(x);
    }

    std::cin >> m;
    std::string tmp;
    std::cin >> tmp;

    while(tmp.length() < n) tmp += tmp;

    m = tmp.length();

    unsigned g = std::__gcd(n, m);

    unsigned long long result = std::numeric_limits<unsigned long long>::max();

    for(unsigned i = 0; i < g; i++) {
        std::vector<unsigned> vec;
        std::string win;
        std::vector<unsigned> idx;

        unsigned j = i;
        do {
            vec.push_back(j < n ? coins[j] : 0);
            win.push_back(tmp[j]);
            idx.push_back(j);
            j = (j + n) % m;
        } while(j >= g);

        result = std::min(result, solve(std::move(vec), std::move(win), std::move(idx), n));
    }

    if(result == std::numeric_limits<unsigned long long>::max()) {
        std::cout << "-1" << std::endl;
    } else {
        std::cout << result << std::endl;
    }
}