L - Lala Land and Apple Trees

Amr lives in Lala Land. Lala Land is a very beautiful country that is located on a coordinate line. Lala Land is famous with its apple trees growing everywhere.

Lala Land has exactly n apple trees. Tree number i is located in a position xi and has ai apples growing on it. Amr wants to collect apples from the apple trees. Amr currently stands in x = 0 position. At the beginning, he can choose whether to go right or left. He'll continue in his direction until he meets an apple tree he didn't visit before. He'll take all of its apples and then reverse his direction, continue walking in this direction until he meets another apple tree he didn't visit before and so on. In the other words, Amr reverses his direction when visiting each new apple tree. Amr will stop collecting apples when there are no more trees he didn't visit in the direction he is facing.

What is the maximum number of apples he can collect?

Input

The first line contains one number n (1 ≤ n ≤ 100), the number of apple trees in Lala Land.

The following n lines contains two integers each xiai ( - 105 ≤ xi ≤ 105xi ≠ 01 ≤ ai ≤ 105), representing the position of the i-th tree and number of apples on it.

It's guaranteed that there is at most one apple tree at each coordinate. It's guaranteed that no tree grows in point 0.

Output

Output the maximum number of apples Amr can collect.

Example
Input
2
-1 5
1 5
Output
10
Input
3
-2 2
1 4
-1 3
Output
9
Input
3
1 9
3 5
7 10
Output
9
这题很明显先判断左边树多还是右边树多,哪边树多,就从这边先开始,在给完数据,先按树离原点的距离排序(交换时,要连同苹果树上的苹果数量一起换),这里我用了pair函数就足够了,但是基本的做法应该是用结构体,因为没做很多题现在,所以一下字都没反应过来,以后类似的就直接用结构体就好了。

#include
#include
using namespace std;
pair a[105];
int main()
{
int n, count = 0;
cin >> n;
for (int i = 0; i{
cin >> a[i].first >> a[i].second;
if (a[i].first<0)
{
count++;
}
}
sort(a, a + n);
int sum = 0;
if (2 * count < n)
{
for (int i = 0; i < min(count * 2 + 1, n); i++)
sum += a[i].second;
}
else
{
for (int i = max(count * 2 - n - 1, 0); isum += a[i].second;
}
cout << sum << endl;
return 0;
}


你可能感兴趣的:(平时学习对应知识点练习题)