Description

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

Input

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 xi, ai ( - 105 ≤ xi ≤ 105, xi ≠ 0, 1 ≤ ai ≤ 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.

Output

Output the maximum number of apples Amr can collect.

Sample Input

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

题意:每一个节点都有两部分数据组成,第一部分是该节点在一维坐标轴上的位置,第二部分是它的权值。以原点x=0为界,分成正负两部分.从原点开始,要按“折返跑”的形式
收集权值,直到一边没值可收时结束,问能收集的最大值。 思路:只有第一次向负方向收集和第一次向正方向收集这两种方案。当正负两方向的节点个数相同时,答案明显,就是总和;不相同时,应第一次向节点个数多的方向收集,结果就是答案。 代码如下:
# include<iostream>
# include<cstdio>
# include<map>
# include<set>
# include<cstring>
using namespace std;
struct node
{
int x,v;
bool operator < (const node &a) const {
return x<a.x;
}
};
set<node>s;
int ss[105];
int main()
{
//freopen("A.txt","r",stdin);
int n,c1,c2;
while(~scanf("%d",&n))
{
s.clear();
int i,sum=0;
c1=c2=0;
node t;
for(i=0;i<n;++i){
scanf("%d%d",&t.x,&t.v);
s.insert(t);
if(t.x<0)
++c1;
else
++c2;
}
set<node>::iterator it;
ss[0]=0;
int cnt=1;
for(it=s.begin();it!=s.end();++it)
ss[cnt++]=it->v;
for(i=1;i<cnt;++i)
ss[i]+=ss[i-1];
if(c1>c2){
printf("%d\n",ss[cnt-1]-ss[cnt-2*c2-2]);
}else if(c1==c2)
printf("%d\n",ss[cnt-1]);
else
printf("%d\n",ss[2*c1+1]-ss[0]);
}
return 0;
}

CodeForces 558A的更多相关文章

  1. codeforces 558A A. Lala Land and Apple Trees(水题)

    题目链接: A. Lala Land and Apple Trees time limit per test 1 second memory limit per test 256 megabytes ...

  2. 【42.07%】【codeforces 558A】Lala Land and Apple Trees

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  3. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  4. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  5. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  6. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  7. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  8. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  9. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

随机推荐

  1. DB2使用笔记

    1.赋予用户LOAD权限的步骤   使用实例用户db2inst1登录数据库:     使用命令db2 update dbm cfg using sysadm_group dasadm1给管理员用户组d ...

  2. JVM-类文件结构

    无关性的基石 I> "平台无关性"实现在操作系统的应用层上:sun公司以及其他虚拟机提供商发布了许多可以运行在各种不同平台上的虚拟机,这些虚拟机都可以载入和执行同一种平台无关 ...

  3. 解决win7资源监视器不能开启

    刚开始时是这样,点击开始监控,无效 需要开始服务即可解决

  4. PP

  5. [zz]Java中的instanceof关键字

    1.What is the 'instanceof' operator used for? stackoverflow的一个回答:http://stackoverflow.com/questions/ ...

  6. 数据结构 《5》----二叉搜索树 ( Binary Search Tree )

    二叉树的一个重要应用就是查找. 二叉搜索树 满足如下的性质: 左子树的关键字 < 节点的关键字 < 右子树的关键字 1. Find(x) 有了上述的性质后,我们就可以像二分查找那样查找给定 ...

  7. linux常用命令:5网络命令

    网络命令 1. 指令名称:write 指令所在路径:/usr/bin/write 执行权限:所有用户 语法:write  <用户名> 功能描述:给用户发送信息,以Ctrl+D保存结束

  8. Connection to DB

    Connect to MySQL PHP5 and later can work with a MySQL database using MySQLi extension PDO PDO will w ...

  9. Git的环境搭建

    Git时当下流行的分布式版本控制系统. 集中式版本控制系统的版本库是集中存放在中央处理器的,所以开发者要先从中央服务器获取最新的版本,编码后再将自己的代码发送给中央处理器.集中式版本控制系统最大的缺点 ...

  10. 破解 abexcrackme2

    系统 : Windows xp 程序 : abexcrackme2 程序下载地址 :http://pan.baidu.com/s/1qXhyt8C 要求 : 注册机编写 使用工具 : OD 可在“PE ...