Codeforces Round 513 by Barcelona Bootcamp (rated, Div. 1 + Div. 2)
D
Social Circles
Codeforces
1900
原题
← CE →
翻译中…

You invited $n$ 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 $i$-th guest wants to have a least $l_i$ free chairs to the left of his chair, and at least $r_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 $l_i$ chairs to his left and $r_i$ chairs to his right may overlap.

翻译中…

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

翻译中…

Input

翻译中…

First line contains one integer $n$  — number of guests, ($1 \leqslant n \leqslant 10^5$).

翻译中…

Next $n$ lines contain $n$ pairs of space-separated integers $l_i$ and $r_i$ ($0 \leqslant l_i, r_i \leqslant 10^9$).

翻译中…

Output

翻译中…

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 $5$ chairs accomodating guests $1$ and $2$, and another one with $10$ chairs accomodationg guests $3$ and $4$.

翻译中…

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.

翻译中…

样例

Input #1
3
1 1
1 1
1 1
Output #1
6
InputCopy OutputCopy
Input #2
4
1 2
2 1
3 5
5 3
Output #2
15
InputCopy OutputCopy
Input #3
1
5 6
Output #3
7
InputCopy OutputCopy
题目信息
题号
1060D
时间限制
2 s
内存限制
512 MB
标签
贪心
数学

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。