# Python implementation for the above approach
def sum(n, k, arr, check, y):
# It will store the final answer
total_sum = 0
count_odd, count_even = 0, 0
# Loop for finding the totalSum
# and count odd and even
for i in range(n):
total_sum += arr[i]
if arr[i]%2 != 0:
count_odd += 1
else:
count_even += 1
# Loop for k queries
for q in range(k):
if check[q]:
# Adding to odd number
total_sum += count_odd*y[q]
if y[q]%2 != 0:
count_even += count_odd
count_odd = 0
else:
# Adding to even number
total_sum += count_even*y[q]
if y[q]%2 != 0:
count_odd += count_even
count_even = 0
# Returning the final answer
return total_sum
# Input 1
n1, k1 = 3, 3
arr1 = [1, 2, 4]
check1 = [False, True, False]
y1 = [2, 3, 5]
# function call
print(sum(n1, k1, arr1, check1, y1))
# Input 2
n2, k2 = 6, 7
arr2 = [1, 3, 2, 4, 10, 48]
check2 = [True, False, False, False, True, False, False]
y2 = [6, 5, 4, 5, 3, 12, 1]
# function call
print(sum(n2, k2, arr2, check2, y2))