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
#include <algorithm>
#include <cassert>
#include <cstdio>

using namespace std;

const int MAXN = 1000005;
const int MAXLOG = 20;

int n, m;
int c[MAXN];
char seq[MAXN];

int sum[MAXN][MAXLOG];
int dip[MAXN][MAXLOG];

int cycle;

int pow2[MAXN];
int npows[MAXLOG];

long long games[MAXN];

int gcd(int a, int b) {
  if (b == 0)  {
    return a;
  } else {
    return gcd(b, a % b);
  }
}

int getSum(int a, int b) {
  int res = 0;
  while (b) {
    int l = pow2[b];
    res += sum[a][l];
    b ^= (1 << l);
    a = (a + npows[l]) % m;
  }
  return res;
}

int getDip(int a, int b) {
  int res = 0;
  int tmpSum = 0;
  while (b) {
    int l = pow2[b];
    res = min(res, tmpSum + dip[a][l]);
    tmpSum += sum[a][l];
    b ^= (1 << l);
    a = (a + npows[l]) % m;
  }
  return res;
}

int main() {
  for (int i = 1; i < MAXN; ++i) {
    int l = 0, d = 1;
    while (i >= (2*d)) {
      ++l;
      d *= 2;
    }
    pow2[i] = l;
  }

  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &c[i]);
  }
  int trueCycleSum = 0;
  scanf("%d%s", &m, seq);
  npows[0] = n;
  for (int i = 1; i < MAXLOG; ++i) {
    npows[i] = (2*npows[i-1]) % m;
  }
  for (int i = 0; i < m; ++i) {
    if (seq[i] == 'W') {
      sum[i][0] = 1;
      dip[i][0] = 0;
      ++trueCycleSum;
    } else {
      sum[i][0] = dip[i][0] = -1;
      --trueCycleSum;
    }
  }
  for (int l = 1, len = n; l < MAXLOG; ++l, len = (2 * len) % m) {
    for (int i = 0; i < m; ++i) {
      sum[i][l] = sum[i][l - 1] + sum[(i + len) % m][l - 1];
      dip[i][l] = min(dip[i][l - 1], sum[i][l - 1] + dip[(i + len) % m][l - 1]);
    }
  }
  cycle = m / gcd(n, m);
  bool inf = true;
  for (int i = 0; i < n; ++i) {
    int cycleSum = getSum(i % m, cycle);
    int cycleDip = getDip(i % m, cycle);
    if (c[i] > -cycleDip) {
      if (cycleSum >= 0) {
        games[i] = -1;
        continue;
      } else {
        int fullCycleGames = (c[i] + cycleDip - cycleSum - 1) / (-cycleSum);
        games[i] += (long long) fullCycleGames * cycle;
        c[i] -= fullCycleGames * (-cycleSum);
      }
    }
    inf = false;
    // c[i] <= -cycleDip
    // musimy znalezc miejsce, gdzie sie zeruje

    // lower = 1, upper = 2^20
    int l = 19, tmpSum = 0, offset = 0, offsetSum = 0;
    while (l >= 0) {
      int curDip = tmpSum + dip[(i + offsetSum) % m][l];
      if (c[i] <= -curDip) {

      } else {
        tmpSum += sum[(i + offsetSum) % m][l];
        offset |= (1 << l);
        offsetSum += npows[l];
      }
      --l;
    }
    games[i] += offset + 1;
  }
  if (inf) {
    printf("-1\n");
    return 0;
  }
  long long allPlay = -1;
  int loserIndex = -1;
  for (int i = 0; i < n; ++i) {
    if (games[i] != -1) {
      if (allPlay == -1) {
        allPlay = games[i];
        loserIndex = i;
      } else {
        if (games[i] < allPlay) {
          allPlay = games[i];
          loserIndex = i;
        }
      }
    }
  }
  printf("%lld\n", (allPlay - 1) * n + loserIndex + 1);
  return 0;
}