Under construction

Still being built. Things may change

Oil Producing Nation

Interview

Time limit1sMemory limit512 MB

Summary
Pick a contiguous arc of districts on a circle that contains at least K threat districts, maximizing the sum of the A values.
Level

Medium6 of 10

Topics
Prefix sum, Sliding window, Array
Solved
No attempts yet

Problem

The United States of Jooddae, a true great power, works every day for world peace. To that end, USJ leader Oh Juwon has decided to contribute to keeping the peace today.

As you well know, the most important condition for a peaceful democracy is oil. You are preparing to seize a contiguous stretch of districts in Yunlac to extract oil.

Yunlac, the enemy of USJ, is divided into circular districts numbered from 11 to NN. Some of these districts have threat groups.

Invading an enemy country to extract oil would draw the attention of other nations, so you have announced that you will occupy at least KK districts that have threat groups.

If you occupy district ii, you extract oil and gain AiA_i in profit. If the cost of occupation is greater than the profit, the total profit may be negative.

How should you occupy the land so that Juwon gets the maximum profit?

Input

The first line contains the number of districts NN and the minimum number of threat groups KK that must be among the occupied districts.

The second line contains the integers A1,A2,…,ANA_1, A_2, \dots, A_N, separated by spaces.

The third line contains the integers B1,B2,…,BNB_1, B_2, \dots, B_N, separated by spaces. If BiB_i is 00, district ii has no threat group. If BiB_i is 11, it has one.

Output

Print the maximum profit Juwon can obtain.

Constraints

  • 1≤K≤N≤500 0001 \le K \le N \le 500\,000
  • −109≤Ai≤109-10^9 \le A_i \le 10^9 (1≤i≤N1 \le i \le N)
  • Bi=0B_i = 0 or 11 (1≤i≤N1 \le i \le N)
  • There are at least KK indices ii with Bi=1B_i = 1.

Examples2

  1. Example 1

    Input
    5 2
    5 -2 -1 -5 1
    1 0 1 1 0
    
    Expected output
    3
    
  2. Example 2

    Input
    1 1
    -1000000000
    1
    
    Expected output
    -1000000000