You are given a string consisting of characters 0 and/or 1. You have to paint every character of this string into one of two colors, red or blue.
If you paint the-th character red, you get coins. If you paint it blue, you get coins.
After coloring the string, you remove every blue character from it, and count the number of inversions in the resulting string (i. e. the number of pairs of characters such that the left character in the pair is 1, and the right character in the pair is 0). For each inversion, you have to pay coin.
What is the maximum number of coins you can earn?
The first line of the input contains one integer( ) — the number of test cases.
Each test case consists of four lines:
- the first line contains one integer ( ) — the length of the string;
- the second line contains 0 or 1; — a string of characters, where each character is either
- the third line contains integers ( );
- the fourth line contains integers ( ).
Additional constraint on the input: the sum of values ofover all test cases does not exceed .
For each test case, print one integer — the maximum number of coins you can earn.
43 36 76 52
Explanations for the test cases for the example (blue characters are underlined, red ones are not):
# Function to calculate the maximum coins that can be earned
def max_coins(n, s, r, b):
coins = 0
red_counts =  * n
total_reds = 0
# Count the number of reds on the right
for i in range(n – 1, -1, -1):
red_counts[i] = total_reds
if s[i] == ‘1’:
total_reds += 1
# Calculate the coins by checking each character in the string
for i in range(n):
if s[i] == ‘1’:
coins += r[i]
coins += b[i]
coins -= red_counts[i]
# Read the number of test cases
t = int(input())
# Iterate through each test case
for _ in range(t):
n = int(input())
s = input()
r = list(map(int, input().split()))
b = list(map(int, input().split()))
result = max_coins(n, s, r, b)