cswork / OAOpen practice workspace

eBayOA #4

Currency Trading Strategy

Estimated MediumJudge-ready

Topics (AI-labelled)Two PointersPrefix & Ranges

Strategy values are limited to −1 (sell), 0 (hold), and +1 (buy), and the profit on day i is −strategy[i]×rate[i]. Choose exactly one contiguous window of length k and reset the strategies in that window to one of these three valid values; find the maximum total profit. Interpreting any value as any value in the set of strategy values is an explicit type convention of this site.

Input

n,k (1≤k≤n≤200000), rates in 0..10⁹, and strategy in −1/0/+1. The ranges are site supplements.

Output

Output the total profit (64-bit).

Example 1

Input:

3 1
1 1 10
0 0 1

Output:

10

Example 2

Input:

3 2
5 2 8
1 -1 0

Output:

7

Example 3

Input:

1 1
4
0

Output:

4

Editorial

Loading solution…