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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <cstdio>
#include <algorithm>

using namespace std;

const int MAX_N = 1000000;
const int MAX_M = 1000000;

int savings[MAX_N];
char cycle[MAX_M];
char gen_cycle[MAX_N];

char player_cycle[MAX_M];
int cycle_visited[MAX_M];

long long compute_player_rounds(int n, int m, int player) {
    for (int i = 0; i < m; i++) {
        cycle_visited[i] = false;
    }
    int cycle_index = player % m;
    int player_cycle_size = 0;
    while (!cycle_visited[cycle_index]) {
        player_cycle[player_cycle_size++] = cycle[cycle_index];
        cycle_visited[cycle_index] = true;
        cycle_index = (cycle_index + n) % m;
    }

    int cycle_sum = 0;
    int cycle_sum_min = MAX_M + 1;
    for (int i = 0; i < player_cycle_size; i++) {
        if (player_cycle[i] == 'W') {
            cycle_sum += 1;
        }
        else {
            cycle_sum -= 1;
        }
        cycle_sum_min = min(cycle_sum_min, cycle_sum);
    }

    /*
    printf("%d %d %d\n", cycle_sum_min, cycle_sum, savings[player]);
    for (int i = 0; i < player_cycle_size; i++) {
        printf("%c", player_cycle[i]);
    }
    printf("\n");
    */

    if (cycle_sum_min >= 0) {  
        return -1;
    }
    int req = -cycle_sum_min + 1;
    if (cycle_sum >= 0) {
        if (savings[player] >= req) {
            return -1;
        }
        else {
            int simulation_savings = savings[player];
            for (int i = 0; i < player_cycle_size; i++) {
                if (player_cycle[i] == 'W') {
                    simulation_savings += 1;
                }
                else {
                    simulation_savings -= 1;
                }
                if (simulation_savings == 0) {
                    return i + 1;
                }
            }
        }
    }
    // cycle_sum_min < 0 && cycle_sum < 0
    long long player_rounds = player_cycle_size;
    int spare = savings[player] - req;
    if (spare < 0) {
        int simulation_savings = savings[player];
        for (int i = 0; i < player_cycle_size; i++) {
            if (player_cycle[i] == 'W') {
                simulation_savings += 1;
            }
            else {
                simulation_savings -= 1;
            }
            if (simulation_savings == 0) {
                return i + 1;
            }
        }
    }
    player_rounds += (spare / (-cycle_sum)) * player_cycle_size;
    int simulation_savings = req + (spare % (-cycle_sum)) - (-cycle_sum);
    //printf("%d %d %d\n", spare, player_rounds, simulation_savings);
    for (int i = 0; i < player_cycle_size; i++) {
        player_rounds += 1;
        if (player_cycle[i] == 'W') {
            simulation_savings += 1;
        }
        else {
            simulation_savings -= 1;
        }
        if (simulation_savings == 0) {
            break;
        }
    }
    return player_rounds;
}

int simulate(int n, int m, int rounds) {
    int simulation_savings[MAX_N];
    for (int i = 0; i < n; i++) {
        simulation_savings[i] = savings[i];
    }
    int savings_index = 0;
    int cycle_index = 0;
    for (int i = 0; i < rounds; i++) {
        if (cycle[cycle_index] == 'W') {
            simulation_savings[savings_index] += 1;
        }
        else {
            simulation_savings[savings_index] -= 1;
        }
        if (simulation_savings[savings_index] == 0) {
            return i + 1;
        }
        savings_index = (savings_index + 1) % n;
        cycle_index = (cycle_index + 1) % m;
    }
    return -1;
}

int main() {
    int n, s, m;
    char c;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &s);
        savings[i] = s;
    }
    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
        scanf(" %c", &c);
        cycle[i] = c;
    }

    long long res = simulate(n, m, 10000000);
    if (res != -1) {
        printf("%lld\n", res);
    }
    else {
        long long min_player_rounds = 9000000000000000000LL;
        int min_player_index = -1;
        for (int i = 0; i < n; i++) {
            long long player_rounds = compute_player_rounds(n, m, i);
            //printf("%d: %lld\n", i, player_rounds);
            if ((player_rounds != -1) && (player_rounds < min_player_rounds)) {
                min_player_rounds = player_rounds;
                min_player_index = i;
            }
        }
        if (min_player_index == -1) {
            printf("-1\n");
        }
        else {
            res = (min_player_rounds - 1) * n + min_player_index + 1;
            printf("%lld\n", res);
        }
    }

    return 0;
}