Ddong Game
Time limit1sMemory limit512 MB
Starting from 1 person, pick one of two operations per turn for N turns, optionally skipping exactly one turn, and maximize the final count while never dropping to 0 or less.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Implementation, Math
- Solved
- No attempts yet
Problem

This game smells so bad you cannot stand to watch it. So you will not play the ddong game yourself; you will have a program play it for you. You start with 1 person. You are given turns in total, and on each turn two of the following four choices are given. The same choice may be given twice. Each choice is one of .
- If you choose , the number of people increases by .
- If you choose , the number of people decreases by .
- If you choose , the number of people is multiplied by .
- If you choose , the number of people is divided by . If the current number of people is not divisible by , the remainder is discarded.
For exactly one of the choices, you may watch an ad and skip the choice. You may also choose not to watch an ad and not to skip a choice. If, after any turn ends, the current number of people becomes 0 or less, the game is over. You must maximize the number of people after going through the choices. It is guaranteed that no matter what choices you make, the number of people never exceeds the 32-bit integer range along the way.
Input
The first line gives the number of choices .
After that, lines each give 2 choices separated by a space.
Each choice is one of ().
Output
Output the maximum number of people after going through the choices.
If the game is over no matter what choices you make, output ddong game.
Hint
In the first example, choosing and and skipping the third choice gives the maximum of 12 people.
In the second example, the game is over no matter which choices you pick.