Collecting apples

3

2 votes
Ready, Open, Recruit, Ready, Open, Recruit, Ready, 2 Dimensional, Dynamic Programming, Two dimensional, Medium, Algorithms, approved
Problem

Your task is to collect apples from various farms. You can move sequentially from your house to the first farm, the first farm to the second farm, from the second farm to the third farm, and so on.

This direction of movement is as follows:

House >1st farm>2nd>3rd>4th>5th..>Nth and so on

There are N farms in a line. Moving from one farm to the next farm consumes a single unit of your energy. Therefore, you may have to refill your energy. You can ask the owner of a farm to provide you milk so that you can gain some energy. The farm owner agrees to provide you milk only on one condition that you cannot take apples from that farm.

At each farm, you are either allowed to drink milk or take apples. Each farm contains different amount of apples and milk. Therefore, from each farm, you are allowed to take only either the entire amount of milk or apples. From each farm, you cannot take both apples and milk or none of them.

Your task is to determine the maximum number of apples that you can collect always having non-negative energy.

Note: Initially, you are at your house. It takes one unit of energy to move to the first farm.

Input format

  • First line: A single integer t denoting the number of test cases in a single test file
  • Second line: N and P denoting the number of farms and your initial energy level
  • Third line: N space-separated integers, where the ith integer is Milk[i] and denotes the amount of energy that you can gain by drinking milk from the ith farm
  • Fourth line: N space-separated integers, where the ith integer is Apples[i] and denotes the number of apples that you can collect from the ith farm if you do not drink milk from that farm

Constraints

1t100

1N1000

0P1018

0Milk[i]1000

0Apples[i]1000

Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

In the first test of the sample, there are 3 farms in a row, and Monk's initial energy is 2.

He shall first move from his house to the first farm. After this,

Energy : 1, Apples : 0

He shall then collect all apples available in the first farm, and then move to the second farm

Energy :0, Apples : 100

He shall then Have all the Milk available in the second farm and move to the 3rd farm.

Energy : 1 , Apples: 100

He shall then Collect all the apples available in the last farm and end his journey.

Energy : 0 ,Apples : 200

This is the maximum number of apples Monk can collect.

Editor Image

?