8445: Mountain Climbing

内存限制:128 MB 时间限制:1 S
题面:传统 评测方式:文本比较 上传者:
提交:1 通过:1

题目描述

Farmer John has discovered that his cows produce higher quality milk when they are subject to strenuous exercise. He therefore decides to send his N cows (1 <= N <= 25,000) to climb up and then back down a nearby mountain! Cow i takes U(i) time to climb up the mountain and then D(i) time to climb down the mountain. Being domesticated cows, each cow needs the help of a farmer for each leg of the climb, but due to the poor economy, there are only two farmers available, Farmer John and his cousin Farmer Don. FJ plans to guide cows for the upward climb, and FD will then guide the cows for the downward climb. Since every cow needs a guide, and there is only one farmer for each part of the voyage, at most one cow may be climbing upward at any point in time (assisted by FJ), and at most one cow may be climbing down at any point in time (assisted by FD). A group of cows may temporarily accumulate at the top of the mountain if they climb up and then need to wait for FD's assistance before climbing down. Cows may climb down in a different order than they climbed up. Please determine the least possible amount of time for all N cows to make the entire journey.

农夫约翰发现,奶牛在剧烈运动后,会产出更高质量的牛奶。

 

因此,他决定送他的 N 头奶牛爬上附近的山,然后再从山上下来。

 

奶牛 i 的上山花费时间为 Ui,下山花费时间为 Di

 

作为家养牛,每头奶牛的爬山都要有农夫全程引导。

 

但是由于经济不景气,现在只有两个农名可用,分别是约翰和他的堂兄唐。

 

其中约翰负责引导奶牛向上爬,唐负责引导奶牛向下爬。

 

由于每头奶牛的爬山全过程都要有向导,而上山或下山都只有一个农夫提供引导,因此在任何时间点最多只能有一头奶牛向上爬(在约翰的协助下),并且在任何时间点最多只能有一头奶牛向下爬(在唐的协助下)。

 

如果一群奶牛爬上了山,它们可能会暂时聚集在山顶,然后等待唐的帮助才能爬下来。

 

奶牛向上爬的顺序可能与向下爬的顺序不同。

 

请确定所有 N 头奶牛完成整个旅程的最短时间。

输入格式

第一行包含整数 N

 

接下来 N 行,每行包含两个整数 Ui Di


输出格式

输出所有奶牛完成整个旅程的最短时间。

输入样例 复制

3
6 4
8 1
2 3

输出样例 复制

17

数据范围与提示

数据范围

1N25000,

1Ui,Di50000