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
Constraints
1≤t≤100
1≤N≤1000
0≤P≤1018
0≤Milk[i]≤1000
0≤Apples[i]≤1000
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.