Codeforces Round #312 (Div. 2) A Lala Land and Apple Trees

Source: Internet
Author: User

Topic Link: A. Lala Land and Apple Trees


Surface:

A. Lala Land and Apple treestime limit per test1 secondmemory limit per test256 megabytesinputstandard 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 has exactly n apple trees. Tree number I is located in a position x i and has a i 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 II integers eachxi,ai ( ?-? ten5?≤? Xi? ≤?105,xi? ≠?0 ,1?≤? a i. ≤?105), representing the position of theI-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 point0.

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 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 would be reversed, Amr have to go tox? =?1, collect Apples from there, then the direction would be is reversed and AMR goes to the final treex? -?2.

In the third sample test the optimal solution are to go right to x=?1, collect apples from there, then T He direction would be reversed and AMR would not be able to collect anymore apples because there is no apple trees to his L Eft.

Problem solving: If the number of positive and negative positions is the same, then it is advisable, if not the same, then the less side of the full take, many side take away from 0 near (less quantity + 1).


Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cstdlib> #include <string> #include <map> #include <vector> #include <set> #include <queue >using namespace std;struct apple_tree{int amount,pos;} Store[100010];bool CMP (apple_tree a,apple_tree b) {return a.pos<b.pos;}    int main () {int n,cntminus=0,cntzheng=0;    Long Long ans=0;    scanf ("%d", &n);      for (int i=0;i<n;i++) {scanf ("%d%d", &store[i].pos,&store[i].amount);      if (store[i].pos<0) cntminus++;    else cntzheng++;        } if (Cntminus==cntzheng) {for (int i=0;i<n;i++) Ans+=store[i].amount;    cout<<ans<<endl;        } else {sort (store,store+n,cmp); if (Cntminus<cntzheng) {for (int i=0;i<2*cntminus+1;i++) {Ans+=store[i].amount          ; }} else {for (int j=n-1;j>= (N-2*cntzheNG-1); j--) Ans+=store[j].amount;    } cout<<ans<<endl; } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces Round #312 (Div. 2) A Lala Land and Apple Trees

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.