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
217
218
219
220
221
222
223
224
225
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

#define FOR(i,b,e) for(int i=(b); i <= (e); ++i)
#define FORD(i,b,e) for(int i=(b); i >= (e); --i)
#define SIZE(c) (int) (c).size()
#define FORE(i,c) FOR(i,0,SIZE(c)-1)
#define PB push_back
#define MP make_pair
#define ST first
#define ND second

using namespace std;

typedef long long int LLI;
typedef pair < int , int > PII;

typedef vector < int > VI;
typedef vector < PII > VP;
typedef vector < VP > VVP;

const int SMALL_INF = 1000000001;
const LLI INF = 1000000000000000001LL;

/*************************************************************************/

struct MinStack
{
    VI S;
    VI M;

    void Push(int x)
    {
        S.PB(x);

        if (M.empty())
            M.PB(x);
        else
            M.PB( min(x, M.back()) );
    }

    int Pop()
    {
        int b = S.back();

        S.pop_back();
        M.pop_back();
        return b;
    }

    int Min()
    {
        if (SIZE(M))
            return (M.back());
        else
            return SMALL_INF;
    }

    bool Empty()
        { return S.empty(); }
};

struct MinQueue
{
    MinStack S1, S2;

    VI Occurence;
    int offset;
    int minVal, maxVal;

    MinQueue(int minVal, int maxVal)
    {
        this->minVal = minVal;
        this->maxVal = maxVal;

        offset = -minVal;
        Occurence.resize(maxVal - minVal + 1, -1);
    }

    void Push(int x, int index)
    {
        S2.Push(x);
        Occurence[x + offset] = index;
    }

    int Pop()
    {
        if (S1.Empty())
            while (!S2.Empty())
                S1.Push(S2.Pop());

        return S1.Pop();
    }

    int Min()
        { return min(S1.Min(), S2.Min()); }

    int FirstOccurence(int val)
    {
        if (val < minVal || val > maxVal)
            return -1;
        else
            return Occurence[val + offset];
    }
};

/*************************************************************************/

LLI solve(LLI step, VI &M, VVP &start)
{
    int m = SIZE(M);

    FOR(i,0,m-1)
        M.PB(M[i]);

    FOR(i,1,2*m-1)
        M[i] += M[i-1];

    LLI sum = M.back() / 2;

    LLI ans = INF;

    MinQueue Queue(-2*m, 2*m);

    FORD(i,2*m-1,m)
        Queue.Push(M[i], i);

    FORD(i,m-1,0)
    {
        Queue.Pop();
        Queue.Push(M[i], i);

        LLI pref = (i ? M[i-1] : 0);
        LLI minDrop = Queue.Min() - pref;

        if (minDrop < 0)
            for (PII boy : start[i])
            {
                LLI money = boy.ND;
                LLI here = boy.ST;

                if (sum < 0)
                {
                    LLI over = max(0LL, money + minDrop);
                    LLI cycles = (over - sum - 1) / (-sum);

                    money += cycles * sum;
                    here += cycles * m * step;
                }

                if (money + minDrop <= 0)
                {
                    int lookingFor = pref - money;
                    int pos = Queue.FirstOccurence(lookingFor);

                    here += step * (pos - i);

                    ans = min(ans, here);
                }
            }
    }

    return ans;
}


/*************************************************************************/

int main()
{
    ios_base::sync_with_stdio(0);

    LLI n;
    cin >> n;

    VI T(n);
    FORE(i,T) cin >> T[i];

    LLI m;
    cin >> m;

    string s;
    cin >> s;

    LLI gcd = __gcd(n, m);

    LLI ans = INF;

    FOR(i,0,gcd-1)
    {
        VI M;
        VVP start;

        /* M - some particular elements of the machine cycle */
        /* start - for each position in M boys that start there */

        int j = i;

        do
        {
            start.PB(VP());

            for (int boy = j; boy < n; boy += m)
                start.back().PB( MP(boy, T[boy]) );

            M.PB( (s[j] == 'W') ? 1 : -1 );

            j = (j + n)%m;
        } while (j != i);

        LLI result = solve(n, M, start);
        ans = min(ans, result);
    }

    if (ans == INF)
        cout << -1;
    else
        cout << 1 + ans;

    return 0;
}

/*************************************************************************/