codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

558A.md (2506B)


      1 Source: https://codeforces.com/problemset/problem/558/A
      2 
      3 A. Lala Land and Apple Trees
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 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.
     22 
     23 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 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.
     24 
     25 What is the maximum number of apples he can collect?
     26 
     27 Input
     28 
     29 The first line contains one number *n* (1 ≤ *n* ≤ 100), the number of apple trees in Lala Land.
     30 
     31 The following *n* lines contains two integers each *x**i*, *a**i* ( - 105 ≤ *x**i* ≤ 105, *x**i* ≠ 0, 1 ≤ *a**i* ≤ 105), representing the position of the *i*-th tree and number of apples on it.
     32 
     33 It's guaranteed that there is at most one apple tree at each coordinate. It's guaranteed that no tree grows in point 0.
     34 
     35 Output
     36 
     37 Output the maximum number of apples Amr can collect.
     38 
     39 Examples
     40 
     41 Input
     42 
     43 ```
     44 2  
     45 -1 5  
     46 1 5
     47 ```
     48 
     49 Output
     50 
     51 ```
     52 10
     53 ```
     54 
     55 Input
     56 
     57 ```
     58 3  
     59 -2 2  
     60 1 4  
     61 -1 3
     62 ```
     63 
     64 Output
     65 
     66 ```
     67 9
     68 ```
     69 
     70 Input
     71 
     72 ```
     73 3  
     74 1 9  
     75 3 5  
     76 7 10
     77 ```
     78 
     79 Output
     80 
     81 ```
     82 9
     83 ```
     84 
     85 Note
     86 
     87 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.
     88 
     89 In the second sample test the optimal solution is to go left to *x* =  - 1, collect apples from there, then the direction will be reversed, Amr has to go to *x* = 1, collect apples from there, then the direction will be reversed and Amr goes to the final tree *x* =  - 2.
     90 
     91 In the third sample test the optimal solution is to go right to *x* = 1, collect apples from there, then the direction will be reversed and Amr will not be able to collect anymore apples because there are no apple trees to his left.