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

using namespace std;

int GCD(int a, int b)
{
    while(a!=b)
       if(a>b)
           a-=b;
       else
           b-=a;
    return a;
}

int main()
{

    /**Czytanie danych**/
    int n,m;

    scanf("%d",&n);
    int friends[n];
    int new_friends[n];

    for(int i=0;i<n;i++)
    {
        scanf("%d",&friends[i]);
        new_friends[i]=friends[i];
    }

    scanf("%d",&m);
    int casino[m];
    char c;

    scanf("%c",&c);

    for(int i=0;i<m;i++)
    {
        scanf("%c",&c);
        if(c=='W') casino[i]=1;
        else casino[i]=-1;
    }

    int end=1;
    long long int i=0;
    int nww=m/(GCD(m,n));
    nww*=n;

    int check;

    while(end)
    {
        new_friends[i%n]+=casino[i%m];
        if(new_friends[i%n]==0) end=0;
        i++;

        if(i==nww)
        {
            check=0;
            for(int j=0;j<n;j++) {if(new_friends[j]<friends[j])check=1;}
            if(check==0) {i=-1;end=0;}
        }

    }

    printf("%d",i);

    return 0;
}