201474: [AtCoder]ARC147 E - Examination

Memory Limit:1024 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $800$ points

Problem Statement

$N$ students, numbered $1,2,\ldots,N$, took an examination. Student $i\,(1 \leq i \leq N)$ had to score at least $B_i$ points to graduate, where they actually scored $A_i$ points.

You can repeat the following operation any number of times (possibly zero):

  • Choose two students, and swap their scores.

Your goal is to make everyone graduate. Determine whether it is possible. If it is possible, find the maximum number of students whose scores do not change during the process.

Constraints

  • $2 \leq N \leq 3 \times 10^5$
  • $1 \leq A_i,B_i \leq 10^9\,(1 \leq i \leq N)$
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$N$
$A_1$ $B_1$
$A_2$ $B_2$
$\vdots$
$A_N$ $B_N$

Output

If it is possible to make everyone graduate, print the maximum number of students whose scores do not change during the process.

Otherwise, print -1.


Sample Input 1

3
1 2
3 1
3 3

Sample Output 1

1

If you swap scores of Student $1$ and $2$, everyone can graduate. Here, the number of students whose scores do not change is $1$ (only Student $3$).


Sample Input 2

2
100 1
100 1

Sample Output 2

2

Sample Input 3

6
3 2
1 6
4 5
1 3
5 5
9 8

Sample Output 3

-1

Sample Input 4

6
3 1
4 5
5 2
2 3
5 4
5 1

Sample Output 4

3

Input

题意翻译

有 $n$ 个学生,第 $i$ 个人需要得到至少 $B_i$ 分,但是目前只有 $A_i$ 分。 你可以任意地交换两个学生的分数,以使得所有学生都能得到他需要的分数。 求最多能在多少学生不与别人交换分数的情况下,使得所有学生都能得到他需要的分数。无解输出 $-1$。

加入题单

算法标签: