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 had exactlyNApple trees. Tree number I was located in a position xi and have ai apples growing o n 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 II integers each xi, ai ( -5≤ xi ≤105, xi ≠0, 1≤ Ai ≤105), representing the position of the i-th tree and number of apples on I T.
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 Test (s) 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 on first to go left or right. In both cases he ' ll get all the apples.
In the second sample test the optimal solution are to go left to x =-1, collect apples from there, then the Direction'll be reversed, Amr have to go into x = 1, collect apples from there and then the direction would be re Versed and AMR goes to the final tree x =-2.
In the third sample test the optimal solution are to go right to x = 1, collect apples from there, then the DI Rection'll be reversed and AMR would not being able to collect anymore apples because there is no apple trees to he left.
The puzzle: The two sides are greedy, pay attention to the same situation.
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <algorithm>5#include <queue>6#include <cstring>7 #definePAU Putchar (")8 #defineENT Putchar (' \ n ')9 using namespacestd;TenInlineintRead () { One intx=0, sig=1;CharCh=GetChar (); A while(!isdigit (CH)) {if(ch=='-') sig=-1; ch=GetChar ();} - while(IsDigit (CH)) x=Ten*x+ch-'0', ch=GetChar (); - returnx*=Sig; the } -InlinevoidWriteintx) { - if(x==0) {Putchar ('0');return;}if(x<0) Putchar ('-'), x=-x; - intlen=0, buf[ the]; while(x) buf[len++]=x%Ten, x/=Ten; + for(inti=len-1; i>=0; i--) Putchar (buf[i]+'0');return; - } + voidinit () { A intN=read (), neg=0; atpair<int,int>A[n]; - for(intI=0; i<n;i++){ -A[i].first=read (); a[i].second=read (); - if(a[i].first<0) neg++; -}sort (a,a+n); - intSol=0; in if((neg<<1) <n) for(intI=0; I<min (neg<<1)+1, n); i++) sol+=A[i].second; - Else for(intI=max ((neg<<1)-N-1,0); i<n;i++) sol+=A[i].second; to write (sol); + return; - } the voidWork () { * return; $ }Panax Notoginseng voidprint () { - return; the } + intMain () {init (); work ();p rint ();return 0;}
Codeforces Round #312 (Div. 2) A.lala Land and Apple Trees