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
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>

using namespace std;

bool is( int tab[], int help[], int n )
{
    bool flag = false;
    for( int i = 0; i < n; i ++ )
    {
        if( tab[i] < help[i] ) flag = true;
    }
    return flag;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n;
    cin >> n;
    queue <int> tab;
    int help[n];
    for( int i = 0; i < n; i ++ )
    {
        int a;
        cin >> a;
        tab.push(a);
        help[i] = a;
    }
    int m;
    cin >> m;
    string A;
    cin >> A;
    int check = 0;
    unsigned long long int lic = 0;
    while( true )
    {
        if( lic == n*m )
        {
            queue <int> R = tab;
            int r[n];
            for( int i = 0; i < n; i ++ )
            {
                r[i] = tab.front();
                tab.pop();
            }
            if( is(r,help,n) == false )
            {
                cout << "-1";
                return 0;
            }
        }
        int x = tab.front();
        tab.pop();
        if( A[check] == 'W' )
        {
            tab.push( x + 1 );
        }
        else
        {
            if( x - 1 == 0 )
            {
                //cout << A[check] << "\n";
                cout << lic + 1;
                return 0;
            }
            tab.push( x - 1 );
        }
        check ++;
        if( check == m ) check = 0;
        lic ++;
    }
    return 0;
}