#P7760. Tuna Sale Valuation

    ID: 20947 Type: Default 1000ms 256MiB

Tuna Sale Valuation

Tuna Sale Valuation

A fisherman caught N tunas last night and is preparing to sell them on a platform. For each fish, the platform provides two estimated values, \(P_1\) and \(P_2\). If the absolute difference between the two estimates satisfies \(|P_1-P_2| \leq X\), the fish's value is taken as \(\max(P_1, P_2)\). Otherwise, if \(|P_1-P_2| > X\), an additional estimate \(P_3\) is provided, and the fish's value is \(P_3\).

Your task is to compute the total value of all the fish.

Input Format:

  • The first line contains two integers \(N\) and \(X\), where \(N\) is the number of fish and \(X\) is the threshold for the difference.
  • The following \(N\) lines each describe a fish. Each line contains either 2 or 3 integers. The first two integers are \(P_1\) and \(P_2\). If only two values are provided, it is guaranteed that \(|P_1-P_2| \leq X\). If three values are provided, it indicates that \(|P_1-P_2| > X\) and the third integer is \(P_3\).

Output Format:

Output a single integer: the sum of the values of all fish.

inputFormat

The input begins with a line containing two space-separated integers \(N\) and \(X\). Each of the next \(N\) lines contains either two or three space-separated integers. For a line with two integers, these represent \(P_1\) and \(P_2\) (with \(|P_1-P_2| \leq X\)). For a line with three integers, they represent \(P_1\), \(P_2\) (with \(|P_1-P_2| > X\)), and \(P_3\) respectively.

outputFormat

Output the total sum of the values of the fish as a single integer.

sample

3 5
10 13
7 2
20 10 15
35