## Stardust Snow

View as PDF

Points: 20 (partial)
Time limit: 1.8s
Memory limit: 64M

Authors:
Problem type

It's almost time to say goodbye! Griffy would like to give Don Mills a parting present, and thought snow would be a good idea! However, not just any ordinary snow; sparkling stardust snow from the Alfheim Alpines! The Alfheim Alpines are strange: it is made of clouds metres above a single bridge of length , and is covered in fog! The entire alpine range can be seen as a 2 dimensional grid ( rows by columns) in which snow falls.

Everyone knows that snow quality is the sum of the sparkling values of all snowflakes it is made of, and snow temperature is the sum of the temperatures of all the snowflakes it is made of. Unfortunately, the backpack Griffy is using is old and about to fall apart, so it cannot hold a total snow temperature that is greater than or equal to , or else it will explode. There are snowflakes that will fall after Griffy arrives. Each stardust snowflake has a temperature and a sparkling value . Because of the fog, you don't always spot snowflakes from the clouds. Instead, they will come into your view horizontal metres from the start of the bridge, and above the height of the bridge. Griffy is not greedy, so he will only collect a maximum of snowflakes.

Because you want to maintain relations for future programming contests, you decide to help Griffy by writing a program that will maximize the snow quality Griffy can get. Griffy starts at horizontal position and can choose to walk up to integer units left or right each second. For example, if , then Griffy can walk any of units. Each snowflake will stay at its horizontal position and fall vertically at a rate of metre per second. Snowflakes can ONLY be collected if the snowflake is at a height of exactly , and if Griffy is at the same horizontal position as the snowflake at the same second. Griffy can take at most snowflake each second. Griffy can choose not to take snowflakes, and no two snowflakes will appear at the same position and time.

#### Input Specification

First line: 6 integers, space separated , , , , , , .

Lines to : 4 integers per line, space separated , , , , , .

#### Output Specification

One integer, the maximum snow quality Griffy can get.

#### Sample Input

2 2 2 10 10 3
4 8 1 1
4 6 2 2

#### Sample Output

14

#### Explanation for Sample Output

There are two snowflakes within a by range. The maximum temperature Griffy's bag can hold is and he will take at most snowflakes. Griffy can walk up to units each second. One snowflake will fall from coordinates with temperature and sparkling value of . The other will fall from coordinates with temperature and sparkling value .

Griffy can take the first snowflake at time 1 and then move to the right by one unit to reach the second snowflake at time 2. This way he will have a total temperature of and snow quality of . This is the highest quality snow he will be able to get.