5170: 啦地和苹果树

内存限制:256 MB 时间限制:1 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:4 通过:4

题目描述

  阿姆尔住在拉拉地。拉拉地是一个非常美丽的国家,位于坐标线上。拉拉土地以其到处生长的苹果树而闻名。
  拉拉兰有整整n棵苹果树。树号i位于位置xi,上面生长着AI苹果。阿姆尔想从苹果树上收集苹果。Amr目前处于x=0位置。一开始,他可以选择是向右还是向左。他会继续朝他的方向走,直到他遇到一棵他以前没有去过的苹果树。他会拿走所有的苹果,然后调转方向,继续朝这个方向走,直到他遇到另一棵他以前没有去过的苹果树,依此类推。换句话说,阿姆尔在参观每棵新苹果树时会改变方向。阿姆尔将停止收集苹果,当他面对的方向不再有他没有去过的树时。
他最多可以收集多少个苹果?

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 xi, ai (-105xi≤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.
Examples
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
Note
In the first sample test it doesn't matter if Amr chose at first to go left or right. In both cases he'll get all the apples.
In the second sample test the optimal solution is to go left to x=-1, collect apples from there, then the direction will be reversed, Amr has to go to x=1, collect apples from there, then the direction will be reversed and Amr goes to the final tree x=-2.
In the third sample test the optimal solution is to go right to x=1, collect apples from there, then the direction will be reversed and Amr will not be able to collect anymore apples because there are no apple trees to his left.

输入格式

第一行包含一个数字 n (1≤n≤100),即拉拉兰的苹果树数量。
接下来的 n 行包含两个整数,每个整数 xi,ai (-105xi≤105xi≠01≤ai≤105),表示第 i 棵树的位置和苹果的数量。
可以保证每个坐标最多有一棵苹果树。可以保证没有树生长在第 0 点。

输出格式

输出 Amr 可以收集的最大苹果数。

输入样例 复制

2
-1 5
1 5

输出样例 复制

10

数据范围与提示

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
Note
在第一个样本测试中,Amr最初选择向左还是向右并不重要。在这两种情况下,他都会得到所有的苹果。
在第二个样本测试中,最佳解决方案是向左转到 x=-1,从那里收集苹果,然后方向将反转,Amr 必须转到 x=1,从那里收集苹果,然后方向将反转,Amr转到最终树 x=-2。
在第三个样本测试中,最佳解决方案是向右转到 x=1,从那里收集苹果,然后方向将反转,Amr将无法再收集苹果,因为他的左边没有苹果树。