Woburn Challenge 2015-16 Round 1 - Junior Division

Problem 4: Trip Budgeting

The PEG trip at the end of the school year is a long-lasting tradition of the club. The leaders usually decide on three teams (junior, intermediate, and senior) based on whom they find to be most attentive and hard-working throughout the years. Then, the deserving teams are taken to some place in the USA (a different city every year!) where they will compete, and most probably dominate, in the American Computer Science League (ACSL) All-Star competition. The trip will then consist of several action-packed days of competing, dining, frolicking, and touring the local attractions. While most PEG students who are selected to go are simple enlightened by the fantastic opportunity, few of them are aware of the rigorous planning and budgeting that takes place behind-the-scenes.

Budgeting is a complex task. We must account for transportation, hotel, food, and most importantly – attractions. Choices must be made in each of these categories with consideration for how much money is going to be spent, and how exciting the trip is expected to be. To make each trip memorable enough for the PEG history books, the PEG leaders would like this year's trip to have at least an excitement level of Emin (1 ≤ Emin ≤ 109) units. Funding has always been a complicated matter due to Woburn's Student Activity Council (SAC). The SAC is willing to help the club satisfy its minimal excitement level, provided that it is done in the cheapest way possible. The cost and excitement of the trip will be calculated with the following rules:

If attended, the i-th attraction (for i = 1..N) will contribute Ei units of excitement to the overall trip.

By going to a particular attraction, money may be saved or extra money may be spent. For instance, the team might have to pay extra transportation costs to reach the event. The team might also have to pick a more expensive hotel in order to be close enough to attend it. Conversely, many attractions provide free food, so the team might actually save money on food.

By choosing attraction i, the team must be prepared to also pay an additional Ti dollars for transportation on top of Tbase. That is, the overall transportation cost is the sum of the base transportation cost and the transportation costs incurred across all of the attractions they decide to visit.

Each attraction has an associated hotel nearby that costs Hi dollars to stay at. Since the team will not be attending more than one hotel, we have to prepare for the worst and assume that the overall hotel cost for the entire trip will be the maximum of Hbase and Hi across all of the attractions that the team eventually decides to attend.

By choosing attraction i, the team will actually saveFi dollars on food because it will be provided at the venue. That is, the overall food cost will be the base food cost Fbase subtract the Fi for each attraction i that was attended. The base food cost cannot go below zero – after Fbase has been reduced to zero, attending additional attractions will have no effect on the nonexistent overall food cost.

The total cost of the trip is equal to the sum of the overall costs of transportation, hotel, and food respectively.

The excitement of the trip is equal to the sum of Ei across all of the attractions that the team decides to attend.

Given this, please help the PEG leaders determine the minimum possible total cost that will produce at least Emin units of excitement for the overall trip. It is guaranteed that this is possible.

Input Format

Line 1 will contain four space-separated integers in the following order: Emin, Tbase, Hbase, and Fbase.
Line 2 will contain a single integer N.
There will be N lines to follow. The i-th of these lines (for i = 1..N) will contain four space-separated integers: Ei, Ti, Hi, and Fi.

Output Format

Output a single integer – the minimum cost (in dollars) required to achieve at least Emin units of excitement on the trip.

Sample Input

Sample Output

24600

Explanation

The team wants to achieve a minimum of 50 units of excitement, and has given the five attractions with excitement ratings of 30, 40, 10, 30, and 20 respectively. By choosing every attraction except for number 2, the team is actually able to achieve 90 units of excitement at a cost of 24600 dollars. The costs are calculated as follows:

Transportation costs 2000 + 300 + 100 + 500 + 400 = 3300 dollars.

The most expensive hotel across all of the attractions being attended costs 20000 dollars.

Food was original going to cost 4000 dollars, but 400 + 800 + 1000 + 500 = 2700 dollars were saved by eating at the venues of the attractions.

Thus, the total cost of the trip is 3300 + 20000 + (4000 − 2700) = 24600 dollars. In fact, this is the best possible answer.

E_min can actually be as low as 0, not 1.
Unfortunately, data reflecting this has already been used in the live contest.
Fortunately, from inspecting the submissions, it did not appear to have actually impacted anybody's ranks.

E_min can actually be as low as 0, not 1. Unfortunately, data reflecting this has already been used in the live contest. Fortunately, from inspecting the submissions, it did not appear to have actually impacted anybody's ranks.