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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>

#define N_MAX 1000000

using namespace std;

int n, m;
int cash[N_MAX], cash_orig[N_MAX], delta;
bool wins[N_MAX], neverEnds = true;
char c[N_MAX];
int bigCycle, cnt, currentIndex = 0, gameNum = 0;

int gcd(int a, int b)
{
    int c;

    while (b != 0)
    {
        c = a % b;
        a = b;
        b = c;
    }

    return a;
}

int lcm(int a, int b) { return a * b / gcd(a,b); }

void play(int player, int game)
{
    cash[player] += (wins[game % m]) ? 1 : -1;
}

int main()
{
    ios_base::sync_with_stdio(false);

    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        cin >> cash[i];
        cash_orig[i] = cash[i];
    }
    cin >> m;
    cin >> c;
    for (int i = 0; i < m; ++i)
    {
        if (c[i] == 'W') wins[i] = true;
    }

    while (true)
    {
        // cout << currentIndex << ' ' << cash[currentIndex] << endl;

        play(currentIndex, gameNum);

        if (cash[currentIndex] == 0)
        {
            cout << gameNum + 1 << '\n';
            return 0;
        }

        ++currentIndex;
        currentIndex %= n;
        ++gameNum;

        if (currentIndex == 0 && gameNum % m == 0) break; // big cycle
    }

    for (int i = 0; i < n; ++i)
    {
        delta = cash[i] - cash_orig[i];
        if (neverEnds && delta < 0) { neverEnds = false; break; }
    }

    if (neverEnds)
    {
        cout << "NIE\n";
        return 0;
    }

    while (true)
    {
        play(currentIndex, gameNum);

        if (cash[currentIndex] == 0)
        {
            cout << gameNum + 1 << '\n';
            return 0;
        }

        ++currentIndex;
        currentIndex %= n;
        ++gameNum;
    }

    return 0;
}