P7760 [COCI 2016/2017 #5] TUNA

Description

A fisherman caught $N$ tuna last night. He plans to sell these fish to a platform. For each fish, the platform provides two estimated values $P_1, P_2$. If the difference between these two values is no more than $X$, then the larger one is taken as the value of the fish. Otherwise, if the difference exceeds $X$, then another value $P_3$ is taken. Now you are given the values for $N$ fish (each fish may have $2$ or $3$ values). Find the total value of all fish.

Input Format

The first line contains an integer $N$, the number of tuna. The second line contains an integer $X$. The following lines are divided into $N$ parts, each describing one fish. Each part has one of the following two formats: - The part consists of exactly one line containing two integers $P_1, P_2$. - The part consists of two lines: the first line contains two integers $P_1, P_2$, and the second line contains one integer $P_3$.

Output Format

Output the total value of all fish.

Explanation/Hint

**Sample 2 Explanation.** The fisherman caught $4$ tuna, and the value of $X$ is $2$: |Tuna ID|$p_1$|$p_2$|$p_3$|Difference|Final value| | :----------: | :----------: | :----------: | :----------: | :----------: | :----------: | |$1$|$3$|$5$|/|$2 \le 2$|$5$| |$2$|$2$|$8$|$4$|$6 \gt 2$|$4$| |$3$|$6$|$5$|/|$1 \le 2$|$6$| |$4$|$6$|$3$|$7$|$3 \le 7$|$7$| So the total value is $5+4+6+7=22$. **Constraints.** For $100\%$ of the testdata, $1 \le N \le 20$, $1 \le X \le 10$, $1 \le P_1, P_2, P_3 \le 100$. **Notes.** **Translated from [COCI 2016-2017](https://hsin.hr/coci/archive/2016_2017/) [CONTEST #5](https://hsin.hr/coci/archive/2016_2017/contest5_tasks.pdf) _T1 Tuna_.** **The score of this problem follows the original COCI problem, with a full score of $50$.** Translated by ChatGPT 5