408411: GYM103115 E is the order a rabbit ??

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

E. is the order a rabbit ??time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

In a remote place, there is a rabbits market where rabbits can be bought and sold in every day. The trading rules in this market are as follows:

1.The market is available only in morning and afternoon in one day. And the rabbit transaction price may be different every day even different between morning and afternoon in a day.

2.It is prohibited that purchasing more than once in a day. And only one rabbit can be purchased at a time.

3. You can only sell once in a day, but you can sell rabbits unlimited quantities (provided that you have so many rabbits)

Mr.Cocktail has traded in this market for N days, suppose he has unlimited money to buy rabbits. Before the first day and after the last day, he has no rabbits. Now, in these N days, how much money did he earn the most?

Input

The first line contains a positive integer N, which means there are N days in total $$$(1 \leq n \leq 10^5)$$$

Next, there are N lines, each line contains two positive integers, representing the rabbit price in the morning and afternoon of the day $$$a_i,b_i(1 \leq a_i ,b_i \leq 10^9)$$$.

Output

Output an integer on a line to indicate the answer.

ExamplesInput
3
1 6
2 3
7 1
Output
11
Input
2
5 4
3 2
Output
0
Note

In example 1, a rabbit was purchased for $1 in the morning of the first day, and a rabbit was purchased for $2 in the afternoon of the second day. On the morning of the third day, the two rabbits were sold, a total profit of $11.

The price of the rabbit in sample 2 continues to decrease, and it is impossible to make money through buying and selling, so choose not to make any buying and selling, and output the answer 0.

加入题单

上一题 下一题 算法标签: