Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- t = int(input())
- for _ in range(t):
- n = int(input())
- s = input()
- a = list(map(int, input().split()))
- i = 0
- answ = 0
- while i < n and s[i] == '1':
- answ += a[i]
- i += 1
- while i < n:
- while i + 1 < n and s[i] == '0' and s[i + 1] == '0':
- i += 1
- sm = a[i]
- mn = a[i]
- while i + 1 < n and s[i + 1] == '1':
- sm += a[i + 1]
- mn = min(mn, a[i + 1])
- i += 1
- answ += sm - mn
- i += 1
- print(answ)
Advertisement
Add Comment
Please, Sign In to add comment