‘’True wisdom lies not in knowing all the answers, but in embracing the mystery of the unknown and allowing it to guide us towards new understandings.’’ - written by Artificial Intelligence.
Artificial intelligence (AI) has undoubtedly become increasingly powerful each day. Its ability to solve complex problems is one of the most remarkable advancements in recent times. Is it possible that one day humans will follow AI instructions without knowing what they are doing?
Imagine you are living in a world where AI is ruling. Every person follows AI instructions, and you are one of them.
In your imaginary world, there are portals, and each is numbered from to . You can open any portal with some instructions given by artificial intelligence. In the beginning, all the portals are closed.
Each portal has a number written on it, denoted by . if is positive every time you are on the portal, you will receive a reward of coins, whereas if is negative, you will be penalized and lose coins.
Initially, you have coins, and it is guaranteed that the instructions provided by the artificial intelligence will ensure that you always have a non-negative number of coins.
There are two types of instructions provided by artificial intelligence:
GOTO : This instruction requires you to leave the current portal without closing it and open the portal. Initially, you are not at any portal.
PREVIOUS: This instruction instructs you to close the current portal and leave it. Then go to the portal opened exactly before it. If the AI instructs you to go previously, it is guaranteed that you previously came from another portal to the current portal.
You have been given instructions to follow in order to collect coins. After following all the instructions, you need to determine the total number of coins you have obtained.
The first line of the input contains two positive integers and , separated by a space — the number of portals and instructions, respectively.
The second line contains integers — the number of coin in portal.
The instructions will be given in the following lines.
Each line will either contain "GOTO " or "PREVIOUS" without quotation marks.
Output a single integer — indicating the total number of coins obtained after following all the instructions.
Input | Output |
---|---|
5 3 1 -3 4 0 9 GOTO 3 GOTO 2 PREVIOUS | 5 |
At first, you open the portal and receive a reward of coins. Currently you have coins. Then, you leave the portal without closing it. After that, you have to open the portal and pay a penalty of coins. Currently you have coins. Lastly, you leave the portal and closing it. Then, you have to go to the portal opened exactly before it, which is the portal and receive coins as reward. Currently you have coins. |