A. Lala land and Apple TreesTime limit per test1 Secondmemory limit per testMegabytesinputStandard InputOutputStandard Output
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 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 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), the number of apple trees in Lala land.
The following N Lines contains, integers each xi , ai (?-? ten5?≤? Xi? ≤?105 , xi? ≠?0 , 1?≤? a i? ≤?105 ), representing the position of 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 Test (s)input
2-1 51 5
Output
10
input
3-2 21 4-1 3
Output
9
input
31 93 57 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 would be reversed, Amr have to go to x ? =?1 , collect apples from there, then the direction would be reversed 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 ther E, then the direction would be reversed and AMR would not be able to collect anymore apples because there is no apple trees To he left.
#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include <string > #include <algorithm> #include <queue> #include <stack>using namespace std;const double PI = ACOs (- 1.0); const double e = 2.718281828459;const double EPS = 1e-8;struct node{int index; int num;} A[110];bool CMP (node x, node Y) {return x.index < Y.index;} int main () {//freopen ("In.txt", "R", stdin); Freopen ("OUT.txt", "w", stdout); int n; while (cin>>n) {int left = 0; int right = 0; int left_sum = 0; int right_sum = 0; for (int i = 1; I <= n; i++) {scanf ("%d%d", &a[i].index, &a[i].num); if (A[i].index < 0) {left++; Left_sum + = A[i].num; } else {right++; Right_sum + = A[i].num; }} sort (a+1, a+1+n, CMP); Int PL, PR; int sum1, sum2, sum; if (left!=0 && right==0) {printf ("%d\n", a[n].num); } else if (left==0 && right!=0) {printf ("%d\n", a[1].num); } else if (ABS (left-right) <= 1) {printf ("%d\n", left_sum+right_sum); } else if (left > right) {pl = left-right; SUM1 = Left_sum+right_sum; for (int i = 1; I <= pl; i++) {sum1-= A[i].num; } sum2 = Left_sum+right_sum; for (int i = 1; I <= pl-1; i++) {sum2-= A[i].num; } printf ("%d\n", Max (SUM1, sum2)); } else if (left < right) {PR = right-left; SUM1 = Left_sum+right_sum; int i = n; while (pr--) {sum1-= A[i--].num; } i = n; PR = right-left-1; sum2 = Left_sum+right_sum; while (pr--) {sum2-= A[i--].num; } printf ("%d\n", Max (SUM1, sum2)); }} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Codeforces 558A Lala Land and Apple Trees (sort quick)