nq1s788

журналы монокарпа

Feb 15th, 2026
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.53 KB | None | 0 0
  1. t = int(input())
  2. for _ in range(t):
  3.     n = int(input())
  4.     s = input()
  5.     a = list(map(int, input().split()))
  6.     i = 0
  7.     answ = 0
  8.     while i < n and s[i] == '1':
  9.         answ += a[i]
  10.         i += 1
  11.     while i < n:
  12.         while i + 1 < n and s[i] == '0' and s[i + 1] == '0':
  13.             i += 1
  14.         sm = a[i]
  15.         mn = a[i]
  16.         while i + 1 < n and s[i + 1] == '1':
  17.             sm += a[i + 1]
  18.             mn = min(mn, a[i + 1])
  19.             i += 1
  20.         answ += sm - mn
  21.         i += 1
  22.     print(answ)
Advertisement
Add Comment
Please, Sign In to add comment