D. Social Circles

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 li free chairs to the left of his chair, and at least ri 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 li chairs to his left and ri

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⩽n⩽105

).

Next n

lines contain n pairs of space-separated integers li and ri (0⩽li,ri⩽109

).

Output

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

Examples

Input

Copy

3
1 1
1 1
1 1

Output

Copy

6

Input

Copy

4
1 2
2 1
3 5
5 3

Output

Copy

15

Input

Copy

1
5 6

Output

Copy

7

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.

看到这道题,脑子里瞬间闪现 多校的一道训练题,是左右括号匹配,让我们求最大值。 确实是相同的思想,但不是一道题,还是对贪心的含义理解的太浅。 那道题是让我们求最大的匹配数,可以不停的往后面走,携带着rest的左括号。

这道题显然不是,只是让我们求都不动的两两之间的最大值。

考虑只排序右手,那么我们肯定想匹配 给这双手最近的左手,这样才能浪费最小,否则差值太大。  那我们直接对左手排序,就好了。 还是 差点思想。

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;

#define rep(i,a,b) for(int i=a;i<b;++i)
#define per(i,a,b) for(int i=b-1;i>=a;--i)

const int N=1e5+10;

int L[N],R[N];

int main(){

	int n;
	scanf("%d",&n);

	for(int i=0;i<n;i++){
		scanf("%d %d",&L[i],&R[i]);
	}
	sort(L,L+n);
	sort(R,R+n);

	LL ans=0;

	rep(i,0,n){
		ans=ans+1+max(L[i],R[i]);
	}

	printf("%I64d\n",ans);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值