Fisherman Šime caught tunas last night. With the help of a special app, he offered them for sale to a famous Japanese company that specializes in purchasing quality fish. In what way does the app estimate the value, or the price, of a tuna?
Based on the photo of the tuna, the app returns two estimated values, and . If the difference between the estimates is less than or equal to , then the higher value is taken. If the difference is strictly larger than , the app returns a third estimate and then that estimate is taken as the final value of the tuna.
Write a programme that will, based on the given estimates (sometimes two, sometimes three of them) for each of tunas, output the total value of caught tunas.
Input Specification
The first line of input contains the integer , the number of tunas from the task.
The second line of input contains the integer , the number from the task.
Then, blocks follow in one of the two following forms:
- In one line, two integers and from the task.
or
- In one line, two integers and from the task, and in the second line an integer from the task.
Output Specification
The first and only line of output must contain the total value of caught tunas.
Sample Input 1
5
2
3 4
2 1
5 3
4 4
4 2
Sample Output 1
19
Sample Input 2
4
2
3 5
2 8
4
6 5
6 3
7
Sample Output 2
22
Explanation for Sample Output 2
Šime caught tunas. For the first tuna, the app returned two estimates ( and ). Since the difference between these two estimates is less than or equal to , the value of the first tuna is . For the second tuna, the difference between the first two estimates ( and ) is higher than , so the app returned a third estimate, . The third tuna's value is , and the value of the fourth tuna is taken as the third estimate, , because the difference between the given estimates ( and ) is higher than .
Sample Input 3
3
10
20 50
30
20 40
50
70 20
10
Sample Output 3
90
Comments