CodeForces 558A

Description

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 ( - 105 ≤ xi ≤ 105, xi ≠ 0, 1 ≤ 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.

Sample Input

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


题意:每一个节点都有两部分数据组成,第一部分是该节点在一维坐标轴上的位置,第二部分是它的权值。以原点x=0为界,分成正负两部分.从原点开始,要按“折返跑”的形式
收集权值,直到一边没值可收时结束,问能收集的最大值。

思路:只有第一次向负方向收集和第一次向正方向收集这两种方案。当正负两方向的节点个数相同时,答案明显,就是总和;不相同时,应第一次向节点个数多的方向收集,结果就是答案。

代码如下:
# include<iostream> # include<cstdio> # include<map> # include<set> # include<cstring> using namespace std; struct node { int x,v; bool operator < (const node &a) const { return x<a.x; } }; set<node>s; int ss[105]; int main() { //freopen("A.txt","r",stdin); int n,c1,c2; while(~scanf("%d",&n)) { s.clear(); int i,sum=0; c1=c2=0; node t; for(i=0;i<n;++i){ scanf("%d%d",&t.x,&t.v); s.insert(t); if(t.x<0) ++c1; else ++c2; } set<node>::iterator it; ss[0]=0; int cnt=1; for(it=s.begin();it!=s.end();++it) ss[cnt++]=it->v; for(i=1;i<cnt;++i) ss[i]+=ss[i-1]; if(c1>c2){ printf("%d\n",ss[cnt-1]-ss[cnt-2*c2-2]); }else if(c1==c2) printf("%d\n",ss[cnt-1]); else printf("%d\n",ss[2*c1+1]-ss[0]); } return 0; } 


你可能感兴趣的:(codeforces)