Treasure Map

On a weighted undirected graph, gold decays each day at every mine; starting at mine 1 with forced moves, maximize total gold collected before stopping.

Hard9GraphDynamic programmingShortest pathGreedyNo attempts yetTime limit2sMemory limit512 MB

Problem

You have found a treasure map. It leads to nn gold mines. Each mine produces gold every day, but the daily haul shrinks by a fixed amount as the days pass. Mine ii produces gig_i on day 1 and loses did_i per day, so on day kk mine ii produces max(0, gi(k1)×di)\max(0,\ g_i - (k-1) \times d_i). A mine never produces a negative amount.

Paths connect the mines, and crossing one path takes a whole number of days. You collect nothing while you travel.

On day 1 you stand at mine 1 and take all of that day's gold from it. You cannot stay at the same mine on two days in a row, so after collecting you have to leave along a path. You may come back to a mine you left earlier, and on the day you come back you take that mine's gold for that day again. You may end the trip at any moment.

Find the largest total amount of gold you can collect.

Input

The first line contains the number of mines nn and the number of paths mm. (2n10002 \le n \le 1000, 1m10001 \le m \le 1000)

Each of the next nn lines describes one mine with its day 1 output gg and its daily decrease dd. (1g10001 \le g \le 1000, 1d10001 \le d \le 1000) For example, if g=9g = 9 and d=4d = 4, the mine produces 9 on day 1, 5 on day 2, 1 on day 3, and 0 from day 4 on. The mines are numbered 1 to nn in the order they appear in the input, and you start at mine 1.

Each of the next mm lines describes one path with the two mine numbers aa and bb it joins and the number of days tt needed to cross it. (1a<bn1 \le a < b \le n, 1t1001 \le t \le 100) A path runs both ways, so it can be used from aa to bb and from bb to aa.

Output

Print the largest total amount of gold you can collect as a single integer.