A. Lala land and Apple Trees
Time Limit:20 Sec
Memory limit:256 MB
Topic Connection
Http://codeforces.com/contest/558/problem/A
Description
Amr lives in Lala land. Lala land was a very beautiful country that's located on a coordinate line. Lala land was famous with its apple trees growing everywhere.
Lala Land has exactly n apple trees. The Tree number I was located in a position Xi and had 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 the all of it apples and then reverse him direction, continue walking in this direction until he meets another app Le tree he didn ' t visit before and so on. In the other words, AMR reverses he direction when visiting each new Apple tree. AMR would stop collecting apples when there was no more trees he didn ' t visit in the direction he was facing.
What is the maximum number of apples he can collect?
Input
The first line contains one number n (1≤n≤100) and the number of apple trees in Lala land.
The following n lines contains and integers each XI, AI (-105≤xi≤105, xi≠0, 1≤ai≤105), representing the Positi On the i-th tree and number of apples on it.
It's guaranteed that there are at the most one apple tree in each coordinate. It ' s guaranteed that no tree grows in point 0.
Output
Output of the maximum number of apples AMR can collect.
Sample Input
2
-1 5
1 5
Sample Output
10
HINT
Test instructions
There is a person who, at first, will walk to the left or to the right, and the apple tree will pick up all the fruits of the apple tree and then exchange directions and repeat the process.
Ask you how many fruits a man can take.
Exercises
First left and right according to the coordinates of a sequence, it is clear that there are only two ways, each to judge who the biggest is good
Code
#include <stdio.h>#include<iostream>#include<math.h>#include<algorithm>using namespacestd;structnode{intx, y;}; Node a[101];node b[101];BOOLCMP (node Aa,node bb) {returnaa.x<bb.x;}intMain () {intN; scanf ("%d",&N); intnum1=0, num2=0; for(intI=0; i<n;i++) { intc,d; CIN>>c>>D; if(c<0) a[num1].x=-c,a[num1++].y=D; Elseb[num2].x=c,b[num2++].y=D; } sort (A,a+num1,cmp); Sort (b,b+num2,cmp); intnum=min (num1,num2); intans=0; for(intI=0; i<num;i++) ans+ = (a[i].y+b[i].y); if(num1>num2) ans+=a[num2].y; Else if(num2>num1) ans+=b[num1].y; cout<<ans<<Endl;}
Codeforces Round #312 (Div. 2) A. Lala Land and Apple Trees violence