Statement
According to a new study, about 8 out of every 10 runners are clinically deficient in Kudos—and, worse, most of them do not even know it.

Bruno is a runner who loves posting his activities on the social network Sbava, mainly to earn likes from his friends on the app, called Kudos. However, his recent activities have earned few Kudos, so he came up with a solution.
He recorded a car activity and will use it to post several runs, thereby earning Kudos. He will cut his activity into disjoint segments of 5 km, 10 km, 21 km, or 42 km, using only integer kilometer positions (he will not cut out a 5 km run that took place from kilometer 1.5 to 6.5 of the car activity, for example).
The car activity consists of segments, the -th of which is kilometers long. Because each race distance has very different requirements and circumstances, each segment gives Bruno different benefits. These benefits are measured in Kudos per kilometer, and every race distance has its own rate in every segment. That is, in segment , the integers , , , and represent how many Kudos Bruno receives for each kilometer covered as part of a 5, 10, 21, or 42 km run, respectively.
Suppose, for example, that the car activity consists of two 50 km segments. In the first segment, each kilometer gives Bruno 1 Kudo if it is part of a 5, 10, or 21 km run, but 10 Kudos if it is part of a 42 km run. In the second segment, each kilometer gives 1 Kudo if it is part of a 5, 21, or 42 km run, and 2 Kudos if it is part of a 10 km run. Bruno would split the route into two 42 km runs followed by one 10 km run and one 5 km run, in that order, as illustrated below. The final kilometer of the car activity is unused and earns 0 Kudos.

Representation of Sample 1. Bruno receives Kudos.
Help Bruno determine the maximum number of Kudos he can earn by cutting the car activity into runs optimally.
Input
The first line contains an integer (), the number of segments in the car activity. The next lines describe the segments. The -th line contains five integers (, ): the segment length and the number of Kudos each kilometer in that segment gives when included in a 5, 10, 21, or 42 km activity, respectively.
Output
Print one integer: the maximum number of Kudos Bruno can earn.
Examples
Input
2
50 1 1 1 10
50 1 2 1 1
Output
559
Input
3
85 1 1 1 100
471 1 100 100 1
85 1 1 1 100
Output
63902
Tutorial
A direct position DP is correct but the route may have length . Inside one constant-rate segment, let be the race length with maximum rate. Far enough from both segment boundaries, replacing any block of km by copies of this best race cannot hurt.
Consequently only a bounded boundary zone matters. Keep kilometers of each segment; greedily fill the removed middle with the locally best race. Run the ordinary weighted interval DP on the shortened route, allowing a chosen race to cross into the next segment and summing the appropriate per-kilometer rates on both sides.
There are states and four transitions per state, hence time with this constant and memory.