Broken Vending Machine

No attempts yetTime limit1sMemory limit256 MB

Problem

Bob found a vending machine that is broken, and he wants to make money from it. The machine has nn positions, numbered 1 to nn. Position ii holds sis_i snacks, one press of its button costs pip_i, and one snack from that position sells on the market for mim_i.

The machine is broken in a consistent way. When Bob presses the button at position ii, the machine charges him pip_i and then dispenses one snack from position f(i)f(i) instead of position ii. If position f(i)f(i) is already empty, Bob pays and receives nothing. A press at position ii never reduces the number of snacks at position ii; only the position the snack came out of loses one. Bob may press any button as many times as he likes, in any order, and he pays for every press.

Bob sells every snack he receives at its market price, and a snack that came out of position jj sells for mjm_j. He has enough money to pay for any number of presses. Find the largest net gain Bob can reach, that is, the total market price of the snacks he receives minus the total amount he pays. Pressing nothing at all is allowed.

Input

The first line contains the number of positions nn (1n1051 \le n \le 10^5). Each of the next nn lines describes one position, from position 1 to position nn, with four integers ff, pp, mm, ss.

  • ff is the position f(i)f(i) the machine dispenses from when Bob presses this position's button (1fn1 \le f \le n).
  • pp is the price of one press at this position (1p1061 \le p \le 10^6).
  • mm is the market price of a snack at this position (1m1061 \le m \le 10^6).
  • ss is the number of snacks at this position (1s1061 \le s \le 10^6).

Output

Print the largest net gain Bob can reach as a single integer on one line.