class Solution:
def maxProfit(self, prices: List[int], strategy: List[int], k: int) -> int:
base = 0
for i in range(len(prices)):
base += prices[i] * strategy[i]
first_half = []
second_half = []
for i in range(len(prices)):
first_half.append(-strategy[i] * prices[i])
second_half.append(prices[i] -strategy[i] * prices[i])
prefix_sum_first_half = [0]
prefix_sum_second_half = [0]
for j in range(len(first_half)):
prefix_sum_first_half.append(prefix_sum_first_half[j] + first_half[j])
prefix_sum_second_half.append(prefix_sum_second_half[j] + second_half[j])
max_delta = 0
for l in range(len(prices) - k + 1):
mid_pt = l + int(k//2)
right_pt = l + k
curr_delta = prefix_sum_first_half[mid_pt] - prefix_sum_first_half[l] + prefix_sum_second_half[right_pt] - prefix_sum_second_half[mid_pt]
max_delta = max(curr_delta, max_delta)
return base + max_delta