## Vending Machine

View as PDF

Points: 10 (partial)
Time limit: 1.0s
Memory limit: 256M

Author:
Problem type

One of Japan's distinctive features are the millions of vending machines spread all over the country. You happen to stumble across one, and decide to spend up to yen in exchange for some of its tasty drinks. There are vending slots in the vending machine, each with two types of drinks. For slot , the first type of drink costs yen and has a tastiness value of , whereas the second type of drink costs yen and has a tastiness value of . To ensure equal sales of drinks in the same slot, for any given slot, the machine will only offer the type of drink you purchased less of, offering both drinks if you've purchased the same amount of each type. Note that the drinks offered are independent for each slot. For example, if there are two slots and you purchased more type drinks from slot but the same amount of type and type drinks from slot , slot will only offer the type drink while slot still offers both types. As a lover of tasty drinks, please find the maximum possible sum of tastiness values you can get by spending up to yen on the drinks.

For all , and .

For all , .

#### Input Specification

The first line contains integers and .

The next lines each contain integers and .

#### Output Specification

Output one integer, the maximum possible sum of tastiness values you can get by spending up to yen on the drinks.

#### Sample Input 1

1 1000
300 4 400 9

#### Sample Output 1

17

#### Explanation for Sample 1

One possible plan is to purchase the second type of drink first, having a tastiness value of and leaving you with yen. Then, the machine only allows you to purchase the first type of drink since you bought less of it. After purchasing the first type of drink, the total tastiness value is and you have yen left. Both drinks are available once again, but you can only afford one more of the first type, leaving you with a total tastiness value of in the end.

This sample input satisfies the constraints of subtasks and .

#### Sample Input 2

3 2000
123 5 123 5
213 9 213 9
321 12 321 12

#### Sample Output 2

83

#### Explanation for Sample 2

The best decision is for you to get drinks from the first slot and drinks from the second (it doesn't matter which type of drink you purchase from each slot since they have the same costs and tastiness values). This gives a total tastiness value of .

This sample input satisfies the constraints of subtasks and .

#### Sample Input 3

4 1500
314 15 100000 29358203
926 53 100000 1249284
589 79 100000 22667121
323 84 100000 47458321

#### Sample Output 3

178

#### Explanation for Sample 3

To maximize the sum of tastiness values, you should get one drink of the first type from each of slots and . This gives a total tastiness value of .

This sample input satisfies the constraints of subtasks and .

#### Sample Input 4

5 100000
271 828182845 904 523536028
747 135266249 775 724709369
995 957496696 762 772407663
353 547594571 382 178525166
427 427466391 932 305992181

#### Sample Output 4

115347629139

#### Explanation for Sample 4

It is important to note that the answer may not fit in a -bit data type.

This sample input satisfies the constraints of subtask .