There is a snail at the bottom of a well. Initially, the height of the snail is $0$ metres. The height of the well is $H$ metres.
You are given $N$ days of weather report. R indicates a rainy day and S indicates a sunny day.
At the start of each day:
- If the day is rainy, the snail will climb $A$ meters up.
- If the day is sunny, the snail will climb $B$ meters up.
At the end of each day, the snail will drop $C$ meters down. If its current height is smaller than $C$, it'll drop to $0$ metres.
Output the day when the snail climbs out of the well. The snail is considered climbed out of the well if at any moment of a day, its height is $\ge H$.
If the snail cannot climb out after the $N^{th}$ day, output $-1$.
Input
The first line consists of one integer $N$.
The second line consists of $4$ integers, $A, B, C, H$.
The third line consists $N$ characters, $S_1, S_2 ... S_n$, the $i^{th}$ character represent the weather status of the $i^{th}$ day.
Output
One integer, the day when the snail climbs out
Scoring
For all test cases, $1 \leq N \leq 10^6, 0 \leq A,B,C \leq 1000, 1 \leq H \leq 10^9$
This problem will be divided into subtasks, see the scoring table below:
| Subtask | Score | Additional Constraints | |
|---|---|---|---|
| 1 | 11 | $A,B=0$ | |
| 2 | 89 | No Additional Constraints |
Sample Test Cases
| Input | Output | |
|---|---|---|
| 3 2 6 2 10 RSS |
3 | |
| 1 0 0 0 1000 R |
-1 |
Scoring: Per Subtask
Authored by s22r42
Appeared in 2026 Mini Comp 2