CF BUDDY
← Problems·

1060D · Social Circles

1900 · greedy, math

Problem: You invited nn guests to dinner! You plan to arrange one or more circles of chairs. Each chair is going to be either occupied by one guest, or be empty. You can make any number of circles.

Your guests happen to be a little bit shy, so the ii-th guest wants to have a least lil_i free chairs to the left of his chair, and at least rir_i free chairs to the right. The "left" and "right" directions are chosen assuming all guests are going to be seated towards the center of the circle. Note that when a guest is the only one in his circle, the lil_i chairs to his left and rir_i chairs to his right may overlap.

What is smallest total number of chairs you have to use?

Input Format: First line contains one integer nn  — number of guests, (1n1051 \leqslant n \leqslant 10^5).

Next nn lines contain nn pairs of space-separated integers lil_i and rir_i (0li,ri1090 \leqslant l_i, r_i \leqslant 10^9).

Output Format: Output a single integer — the smallest number of chairs you have to use.

Note: In the second sample the only optimal answer is to use two circles: a circle with 55 chairs accomodating guests 11 and 22, and another one with 1010 chairs accomodationg guests 33 and 44.

In the third sample, you have only one circle with one person. The guest should have at least five free chairs to his left, and at least six free chairs to his right to the next person, which is in this case the guest herself. So, overall number of chairs should be at least 6+1=7.

Sample Cases

Case 1

Input

3
1 1
1 1
1 1

Output

6

Case 2

Input

4
1 2
2 1
3 5
5 3

Output

15

Case 3

Input

1
5 6

Output

7

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback