The Enchanted Glasses

All submissions for this problem are available.

One day while roaming the lands of Greece, Rubixius came across a strange arrangement of glasses. He saw N glasses kept randomly with a cloud hovering over a glass ni for a certain period of time pouring 1 drop of rain per second. After the time was up it would go to another glass and start dripping water in that glass.

The strange thing about these glasses was that, if at any time a glass was filled to the brim it would spill into the next glass and the last glass would spill into the First glass. The thing that he found stranger was that each glass could only hold up to ci drops of overflow water after which the overflow water would vanish if the glass was not full.

Help Rubixius figure out how many drops of water all the glasses hold after time T.

Input

The input starts with an integer S specifying the number of test cases. Each case will start with N and T which will specify the number of glasses and the total time respectively. The next line will contain the capacity ni of each glass. The following line will have the overflow limit ci for each glass. The last line of the case will have pairs of integers which will be in the form - gi ti - indicating the glass over which the cloud is hovering and for the period which it hovers. The input for each case is such that the total of all the time periods indicated on the last line of the test case will equal the total time T.

Constraints

0<N<50

0<N<10000

0<T<100000000

Output

Output a single line for each case containing the maximum drops that are accumulated after time T.

Comments

Please login at the top to post a comment.

SUCCESSFUL SUBMISSIONS

Fetching successful submissions

HELP

Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:

Accepted Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark.

Time Limit Exceeded Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.

Wrong Answer Your program compiled and ran succesfully but the output did not match the expected output.

Runtime Error Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.

Compilation Error Your code was unable to compile. When you see this icon, click on it for more information.

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to INR 20,000 (for Indian Community), $700 (for Global Community) and lots more CodeChef goodies up for grabs.