Codeforces Round #513 by Barcelona Bootcamp (rated, Div. 1 + Div. 2) D. Social Circles 【贪心】

D. Social Circles

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

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 lili free chairs to the left of his chair, and at least riri 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 lili chairs to his left and riri chairs to his right may overlap.

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

Input

First line contains one integer nn  — number of guests, (1⩽n⩽1051⩽n⩽105).

Next nn lines contain nn pairs of space-separated integers lili and riri (0⩽li,ri⩽1090⩽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 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.

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX = 2e6 + 3;
int a[MAX], b[MAX];

int main(){
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        scanf("%d%d", &a[i], &b[i]);
    }
    sort(a, a + n);
    sort(b, b + n);
    ll ans = 0;
    for(int i = 0; i < n; i++){
        ans += max(a[i], b[i]) + 1;
    }
    printf("%lld", ans);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值