COCI '16 Contest 5 #1 Tuna

View as PDF

Submit solution


Points: 3 (partial)
Time limit: 1.0s
Memory limit: 64M

Problem type

Fisherman Šime caught N 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, P1 and P2. If the difference between the estimates is less than or equal to X, then the higher value is taken. If the difference is strictly larger than X, the app returns a third estimate P3 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 N tunas, output the total value of caught tunas.

Input Specification

The first line of input contains the integer N (1N20), the number of tunas from the task.

The second line of input contains the integer X (1X10), the number from the task.

Then, N blocks follow in one of the two following forms:

  • In one line, two integers P1 and P2 (1P1,P2100) from the task.

or

  • In one line, two integers P1 and P2 (1P1,P2100) from the task, and in the second line an integer P3 (1P3100) from the task.

Output Specification

The first and only line of output must contain the total value of caught tunas.

Sample Input 1

Copy
5
2
3 4
2 1
5 3
4 4
4 2

Sample Output 1

Copy
19

Sample Input 2

Copy
4
2
3 5
2 8
4
6 5
6 3
7

Sample Output 2

Copy
22

Explanation for Sample Output 2

Šime caught 4 tunas. For the first tuna, the app returned two estimates (3 and 5). Since the difference between these two estimates is less than or equal to 2, the value of the first tuna is 5. For the second tuna, the difference between the first two estimates (2 and 8) is higher than 2, so the app returned a third estimate, 4. The third tuna's value is 6 (652), and the value of the fourth tuna is taken as the third estimate, 7, because the difference between the given estimates (6 and 3) is higher than 2.

Sample Input 3

Copy
3
10
20 50
30
20 40
50
70 20
10

Sample Output 3

Copy
90

Comments

There are no comments at the moment.