This page is still under construction.

Parts of this page are still being built. What you see may change.

To Fish or Not to Fish

Time limit1sMemory limit512 MB

Summary
Choose fishing points to visit and bases to sell at along a river, paying fuel only while moving upstream, to maximize profit.
Level

Hard8 of 10

Topics
Greedy, Sorting, Prefix sum, Dynamic programming
Solved
No attempts yet

Problem

The owners of a fishing vessel working the Kama River decided to optimize their business for the summer season.

They obtained a seasonal permit to fish at nn points along the river at distances x1,x2,…,xnx_1, x_2, \ldots, x_n kilometers from the mouth. At point number ii, at most aia_i tons of fish may be caught.

The fish caught can be sold at mm wholesale bases located along the river bank at distances y1,y2,…,ymy_1, y_2, \ldots, y_m kilometers from the mouth. The base at point number jj is willing to buy at most bjb_j tons of fish this season at a price of cjc_j rubles per ton.

Distances from the mouth to the fishing points and wholesale bases are measured along the river channel.

The vessel sets out to fish from the river mouth and must return there after the season ends. During the season, the vessel may travel freely up and down the river, stopping to fish or to sell fish. The vessel's carrying capacity is sufficient to transport any amount of caught fish. When moving away from the mouth, the vessel travels against the current and spends fuel worth pp rubles per kilometer. When moving toward the mouth, the vessel travels with the current and therefore spends no fuel.

At the end of the season, the profit from the catch equals the total value of the fish sold minus the total cost of the fuel spent.

Write a program that determines the maximum profit that can be obtained during the season.

Input

The first line of the input contains three integers nn, mm, and pp: the number of fishing points, the number of wholesale bases, and the fuel price (1≤n,m≤500 0001 \le n, m \le 500\,000; 0≤p≤1090 \le p \le 10^9).

The next nn lines contain two integers each, xix_i and aia_i: the distance from the mouth and the maximum catch for each fishing point (0<x1<x2<…<xn≤1090 < x_1 < x_2 < \ldots < x_n \le 10^9; 0<ai≤1060 < a_i \le 10^6).

The next mm lines contain three integers each, yjy_j, bjb_j, and cjc_j: the distance from the mouth, the maximum amount of fish bought in tons, and the purchase price per ton for each wholesale base (0<y1<y2<…<ym≤1090 < y_1 < y_2 < \ldots < y_m \le 10^9; 0<bj,cj≤1060 < b_j, c_j \le 10^6).

Output

The output must contain a single integer: the maximum possible profit.

Hint

In the second example, the following actions are optimal. The vessel should reach the point at a distance of 6 kilometers from the mouth, spending 600 rubles on fuel, and catch 5 tons of fish there. After that, it should go 1 kilometer down the river to the base at a distance of 5 kilometers from the mouth and sell the caught fish at a price of 2000 rubles per ton. Then it should return to the river mouth. The total profit will be 9400 rubles.

Examples3

  1. Example 1

    Input
    3 2 0
    1 5
    2 3
    4 5
    2 2 10
    3 6 5
    
    Expected output
    50
    
  2. Example 2

    Input
    2 1 100
    6 5
    100 4
    5 100 2000
    
    Expected output
    9400
    
  3. Example 3

    Input
    3 3 10
    1 1
    10 100
    20 10
    2 1000 1
    11 50 50
    17 50 2
    
    Expected output
    2441