#P7760. [COCI 2016/2017 #5] TUNA

[COCI 2016/2017 #5] TUNA

Description

A fisherman caught NN tuna last night. He plans to sell these fish to a platform.

For each fish, the platform provides two estimated values P1,P2P_1, P_2. If the difference between these two values is no more than XX, then the larger one is taken as the value of the fish. Otherwise, if the difference exceeds XX, then another value P3P_3 is taken.

Now you are given the values for NN fish (each fish may have 22 or 33 values). Find the total value of all fish.

Input Format

The first line contains an integer NN, the number of tuna.

The second line contains an integer XX.

The following lines are divided into NN parts, each describing one fish. Each part has one of the following two formats:

  • The part consists of exactly one line containing two integers P1,P2P_1, P_2.
  • The part consists of two lines: the first line contains two integers P1,P2P_1, P_2, and the second line contains one integer P3P_3.

Output Format

Output the total value of all fish.

5
2
3 4
2 1
5 3
4 4
4 2
19
4
2
3 5
2 8
4
6 5
6 3
7
22
3
10
20 50
30
20 40
50
70 20
10
90

Hint

Sample 2 Explanation.

The fisherman caught 44 tuna, and the value of XX is 22:

Tuna ID p1p_1 p2p_2 p3p_3 Difference Final value
11 33 55 / 222 \le 2 55
22 88 44 6>26 \gt 2 44
33 66 55 / 121 \le 2 66
44 33 77 373 \le 7 77

So the total value is 5+4+6+7=225+4+6+7=22.

Constraints.

For 100%100\% of the testdata, 1N201 \le N \le 20, 1X101 \le X \le 10, 1P1,P2,P31001 \le P_1, P_2, P_3 \le 100.

Notes.

Translated from COCI 2016-2017 CONTEST #5 T1 Tuna.

The score of this problem follows the original COCI problem, with a full score of 5050.

Translated by ChatGPT 5