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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <cstdio>
#include <vector>
#include <utility>

std::vector<long long> kasa;
std::vector<long long> patt;

std::vector<std::pair<long long,long long> > patt2cyc;

struct result {
    result(long long a, long long b)
      : gdzie(a)
      , ile(b)
    {}
    long long gdzie;
    long long ile;
};

struct node {
    long long sum;
    long long min;
};

struct cycle {
    std::vector<long long> desc;
    std::vector<node> drzewo;
    long long all;
    long long min;
};

std::vector<cycle> cycVector;

void czytaj() {
    long long n;
    scanf("%lld",&n);
    kasa.resize(n);
    for (long long i=0; i<n; ++i) {
        scanf("%lld", &kasa[i]);
    }

    scanf("%lld",&n);
    std::vector<char> x;
    x.resize(n+10);
    scanf("%s", &x[0]);
    patt.resize(n);

    for (long long i=0; i<n; ++i) {
        if (x[i]=='W')
            patt[i] = 1;
        else
            patt[i] = -1;
    }
}

void obliczDrzewo(cycle &cc);

void findCyc() {
    patt2cyc.resize(patt.size(), std::make_pair<long long,long long>(-1,-1));

    long long ps = patt.size();
    long long sk = kasa.size();

    for (long long i=0,cId=0; i<patt.size(); ++i) {
        if (patt2cyc[i].first != -1)
            continue;

        cycle c;
        long long csum = 0;
        long long min = 1000000000000;
        for (long long j=i, pos=0; patt2cyc[j].first == -1; ++pos, j = (j+sk)%ps) {
            patt2cyc[j].first = cId;
            patt2cyc[j].second = pos;
            c.desc.push_back(patt[j]);
            csum += patt[j];
            if (csum < min)
                min = csum;
        }
        c.all = csum;
        c.min = -min;
        cycVector.push_back(c);
        ++cId;
    }

    for (long long i=0; i <cycVector.size(); ++i)
        obliczDrzewo(cycVector[i]);
}

void printCyc() {
    printf("==== CYC START ====\n");
    for (long long i=0; i<cycVector.size(); ++i) {
        printf("cyc %lld size:%lld\n", i, (long long)cycVector[i].desc.size());
        for (long long j=0; j<cycVector[i].desc.size(); ++j)
           printf(" %2lld", cycVector[i].desc[j]);
        printf("\n all: %lld\n min: %lld\n", cycVector[i].all, cycVector[i].min);
    }
    printf("==== CYC END ====\n");

}

void obliczDrzewo(cycle &cc, long long index, long long a, long long b) {
    if (a == b) {
        cc.drzewo[index].sum = cc.desc[a];
        cc.drzewo[index].min = cc.desc[a] > 0 ? 0 : cc.desc[a];
        return;
    }

    long long c = (a+b)/2;
    long long lewy = index*2;
    long long prawy = lewy+1;

    obliczDrzewo(cc, lewy, a, c);
    obliczDrzewo(cc, prawy, c+1, b);
    cc.drzewo[index].sum = cc.drzewo[lewy].sum + cc.drzewo[prawy].sum;
    cc.drzewo[index].min = cc.drzewo[lewy].sum + cc.drzewo[prawy].min;
}

void obliczDrzewo(cycle &c) {
    c.drzewo.resize(c.desc.size()*3);
    obliczDrzewo(c, 1, 0, c.desc.size()-1);
}

result jakDaleko(cycle &cc, long long index, long long a, long long b, long long gdzie, long long kas) {
    if (a == b)
        return result(b, kas + cc.desc[b]);

    if (a == gdzie && kas > (-cc.drzewo[index].min))
        return result(b, kas + cc.drzewo[index].sum);

    long long c = (a+b)/2;
    long long lewy = index*2;
    long long prawy = lewy+1;

    if (gdzie <= c) {
        result res = jakDaleko(cc, lewy, a, c, gdzie, kas);
        if (res.ile <= 0)
            return res;
        return jakDaleko(cc, prawy, c+1, b, c+1, res.ile);
    }

    return jakDaleko(cc, prawy, c+1, b, gdzie, kas);
}

//long long rozwiaz(long long i) {
//    cycle &c = cycVector[patt2cyc[i%patt.size()].first];
//    long long start = patt2cyc[i%patt.size()].second;
//
//    result res = jakDaleko(c, 1, 0, c.desc.size()-1, start, kasa[i]);
//
//    if (res.ile == 0)
//        return (res.gdzie - start) * kasa.size() + (i+1);
//
//    if (res.ile > c.min && c.all >= 0)
//        return -1;
//
//    long long tury = 0;
//
//    if (res.ile > c.min) {
//        tury = res.ile/(-c.all);
//        res.ile -= tury*(-c.all);
//    }
//
//    result res2 = jakDaleko(c, 1, 0, c.desc.size()-1, 0, res.ile);
//
//    return ((res.gdzie - start + res2.gdzie + tury +1)*kasa.size()) + (i+1);
//}

long long rozwiaz(long long i) {
    long long ret = i+1;
    cycle &c = cycVector[patt2cyc[i%patt.size()].first];
    long long start = patt2cyc[i%patt.size()].second;

    result res = jakDaleko(c, 1, 0, c.desc.size()-1, start, kasa[i]);

    ret += (res.gdzie - start) * kasa.size();

    if (res.ile == 0)
        return ret;

    if (res.ile > c.min && c.all >= 0)
        return -1;

    long long tury = 0;
    if (res.ile > c.min) {
        tury = (res.ile - c.min)/(-c.all) + 1;
        res.ile -= tury*(-c.all);
    }
    ret += tury*kasa.size()*c.desc.size();

    if (res.ile > 0) {
        result res2 = jakDaleko(c, 1, 0, c.desc.size()-1, 0, res.ile);
        ret += (res2.gdzie+1)*kasa.size();
    }

    return ret;
}

int main() {
    czytaj();
    findCyc();
//    printCyc();
    long long min = 1000000000000000000;
    bool find = false;
    for (long long i=0; i<kasa.size(); ++i) {
        long long roz = rozwiaz(i);
        if (roz==-1)
            continue;
        find = true;
        if(roz < min)
            min = roz;
    }
    if (find)
        printf("%lld\n", min);
    else
        printf("-1\n");
    return 0;
}