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
226
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

const int INFINITY = 1000000000;
const long long LINFINITY = INFINITY;

int cash[1000000];
int cycle[1000000];

int Pcash[1000000];
int Pcycle[1000000];

int RcycleMap[1000000]; // maps from index in reverse cycle to normal index
int PcycleMap[1000000]; // maps from normal index to rcycle index
int Rcycle[1000000];
int RcycleMin[1000000];

long long Diff[1000000];
long long Shift[1000000];

int minEpoch[1000000];

int NWD(int a, int b)
{
    if(b!=0)
      return NWD(b,a%b);
 
    return a;
}

int FingBestFromCurrent(int n, int m, int im)
{
   int best = RcycleMap[im];
   int Value = Pcash[best];
   
   for(int i = RcycleMap[im]; i < n; i += m)
   {
      if(Pcash[i] < Value)
      {
         best = i;
         Value = Pcash[i];
      }
   }
   
   return best;
}

long long Process(int n, int m)
{
   //rewerse cycle for single player
   int change = 0;
   int minvalue = 0;
   int minimalEpoch = INFINITY;
   for(int i = 0, s = 0; i < m; i++)
   {
      RcycleMap[i] = s;
      PcycleMap[s] = i;
      Rcycle[i] = Pcycle[s];
      change += Rcycle[i];
      if(change < minvalue)
         minvalue = change;
      
      s = (s + n) % m;
   }
   
   //calculate min for different starting round
   for(int i = 0; i < m; i++)
   {
      RcycleMin[i] = minvalue;
      minvalue += Rcycle[i];
      if(minvalue > change)
         minvalue = change;
   }
   
   for(int i = 0; i < n && i < m; i++)
   {
      int val = Pcash[i];
      int idx = i;
      for(int j = i + m; j < n; j+= m)
      {
         if(Pcash[j] < val)
         {
            minEpoch[idx] = INFINITY;
            val = Pcash[j];
            idx = j;
         }
         else
         {
            minEpoch[j] = INFINITY;
         }
      }
      int Ri = PcycleMap[idx % m];
      int min = RcycleMin[Ri];
      
      
      if(val + min < 0)
         minimalEpoch = 0;
      else
      {
         int mE = (val + min) / (-change) + 1;
         
         if(val + change * mE == 0)
            mE--;
         
         if(change >= 0 && mE != 0)
            mE = INFINITY;
            
         minEpoch[idx] = mE;
         
         if(mE < minimalEpoch)
            minimalEpoch = mE;
      }
   }
   
   if(minimalEpoch == INFINITY)
   {
      return -1;
   }
   
   long long minResult = LINFINITY * LINFINITY;
   
   for(int i = 0; i < n; i++)
   {
      if(minEpoch[i] != minimalEpoch)
         continue;
         
      int cash = Pcash[i] + change * minimalEpoch;
      int Ri = PcycleMap[i % m];
      bool bFound = false;
      
      for(int j = 0; j < m; j++)
      {
         cash += Rcycle[Ri];
         
         if(cash == 0)
         {
            long long result = minimalEpoch * n * m + j * n + i;
            if(result < minResult)
               minResult = result;
            
            bFound = true;
            break;
         }
         
         Ri = (Ri + 1) % m;
      }
//      if(bFound == false)
//         cout<<"something wrong, i = "<<i<<endl;
   }
   
   if(minResult == LINFINITY * LINFINITY)
      return -1;
   else
      return minResult;
/*   
   //we are sure that if solution exist than we can find them i single cycle
   int currentValue = Pcash[0];
   long long currentDiff = 0;
   long long currentShift = 0;
   for(int im = 0; im < m; im++)
   {
      int globalShift = im * n;
      //we need to find best element which start procees from current cycle element
      int in = FingBestFromCurrent(n, m, im);
      
      Diff[im] = ;
      Shift[im] = in;
      
      if(Pcash[in] <= currentValue)
      {
         
      }
   }*/
}

int main(int argc, char *argv[])
{
   ios_base::sync_with_stdio(0);
   
   int n, m;
   cin >> n;
   for(int i = 0; i < n; i++)
   {
      cin >> cash[i];
   }
   
   cin >> m;
   
   string c;
   cin >> c;
   for(int i = 0; i < m; i++)
   {
      if(c[i] == 'W')
         cycle[i] = 1;
      else
         cycle[i] = -1;
   }
   
   int nwd = NWD(n,m);
   int Pn = n / nwd;
   int Pm = m / nwd;
   long long min = -1;
   for(int i = 0; i < nwd; i++)
   {
      for(int j = 0; j < Pn; j++)
      {
         Pcash[j] = cash[j * nwd + i];
      }
      for(int j = 0; j < Pm; j++)
      {
         Pcycle[j] = cycle[j * nwd + i];
      }
      
      long long result = Process(n, m);
      result = result * nwd + i;
      if(result != -1 && (result < min || min == -1))
         min = result;
   }
   if(min == -1)
      cout<<-1;
   else
      cout<<min + 1;
}