Milk and Honey

Interview

Time limit1sMemory limit1024 MB

Summary
Assign each field to cows or bees to maximize total happiness, where each field's output value declines linearly with each added unit.
Level

Medium4 of 10

Topics
Greedy, Math, Implementation
Solved
No attempts yet

Problem

In the land of milk and honey, Juku is in charge of cows and bees. Bees sting cows and make them unhappy, and cows eat all the flowers bees use to make honey, so cows and bees must be kept on separate fields. Each field can be used for cows or for bees, but not both.

Cows and bees are free to obtain, so a field used for cows is filled with the maximum CC cows it can support, and a field used for bees is filled with BB bees.

Each cow produces one unit of milk and each bee produces one unit of honey. Milk and honey give different amounts of happiness when consumed, and customers value rare goods more highly. So, within a single field, the first unit of milk produces MM units of happiness, the second only M−DMM - D_M, the third M−2⋅DMM - 2 \cdot D_M, and so on (a value never drops below 00). The same rule holds for honey with HH and DHD_H.

Juku wants to decide, for each field, whether to raise cows or bees so that the total happiness is maximized. There are far too many cases to work out by hand, so compute the maximum for Juku.

Input

The first line contains two integers: MM (0≤M≤10000 \le M \le 1000), the happiness of the first unit of milk, and DMD_M (0≤DM≤M0 \le D_M \le M), the amount by which the happiness value drops for each additional unit of milk on a field.

The second line contains two integers HH and DHD_H (0≤DH≤H≤10000 \le D_H \le H \le 1000), giving the same information for honey.

The third line contains NN (1≤N≤10001 \le N \le 1000), the number of fields. Each of the next NN lines contains CC (0≤C≤1000 \le C \le 100) and BB (0≤B≤1000 \le B \le 100), the number of cows and the number of bees that field can support.

Output

Print a single line containing the maximum total happiness achievable.

Examples2

  1. Example 1

    Input
    3 0
    5 0
    3
    4 2
    3 2
    2 1
    
    Expected output
    28
    
  2. Example 2

    Input
    7 4
    5 2
    3
    2 2
    1 3
    3 1
    
    Expected output
    29