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
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <vector>
#include <string>
#include <cstdio>
using namespace std;

int main()
{
    long long int friends;
    scanf("%lld", &friends);

    long long int money[friends];
    for(long long int z=0; z<friends; z++)
        scanf("%lld", &money[z]);

    long long int cycle_len;
    scanf("%lld", &cycle_len);
    long long int cycle[cycle_len];

    getchar();
    for(long long int v=0; v<cycle_len; v++)
        cycle[v] = getchar();

    long long int unmodified[friends];
    for(long long int z=0; z<friends; z++)
        unmodified[z] = money[z];

    long long int max_decrease[friends], diff[friends];
    for(long long int z=0; z<friends; z++)
    {
        max_decrease[z] = 0;
        diff[z] = 0;
    }

    if(cycle[0] == 'W')
        ++diff[0];
    else
        --diff[0];

    long long int m_i, c_i, checked = 0, round = 1, ended = 0, no_money = 0;

    if(friends == 1)
        m_i = 0;
    else
        m_i = 1;
    if(cycle_len == 1)
        c_i = 0;
    else
        c_i = 1;

    if(money[0] + diff[0] == 0)
        ended = 1;
    long long int a = cycle_len, b = friends, tmp;
    while(b!=0)
    {
        tmp=b;
        b=a%b;
        a=tmp;
    }
    long long int NWW = (friends/a)*cycle_len;



    while(!ended)
    {
        ++round;
        if(cycle[c_i] == 'W')
            ++diff[m_i];
        else
        {
            --diff[m_i];
            if(diff[m_i] + money[m_i] == 0)
            {
                no_money = 1;
                break;
            }
            if(diff[m_i] < max_decrease[m_i])
                max_decrease[m_i] = diff[m_i];
        }

        m_i = (m_i+1) % friends;
        c_i = (c_i+1) % cycle_len;
        if(m_i == 0 && c_i == 0 && checked == 0)
        {
            long long int r, max_r = 10000000;
            checked = 1;
            ended = 1;
            for(long long int v=0; v<friends; v++)
            {
                money[v] += diff[v];
                if(money[v] < unmodified[v])
                {
                    ended = 0;
                    if(money[v] + (max_decrease[v] - diff[v]) > 0)
                        r = (money[v] + (max_decrease[v] - diff[v])) / diff[v];
                    else
                    {
                        max_r = 0;
                        break;
                    }
                    if(r < max_r)
                    {
                        max_r = r;
                        if(max_r <= 0)
                            break;
                    }
                }
            }

            if(max_r > 0)
            {
                for(long long int v=0; v<friends; v++)
                    money[v] -= max_r * diff[v];
                round += NWW * max_r;
            }
            for(long long int v=0; v<friends; v++)
                diff[v] = 0;
        }
    }
    if(ended && !no_money)
        printf("1");
    else
    {
        printf("%lld", round);
    }

    return 0;
}