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<iostream>
#include<algorithm>
using namespace std;
pair <int, int> a[105];
int main()
{
int n, count = 0;
cin >> n;
for (int i = 0; i<n; 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); i<n; i++)
sum += a[i].second;
}
cout << sum << endl;
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值