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
#include <cstdio>
#include <map>
#include <vector>

long long NWD(long long a, long long b)
{
  long long pom;

  while(b!=0)
  {
      pom = b;
      b = a%b;
      a = pom;
    }
    return a;
}

long long NWW(long long a, long long b)
{
    return (a*b) / NWD(a, b);
}
typedef std::multimap<int, int> Multimap;
typedef std::pair<int, int> Pair;
int main()
{
    int n, m, tmp;
    scanf("%d", &n);
    Multimap kolejnosc;
    int* in = new int[n];
    int* out = new int[n];
    for(int i = 0; i < n; i++)
    {
        scanf("%d", &tmp);
        in[i] = out[i] = tmp;
    }
    scanf("%d", &m);
    char c;
    int* wygrane = new int[m];
    for(int i = 0; i < m; i++)
    {
        scanf("%c", &c);
        if(c == 'W')
            wygrane[i] = 1;
        else if(c == 'P')
            wygrane[i] = -1;
        else
            i--;
    }


    long long nww = NWW(n, m);

    long long iteracja = 0L;
    int index = 0;
    int indexM = 0;
    while(nww > iteracja)
    {
        iteracja++;
        index %= n;
        indexM %= m;
        out[index] += wygrane[indexM];
        if(out[index] == 0)
        {
            printf("%lld\n", iteracja);
            return 0;
        }
        index++;
        indexM++;
    }
    bool infinity = true;
    for(int i = 0; i < n; i++)
    {
        if(out[i] < in[i])
        {
            infinity = false;
        }
    }

    if(infinity == true)
    {
        printf("-1\n");
        return 0;
    }

    while(1)
    {
        iteracja++;
        index %= n;
        indexM %= m;
        out[index] += wygrane[indexM];
        if(out[index] == 0)
        {
            printf("%lld\n", iteracja);
            return 0;
        }
        index++;
        indexM++;
    }
    return 0;
}