Fence Posts
Time limit5sMemory limit1024 MB
Given post heights and per-post costs to raise or lower them by 1 cm, find the minimum total cost so that some K consecutive posts end at the same height.
- Level
Hard8 of 10
- Topics
- Prefix sum, Sliding window, Binary search, Sorting
- Solved
- No attempts yet
Problem
At the UCPC farm, fence posts are driven in a row. Their heights are random, so the farm does not look beautiful. You must adjust the heights of the posts to make the farm beautiful.
The posts are numbered through from left to right, and the initial height of post is cm. Each post is made of a different material, so lifting or driving each post takes a different amount of force. Lifting post by cm takes of force, and driving it in by cm takes of force.
The beauty of the UCPC farm is the length of the longest contiguous segment of posts that all have the same height. Find the minimum force needed to make the beauty of the farm at least .
Input
The first line gives the number of posts and the required beauty of the farm .
The next line gives the initial heights of the posts , separated by spaces.
The next line gives the force needed to lift each post by cm, , separated by spaces.
The next line gives the force needed to drive each post in by cm, , separated by spaces.
All values in the input are integers.
Output
On the first line, output the minimum force needed to make the beauty of the farm at least .

