Problem F
Speedrunning

Mario has recently discovered that the physics engine has gotten an update: he can now run faster when he’s in Small Mario form. This has caused quite the headache for speedrunners.
For simplicity, we consider the level to be a
On each tile, there can be either a question block (denoted
‘?’), a saw (denoted ‘S’), or neither (denoted by ‘.’). It is
guaranteed that the first tile and
Mario can take one of three forms: Small Mario, Super Mario,
or Fire Mario. To move one tile forward, the movement takes
Small Mario
When Mario reaches a tile with a saw on it, he activates the saw and takes damage (the saws are unavoidable). This causes the following behavior:
-
If Mario is in Small Mario form, he dies.
-
If Mario is in Super Mario form, he becomes Small Mario.
-
If Mario is in Fire Mario form, he becomes Super Mario.
When Mario reaches a question block, he can choose to activate it, causing the following behavior if he activates it:
-
If Mario is in Small Mario form, he becomes Super Mario.
-
If Mario is in Super Mario form, he becomes Fire Mario.
-
If Mario is in Fire Mario form, nothing happens.
Interactions with saws and question blocks both take
Speedrunners now face the difficult choice of min-maxing
whether or not they should activate each question block. To
make matters even more complicated, players are allowed to
start in any of the
Given optimal play, is it possible to complete the level, and if so, what is the minimum amount of game ticks required to successfully complete the level?
Input
The first line of input is an integer
The second line of input is a string of length
Output
Output a single integer denoting the minimum amount of game
ticks required to successfully complete the level, or
Explanation for Sample Input 1: Mario should start the level on the 1st tile as Super Mario. In this form, it takes 2 ticks for a movement. Thus, Mario reaches the 2nd tile after 2 ticks. The saw instantly activates, and Mario becomes Small Mario. It now takes him only 1 tick to complete a movement. He reaches the 3rd tile in one additional tick, and reaches the 4th and final tile in one more tick. Thus, it takes him a total of 2+1+1 = 4 ticks.
Sample Input 1 | Sample Output 1 |
---|---|
4 .S.. |
4 |
Sample Input 2 | Sample Output 2 |
---|---|
6 .?.?S. |
6 |
Sample Input 3 | Sample Output 3 |
---|---|
7 .SS?SS. |
-1 |