bzoj#P4781. [USACO2017 Open] Paired Up

[USACO2017 Open] Paired Up

题目描述

Farmer John finds that his cows are each easier to milk when they have another cow nearby for moral support. He therefore wants to take his mm cows and partition them into m2\dfrac m 2 pairs. Each pair of cows will then be ushered off to a separate stall in the barn for milking. The milking in each of these m2\dfrac m 2 stalls will take place simultaneously.

To make matters a bit complicated, each of Farmer John's cows has a different milk output. If cows of milk outputs aa and bb are paired up, then it takes a total of a+ba+b units of time to milk them both.

Please help Farmer John determine the minimum possible amount of time the entire milking process will take to complete, assuming he pairs the cows up in the best possible way.

输入格式

The first line of input contains nn.
Each of the next nn lines contains two integers xx and yy, indicating that FJ has xx cows each with milk output yy . The sum of the xx's is mm, the total number of cows.

输出格式

Print out the minimum amount of time it takes FJ's cows to be milked, assuming they are optimally paired up.

3
1 8
2 5
1 2
10

样例解释

Here, if the cows with outputs 8+28+2 are paired up, and those with outputs 5+55+5 are paired up, the both stalls take 1010 units of time for milking. Since milking takes place simultaneously, the entire process would therefore complete after 1010 units of time. Any other pairing would be sub-optimal, resulting in a stall taking more than 1010 units of time to milk.

数据规模与约定

对于 100%100\% 的数据,1n1051 \leq n \leq 10^5m109m \leq 10^9mm 是偶数, 1y1091 \leq y \leq 10^9

题目来源

Silver