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
#include <bits/stdc++.h>

#define REP(i,x) for(uint32 i = 0 ; i < (x) ; i++)

//#define DEBUG_MODE
#ifdef DEBUG_MODE
#define print(x) cout << #x << " = " << x << endl
#define debug(x) x
#else
#define print(x)
#define debug(x)
#endif

#define hash_map      unordered_map
#define hash_multimap unordered_multimap
#define hash_set      unordered_set
#define hash_multiset unordered_multiset

using namespace std;

typedef short int int16;
typedef unsigned short int uint16;
typedef int int32;
typedef unsigned int uint32;
typedef long long int64;
typedef unsigned long long uint64;

typedef pair <int, int> PII;
typedef pair <uint32, uint32> PUU;
typedef pair <uint32, int> PUI;
typedef pair <int, uint32> PIU;
typedef pair <int64, int64> PLL;
typedef pair <int64, int> PLI;
typedef pair <int, int64> PIL;
typedef pair <int, int16> PIS;
typedef pair <int16, int> PSI;

#define NETBEANS
#define gc getchar_unlocked
inline void FScanUint32(uint32& x)
{
#ifndef NETBEANS
    register uint32 c = gc();
    x = 0;
    for(;(c<48 || c>57);c = gc());
    for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
#else
    scanf("%u", &x);
#endif
    
}

inline void FScanUint64(uint64& x)
{
#ifndef NETBEANS
    register uint32 c = gc();
    x = 0;
    for(;(c<48 || c>57);c = gc());
    for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
#else
    scanf("%llu", &x);
#endif
}


 #define pc(x) putchar_unlocked(x);
    inline void FWriteUint64 (uint64& n)
    {
        #ifndef NETBEANS
        uint64 N = n, rev, count = 0;
        rev = N;
        if (N == 0) { pc('0'); pc('\n'); return ;}
        while ((rev % 10) == 0) { count++; rev /= 10;} //obtain the count of the number of 0s
        rev = 0;
        while (N != 0) { rev = (rev<<3) + (rev<<1) + N % 10; N /= 10;}  //store reverse of N in rev
        while (rev != 0) { pc(rev % 10 + '0'); rev /= 10;}
        while (count--) pc('0');
        #else
        printf("%llu", n);
    #endif
    }

    
    uint64 min(uint64 a, uint64 b)
    {
        return((a < b)? a : b );
    }

class Application
{
public:
    inline void Run();
private:
    // Methods
    inline void LoadData();
    inline void Solve();
    
    // Fields
    uint32 people_nr_;
    vector <uint32> money_;
    uint32 pattern_size_;
    vector <bool> pattern_;
    uint32 nww;
};

int main()
{
  //  ios_base::sync_with_stdio(false);
    Application app;
    app.Run();
}

inline void Application::LoadData()
{
    FScanUint32(people_nr_);
    money_.reserve(people_nr_);
    
    uint32 temp;
    REP(i, people_nr_)
    {
        FScanUint32(temp);
        money_.push_back(temp);
    }
    
    FScanUint32(pattern_size_);
    nww =  people_nr_ / __gcd(people_nr_, pattern_size_) * pattern_size_;
   
    pattern_.reserve(nww);
    char t = 0;
    
    while(!(t == 'W' || t == 'P'))
        t = getchar();
        
    pattern_.push_back(t == 'W'? 1 : 0);

    
    REP(i, pattern_size_ - 1)
    {
        t = getchar();
        pattern_.push_back(t == 'W'? 1 : 0);
    }
    
//    for(int i = pattern_size_; i < nww; ++i)
//    {
//        pattern_.push_back(pattern_[i - pattern_size_]);
//    }
}

inline void Application::Solve()
{
    int64 act_bal = 0;
    uint64 num = 0;
    
    for(num = 0; num < nww; ++num)
    {
        
        if(pattern_[num % pattern_size_])
        {
            ++ act_bal;
            ++ money_[num % people_nr_];
        }
        else
        {
            -- act_bal;
            -- money_[num % people_nr_];
            if(money_[num % people_nr_] <= 0)
            {
                cout << num  + 1<< "\n";
                return;
            }
        } 
        //++num;
    }
    
    if(act_bal >= 0)
        cout << -1 << "\n";
    
    while(true)
    {
         ++num;
        if(pattern_[num % pattern_size_])
        {
            ++ act_bal;
            ++ money_[num % people_nr_];
        }
        else
        {
            -- act_bal;
            -- money_[num % people_nr_];
            if(money_[num % people_nr_] <= 0)
            {
                cout << num + 1<< "\n";
                return;
            }
        } 
    }
  
}

inline void Application::Run()
{
    LoadData();
    Solve();
}