Dungeon
InterviewTime limit1sMemory limit512 MB
Given monster damage and scroll effects for each room, find the minimum starting health that lets the hero defeat every monster in order.
Problem
Albert designed a dungeon game that one person can play alone. The game takes place on a one-dimensional line. The hero moves through rooms in order, from room to room . The hero starts in room with initial health . (Albert decides this value later.)
Each room from to has one monster, and each monster guards one magic scroll. Fighting the monster in room reduces the hero's health by . If the health drops to or below, the hero loses. After defeating a monster, the hero uses the scroll in that room to raise health. Each scroll is either an addition scroll or a multiplication scroll, and it has a positive integer written on it. The string is given as input, and gives the type of the scroll in room .
- If the scroll in room is an addition scroll, is '
+', and using it raises the hero's health by . - If the scroll in room is a multiplication scroll, is '
*', and using it multiplies the hero's health by .
Albert's goal is to start with health in room , move through each room in order, and defeat every monster up to room . The scroll from room must be used right away.
For example, take , , '+*++', and .
- If , room 1 leaves health at 5, and the +2 scroll brings it to 7. In room 2, health becomes , so the hero loses.
- If , room 1 leaves 17, and the scroll brings it to 19. Room 2 leaves 9, and the scroll brings it to 27. Room 3 leaves 12, and the +4 scroll brings it to 16. Room 4 leaves , so the hero loses.
- If , room 1 leaves 25, and the scroll brings it to 27. Room 2 leaves 17, and the scroll brings it to 51. Room 3 leaves 36, and the scroll brings it to 40. Room 4 leaves 20, and the scroll brings it to 25. The hero defeats every monster.
In this example, the hero cannot defeat all monsters with initial health below 24. With initial health 24 or more, the hero can defeat all monsters.
Input
The first line contains the number of test cases .
The first line of each test case contains . The second line contains positive integers , separated by spaces. The third line contains a string of length with no spaces, where each character is '+' or '*'. The fourth line contains positive integers , separated by spaces.
Output
Print the answer for each test case on its own line.
Constraints
- For every with :
- is '
+' or '*'.
- The answer for each test case is at most .