Tedious Work
Practice
4.2 (5 votes)
Algorithms
Dynamic programming
Medium
Problem
71% Success 1368 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

John has a very long and tedious project in his current semester, in which he has to write n documents, numbered from 1 to n. The ith document takes A(i) amount of time to complete. He needs to complete the document in the order of their numbers and he cannot skip any document. In other words, he has to complete 1st document, then 2nd, then 3rd and so on.

He doesn't like to write documents, so he decided to copy documents from the internet. It takes him C(i) amount of time to look for the ith document online. If he searches for the ith document, then search result gives him next k documents starting from the ith document. He can copy these documents to complete the project. After getting k documents (starting from ith document) he can directly jump to (i+j)th document where 1 ≤ j ≤ k-1 by copying all the documents from index i to (i+j-1) from the search result. It is not necessary for John to copy all k documents. As John hates to write these documents, he wants to spend minimum time doing this project. Help him to determine the minimum time it takes to complete the project. Copy-paste does not take any time.

Input Format

In the first line, you are given two integers, n (number of documents) and k (maximum continuous documents that John can copy).

In the second line, you are given an array A with n integers, A(i), where 1 ≤ i ≤ n, represents the time to write the ith document.

In the third line, you are given an array C with n integers, C(i), where 1 ≤ i ≤ n, represents the time it takes to look for ith document online.

Constraints

1 ≤ n ≤ 1000

1 ≤ k ≤ n

1 ≤ A(i) ≤ 105

1 ≤ C(i) ≤ 105

Output Format

Print the single Integer, which is the minimum required time to complete the project.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
7 votes
Tags:
Medium
Points:30
34 votes
Tags:
Basic ProgrammingMediumReady
Points:30
8 votes
Tags:
AlgorithmsDynamic ProgrammingMediumString Manipulation