Oil Producing Nation
InterviewTime limit1sMemory limit512 MB
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 to . 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 districts that have threat groups.
If you occupy district , you extract oil and gain 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 and the minimum number of threat groups that must be among the occupied districts.
The second line contains the integers , separated by spaces.
The third line contains the integers , separated by spaces. If is , district has no threat group. If is , it has one.
Output
Print the maximum profit Juwon can obtain.
Constraints
- ()
- or ()
- There are at least indices with .