Under construction

Still being built. Things may change

Dungeon

Interview

Time limit1sMemory limit512 MB

Summary
Given monster damage and scroll effects for each room, find the minimum starting health that lets the hero defeat every monster in order.
Level

Medium5 of 10

Topics
Greedy, Math
Solved
No attempts yet

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 00 to room NN. The hero starts in room 00 with initial health XX. (Albert decides this value later.)

Each room from 11 to NN has one monster, and each monster guards one magic scroll. Fighting the monster in room ii reduces the hero's health by Di>0D_i > 0. If the health drops to 00 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 RiR_i written on it. The string SS is given as input, and SiS_i gives the type of the scroll in room ii.

  • If the scroll in room ii is an addition scroll, SiS_i is '+', and using it raises the hero's health by RiR_i.
  • If the scroll in room ii is a multiplication scroll, SiS_i is '*', and using it multiplies the hero's health by RiR_i.

Albert's goal is to start with health XX in room 00, move through each room in order, and defeat every monster up to room NN. The scroll from room ii must be used right away.

For example, take N=4N = 4, D=[5,10,15,20]D = [5, 10, 15, 20], S=S = '+*++', and R=[2,3,4,5]R = [2, 3, 4, 5].

  • If X=10X = 10, room 1 leaves health at 5, and the +2 scroll brings it to 7. In room 2, health becomes −3-3, so the hero loses.
  • If X=22X = 22, room 1 leaves 17, and the scroll brings it to 19. Room 2 leaves 9, and the ×3\times 3 scroll brings it to 27. Room 3 leaves 12, and the +4 scroll brings it to 16. Room 4 leaves −4-4, so the hero loses.
  • If X=30X = 30, 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 TT.

The first line of each test case contains NN. The second line contains NN positive integers D1,D2,…,DND_1, D_2, \dots, D_N, separated by spaces. The third line contains a string SS of length NN with no spaces, where each character is '+' or '*'. The fourth line contains NN positive integers R1,R2,…,RNR_1, R_2, \dots, R_N, separated by spaces.

Output

Print the answer for each test case on its own line.

Constraints

  • 1≤T≤101 \le T \le 10
  • 1≤N≤1051 \le N \le 10^5
  • For every ii with 1≤i≤N1 \le i \le N:
    • 1≤Ri,Di≤1081 \le R_i, D_i \le 10^8
    • SiS_i is '+' or '*'.
  • The answer for each test case is at most 263−12^{63}-1.

Examples1

  1. Example 1

    Input
    7
    4
    5 10 15 20
    +*++
    2 3 4 5
    4
    3 5 3 5
    +**+
    5 2 2 4
    3
    2023 2023 2023
    ***
    1 2 3
    3
    2023 2023 2023
    ***
    3 2 1
    3
    1000 1000 1000
    +++
    1 1 1
    3
    1000 1000 1000
    ***
    1 1 1
    3
    10 10 10
    ***
    100000000 100000000 100000000
    
    Expected output
    24
    6
    5058
    3035
    2999
    3001
    11