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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <cstdlib>
#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>

typedef long long money_t;
typedef int seq_t;
typedef long long turn_t;

class Sequence {
private:

    int idxOfMinusAfterPlus() {
        for (unsigned int i = 1; i < sequence.size(); ++i) {
            if (sequence[i - 1] == 1 && sequence[i] == -1) {
                return i;
            }
        }
        return 0;
    }

    long long maxLoseForSequence() {
        int idx = idxOfMinusAfterPlus();
        long long result = 0L;
        long long sum = 0;
        for (unsigned int i = 0; i < sequence.size(); ++i) {
            seq_t value = sequence[(i + idx) % sequence.size()];
            total += value;
            sum += value;
            if (sum > 0) {
                sum = 0;
            }
            if (sum < result) {
                result = sum;
            }
        }
        return result;
    }
public:
    std::vector<seq_t> sequence;
    long long maxLose = 0L;
    long long total = 0L;

    Sequence(const std::vector<seq_t>& v) :
    sequence(v) {
        maxLose = maxLoseForSequence();
    }

    long long maxLoseFromIndex(const int idx) {
        long long result = 0L;
        long long sum = 0;
        for (unsigned int i = 0; i < sequence.size(); ++i) {
            seq_t value = sequence[(i + idx) % sequence.size()];
            sum += value;
            if (sum < result) {
                result = sum;
                if(result == maxLose) {
                    break;
                }
            }
        }
        return result;
    }
};

class PlacedSequence {
private:
    Sequence* sequence;
    unsigned long long offset = 0L;
    long long realMaxLose = 1L;
private:

    long long getRealMaxLose() {
        if (realMaxLose == 1L) {
            realMaxLose = sequence->maxLoseFromIndex(offset);
        }
        return realMaxLose;
    }
public:

    PlacedSequence(Sequence* s, const unsigned long long offset = 0) :
    sequence(s), offset(offset) {
    }

    turn_t turnsToLose(int dollars, long long dontLookForMoreThan) {
        if (sequence->maxLose + dollars <= 0 && getRealMaxLose() + dollars <= 0) {
            long long sum = 0;
            for (unsigned int i = 0; i < sequence -> sequence.size(); ++i) {
                seq_t value = sequence->sequence[(i + offset) % sequence->sequence.size()];
                sum += value;
                if (sum + dollars == 0) {
                    return i + 1;
                }
            }
            return -1;
        } else {
            if (sequence->total >= 0) {
                return -1;
            }
            long long m = dollars % (-(sequence->total));
            turn_t fullRounds = dollars / (-sequence->total);
            if (m == 0) {
                fullRounds = (fullRounds - 1) * sequence->sequence.size();
                if (dontLookForMoreThan == -1 || fullRounds < dontLookForMoreThan) {
                    return fullRounds + turnsToLose(-sequence->total, dontLookForMoreThan);
                }
                return dontLookForMoreThan;
            } else {
                fullRounds *= sequence->sequence.size();
                if (dontLookForMoreThan == -1 || fullRounds < dontLookForMoreThan) {
                    return fullRounds + turnsToLose(m, dontLookForMoreThan);
                }
                return dontLookForMoreThan;
            }
        }
    }
};

class Child {
private:
    money_t money;
    PlacedSequence* sequence;
public:

    Child()
    : money(0), sequence(NULL) {
    }

    explicit Child(money_t money) :
    money(money), sequence(NULL) {
    }

    void setSequence(PlacedSequence* sequence) {
        this->sequence = sequence;
    }

    turn_t turnsToLose(long long dontLookForMoreThan) {
        return sequence->turnsToLose(money, dontLookForMoreThan);
    }
};

class Solver {
private:
    std::vector<Child> children;
    std::vector<int> offsets;
    std::string cycle;
private:

    void readInput() {
        int n;
        std::cin>>n;
        children.resize(n);
        for (int i = 0; i < n; ++i) {
            money_t dollars;
            std::cin>>dollars;
            children[i] = Child(dollars);
        }
        int m;
        std::cin>>m;
        std::cin>>cycle;
    }

    unsigned long long cycleOffset(const int idx) {
        return offsets[idx % offsets.size()];
    }

    void assignSequences() {
        if (children.size() % cycle.size() == 0) {
            Sequence* winningSequence = new Sequence(std::vector<seq_t>(1, 1));
            Sequence* losingSequence = new Sequence(std::vector<seq_t>(1, -1));
            for (unsigned int i = 0; i < children.size(); ++i) {
                if (cycle[i % cycle.size()] == 'W') {
                    children[i].setSequence(new PlacedSequence(winningSequence));
                } else {
                    children[i].setSequence(new PlacedSequence(losingSequence));
                }
            }
        } else if (cycle.size() % children.size() == 0) {
            unsigned int seqLen = cycle.size() / children.size();
            std::vector<seq_t> sequence(seqLen);

            for (unsigned int i = 0; i < children.size(); ++i) {
                for (unsigned int j = 0; j < seqLen; ++j) {
                    sequence[j] = cycle[children.size() * j + i] == 'P' ? -1 : 1;
                }
                children[i].setSequence(new PlacedSequence(new Sequence(sequence)));
            }
        } else {
            offsets.resize(cycle.size());
            std::vector<seq_t> transposedSequence(cycle.size());
            for (unsigned long long i = 0; i < cycle.size(); ++i) {
                int idx = (i * children.size()) % cycle.size();
                transposedSequence[i] = cycle[idx] == 'P' ? -1 : 1;
                offsets[idx] = i;
            }

            Sequence* sequence = new Sequence(transposedSequence);

            for (unsigned int i = 0; i < children.size(); ++i) {
                children[i].setSequence(new PlacedSequence(sequence, cycleOffset(i)));
            }
        }
    }

    void printSolution() {
        long long roundsNeededToLose = -1;
        unsigned int childIndex = 0;
        for (unsigned int i = 0; i < children.size(); ++i) {
            turn_t turns = children[i].turnsToLose(roundsNeededToLose);
            if (turns != -1) {
                if (roundsNeededToLose == -1) {
                    childIndex = i;
                    roundsNeededToLose = turns;
                } else if (turns < roundsNeededToLose) {
                    roundsNeededToLose = turns;
                    childIndex = i;
                }
            }
        }
        if (roundsNeededToLose == -1) {
            std::cout << -1 << std::endl;
        } else {
            std::cout << ((roundsNeededToLose - 1) * children.size()) + childIndex + 1 << std::endl;
        }
    }
public:

    void solve() {
        readInput();
        assignSequences();
        printSolution();
    }
};

int main(int argc, char** argv) {
    std::ios_base::sync_with_stdio(false);
    Solver solver;
    solver.solve();
    return 0;
}