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

using namespace std;

int main()
{
    int n, m, x1=0, x2=0, l;
    string gry;

    cin>>n;
    int t1[n], t2[n];

    for (int i=0; i<n; i++)
    {
        cin>>t1[i];
        t2[i]=t1[i];
    }
    cin>>m;
    cin>>gry;

    for (int i=0; i<m; i++)
    {
        if (gry[i]=='P')
        {
            l=0;
            while (t2[x2]>0)
            {
                if (gry[x1]=='W')
                {
                    t2[x2]++;
                }
                else
                {
                    t2[x2]--;
                }

                l++;

                if (t2[0]==t1[0] && t2[1]==t1[1])
                {
                    l=-1;
                    break;
                }

                if (t2[x2]==0)
                {
                    break;
                }

                x1=(x1+1)%m;
                x2=(x2+1)%n;
            }
            break;
        }
        else
            l=-1;
    }
    cout<<l;
}