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
#include <iostream>
#include <string>
#define REP(a, b) for(int a = 0; a < b; a++)

using namespace std;

int main()
{
    int n, m, maks = 0;
    char c;
    string a = "";
    cin >> n;
    int k[n];
    REP(i, n){
        cin >> k[i];
        maks = max(maks, k[i]);
    }

    cin >> m;
    int cykl[m];

    REP(i, m) {
        cin >> c;
        a += c;
        cykl[i] = 0;
    }

    int wsk1=0, wsk2=0;
    int wyn = 0, s = 0, l = 0;

    while(1)
    {
        wyn++;

        if(a[wsk2] == 'W')
        {
            k[wsk1]++;
            cykl[wsk1]++;
        }
        else
        {
            k[wsk1]--;
            cykl[wsk1]--;
        }
        l++;
        if(cykl[wsk1] >= 0) s++;


        if(k[wsk1] == 0)
        {
            cout << wyn << endl;
            break;
        }

        if(wsk1 < n-1) wsk1++;
        else
        {
            if(l >= m && s == (n > m ? n : m))
            {
                cout << -1 << endl;
                break;
            }
            wsk1 = 0;
            if(l >= m)  {
                    s = 0;
                    l = 0;
            }
        }

        if(wsk2 < m-1) wsk2++;
        else wsk2 = 0;
    }
}