CF558A.Lala Land and Apple Trees

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

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.

What is the maximum number of apples he can collect?

阿米尔住在拉拉国。拉拉国是一个非常美丽的国家,位于一条数轴上。拉拉国以遍地生长的苹果树而闻名。

拉拉国恰好有 nn 棵苹果树。第 ii 棵树位于位置 xix_i,上面结有 aia_i 个苹果。阿米尔想从这些苹果树上采摘苹果。他当前站在位置 x=0x = 0。初始时,他可以选择向右或向左走。之后,他将一直沿所选方向前进,直到遇到一棵他尚未访问过的苹果树;他将摘走该树上的全部苹果,然后立即掉转方向,继续沿新方向行走,直至再次遇到一棵尚未访问过的苹果树,依此类推。换言之,阿米尔每访问一棵新的苹果树,就改变一次行走方向。当他在当前面对的方向上已无未访问过的苹果树时,他便停止采摘苹果。

他最多能采摘多少个苹果?

输入格式

The first line contains one number n (1 ≤ n ≤ 100), the number of apple trees in Lala Land.

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.

It's guaranteed that there is at most one apple tree at each coordinate. It's guaranteed that no tree grows in point 0.

第一行包含一个整数 nn(1≤n≤1001 \leq n \leq 100),表示拉拉国中苹果树的数量。

接下来的 nn 行,每行包含两个整数 xix_i、aia_i(−105≤xi≤105-10^5 \leq x_i \leq 10^5,xi≠0x_i \neq 0,1≤ai≤1051 \leq a_i \leq 10^5),分别表示第 ii 棵树的位置及其上的苹果数量。

保证每个坐标上至多有一棵苹果树。保证没有任何一棵树生长在坐标 00 处。

输出格式

Output the maximum number of apples Amr can collect.

输出 Amr 能够收集到的苹果的最大数量。

输入输出样例

  • 输入#1

    2
    -1 5
    1 5

    输出#1

    10
  • 输入#2

    3
    -2 2
    1 4
    -1 3

    输出#2

    9
  • 输入#3

    3
    1 9
    3 5
    7 10

    输出#3

    9

说明/提示

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 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.

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.

在第一个样例测试中,Amr 首先选择向左还是向右走都无关紧要;两种情况下他都能收集到全部苹果。

在第二个样例测试中,最优策略是:先向左走到 x=−1x = -1 处收集该处的苹果,然后方向反转,Amr 必须向右走到 x=1x = 1 处收集该处的苹果,接着方向再次反转,Amr 继续向左走到最后一棵苹果树 x=−2x = -2 处。

在第三个样例测试中,最优策略是:先向右走到 x=1x = 1 处收集该处的苹果,然后方向反转,但此时 Amr 左侧已无苹果树,因此无法再收集更多苹果。

输入解题思路,AI测评打分。不知道怎么写?

首页