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
129
130
131
132
133
134
135
136
137
138
139
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <climits>

using namespace std;

#define DEBUG false
#define log if (DEBUG) cerr
typedef long long ll;
typedef unsigned long long ull;

const ll INF_LL = 9223372036854775807LL;

static vector<ll> balances;
static vector<int> automat;
static int n, m;



static void readData()
{
    int i;
    string program;

    cin >> n;
    balances.resize(n);
    for (auto& a : balances)
    {
        cin >> a;
    }

    cin >> m >> program;
    for (i = 0; i < m; i ++)
    {
        automat.push_back(program[i] == 'W'? +1 : -1);
    }
}

static ll solveForBoy(int cashIdx)
{
    vector<ll> deltas;
    ll result;
    int autoIdx;
    set<int> visitedAutos;
    ll value;
    ll minDelta;
    ll cycleValue;
    ll balance = balances[cashIdx];

    autoIdx = cashIdx % m;
    value = 0;
    minDelta = 55;

    log << "Boy " << cashIdx << endl;
    while (true)
    {
        visitedAutos.insert(autoIdx);

        value += automat[autoIdx];
        deltas.push_back(value);
        minDelta = min(value, minDelta);

        autoIdx = (autoIdx + n) % m;
        if (visitedAutos.find(autoIdx) != visitedAutos.end())
        {
            break;
        }
    }

    cycleValue = value;

    if (DEBUG)
    {
        log << "ba: " << balance << ", cv: " << cycleValue << ", md: " << minDelta << endl;
        for (auto delta : deltas) log << delta << ",";
        log << endl;
    }

    if (cycleValue >= 0 && balance + minDelta > 0)
    {
        return -1;
    }

    ll nc = 0;
    if (balance + minDelta > 0)
    {
        nc = (balance + minDelta) / (-cycleValue);
        if ((balance + minDelta) % (-cycleValue) > 0)
        {
            nc ++;
        }
    }

    result = nc * deltas.size();
    value = balance + cycleValue * nc;
    for (ll delta : deltas)
    {
        result ++;
        if (value + delta <= 0)
        {
            break;
        }
    }

    log << "result: " << result << endl;
    return result;
}

static void solveBetter()
{
    ll result = INF_LL;
    for (int i = 0; i < n; i++)
    {
        ll boyResult = solveForBoy(i);
        if (boyResult > 0)
        {
            boyResult = (boyResult-1) * n + i + 1;
            result = min(result, boyResult);
        }
    }

    if (result == INF_LL)
    {
        cout << -1;
    }
    else
    {
        cout << result;
    }
}

int main()
{
    readData();
    solveBetter();
    return 0;
}