Capital OneOA #10
Stock Trading with Algo Override
Estimated MediumJudge-ready
Topics (AI-labelled)Prefix & RangesTwo Pointers
Given daily stock prices prices[i] and an action array algo[i] (0 means buy, 1 means sell), the original profit is the sum of sell prices minus the sum of buy prices. You may choose one contiguous range of length exactly k and set every algo in it to 1. Find the maximum total profit achievable after the change. Assume you hold enough shares, so selling is not limited by inventory.
Input
The first line contains n, k; the second line contains n positive integers prices[i]; the third line contains n 0/1 values algo[i]. This site adds 1≤k≤n≤200000, 1≤prices[i]≤10^9.
Output
Output the maximum total profit (a signed 64-bit integer).
Example 1
Input:
7 4
2 4 1 5 2 6 7
0 1 0 0 1 0 0
Output:
21
Example 2
Input:
3 3
5 1 9
0 0 1
Output:
15
Example 3
Input:
4 1
3 8 2 5
1 1 1 1
Output:
18
Editorial
Loading solution…