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
#include <time.h>
#include <stdio.h>
#include <deque>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;

typedef unsigned long long ull;

int main()
{
//    const clock_t begin_time = clock();

    int N, M;

    scanf( "%d", &N );
    vector<int> players( N );

    for( int i = 0; i < N; ++i ){
        scanf( "%d", &players[i] );
    }
    
    scanf( "%d", &M );
    vector<int> cycle( M );

    string s;
    s.resize( M );
    scanf( "%s", s.c_str() );

    int cycle_sum = 0;
    for( int i = 0; i < M; ++i ){
        cycle[i] = s[i] == 'W' ? 1 : -1;
        cycle_sum += cycle[i];
    }

    bool isWinner = false, process = true;
    ull cntr = 0;
    while( process ){
        for( int step : cycle ){
            for( int& player : players ){
                ++cntr;
                player += step;
                if( player == 0 ){
                    process = false;
                    isWinner = true;
                    break;
                }
            }

            if( !process )
                break;
        }

        if( cycle_sum >= 0 )
            break;
    }


    if(isWinner)
        printf( "%lld\n", cntr );
    else
        printf( "-1\n", cntr );

//    printf("\n%f", float( clock() - begin_time ) / CLOCKS_PER_SEC);
}