Alice and Bonnie are sisters, but they don't like each other very much. So when some old family photos were found in the attic, they started to argue about who should receive which photos. In the end, they decided that they would take turns picking photos. Alice goes first.

There are n stacks of photos. Each stack contains exactly two photos. In each turn, a player may take only a photo from the top of one of the stacks.

Each photo is described by two non-negative integers a and b, indicating that it is worth a units of happiness to Alice and b units of happiness to Bonnie. Values of a and b might differ for different photos.

It's allowed to pass instead of taking a photo. The game ends when all photos are taken or both players pass consecutively.

The players don't act to maximize their own happiness. Instead, each player acts to maximize the amount by which her happiness exceeds her sister's. Assuming both players play optimal, find the difference between Alice's and Bonnie's happiness. That is, if there's a perfectly-played game such that Alice has x happiness and Bonnie has y happiness at the end, you should print x - y.

Input

The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the number of two-photo stacks. Then follow n lines, each describing one of the stacks. A stack is described by four space-separated non-negative integers a1, b1, a2 and b2, each not exceeding 109. a1 and b1 describe the top photo in the stack, while a2 and b2 describe the bottom photo in the stack.

Output

Output a single integer: the difference between Alice's and Bonnie's happiness if both play optimally.

Examples
Input
2
12 3 4 7
1 15 9 1
Output
1
Input
2
5 4 8 8
4 12 14 0
Output
4
Input
1
0 10 0 10
Output
-10

这题是神贪心。。

考虑一对照片(a,b)--(c,d),其中(a,b)在上

那么对于A,如果他先取得a,那么B会取到d,这样是a-d。否则B先取到b,A再取到c,这样是c-b。如果A要先取,应当是a-d>=c-b的,为了不让B获得更多价值,A才会先取。a-d>=c-b移项得到a+b>=c+d

同样对于B,如果他先取得b,那么A会取到c,这样是b-c。否则A先取到a,B再取到d,这样是d-a。同样的,B要先取应当满足b-c>=d-a。移项变成a+b>=c+d,结果竟然跟A先取的条件一样了

这说明对于a+b>=c+d的照片,A、B都会想要先取以拉开差距。

对于a+b<c+d的,A、B先手取都不利于获得差值,显然都希望对方先取。如果a+d>=b+c的都取完了,大家都不先手取了,那么实际上此时a+b<c+d的先手取还是有一点关系的。

如果a>d,说明A先取还是能拉开一点差距的,虽然不如B先取A再取能拉开更大差距,但是B显然也不可能先取的。

同样的,如果b>c,说明B先取也能拉开一点差距。

这些照片可以在a+b>=c+d的取完之后再考虑取。

最后,如果有a+d<b+c&&a<=d&&b<=c的,显然A和B取了都不优,所以扔掉。

再把一张照片(x,y)做变换成((x+y)/2,(x+y)/2),答案预先加上个(x-y)/2。这样取了A,答案贡献是(x-y)/2+(x+y)/2=x,取了B,答案贡献是(x-y)/2-(x+y)/2=-y。这样每张照片对A和B价值都是一样的,可以排序完A和B轮流去取。要注意除2之后可能有0.5,要用double

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
double p[];
int n,cnt;
double ans;
inline void solve(int a,int b)
{
ans+=(double)(a-b)/;
p[++cnt]=(double)(a+b)/;
}
int main()
{
n=read();
for (int i=;i<=n;i++)
{
LL a=read(),b=read(),c=read(),d=read();
if (a+b>=c+d)
{
solve(a,b);
solve(c,d);
}else
{
if (a>=d)solve(a-d,d-a);
if (b>=c)solve(c-b,b-c);
}
}
sort(p+,p+cnt+,greater<double>());
for (int i=;i<=cnt;i++)
{
if (i&)ans+=p[i];
else ans-=p[i];
}
printf("%.0f\n",ans);
}

cf 725F

最新文章

  1. Snapshot Instance 操作详解 - 每天5分钟玩转 OpenStack(36)
  2. CodeForces 518B. Tanya and Postcard
  3. jquery.validate.js在IE8下报错不运行
  4. hdu 2074
  5. python基础整理笔记(九)
  6. js保留位和取整
  7. qq临时会话设置
  8. 一步步学敏捷开发:5. Scrum的4种会议
  9. BOM、DOM学习笔记——JavaScript
  10. 03:计算(a+b)/c的值
  11. EAT/IAT Hook
  12. 【转】iOS 解决ipv6问题
  13. Scala学习笔记--List、ListBuffer
  14. LINQ中的Aggregate用法总结
  15. makinacorpus/spynner
  16. JavaScript继承基础讲解,原型链、借用构造函数、混合模式、原型式继承、寄生式继承、寄生组合式继承
  17. git 克隆到本地linux目录的2种方式
  18. 2429: [HAOI2006]聪明的猴子
  19. 解决克隆 centos虚拟机后修改克隆后的机器的ip、mac、uuid失败的问题
  20. Saving Princess claire_(hdu 4308 bfs模板题)

热门文章

  1. SQL简单查询后续记录
  2. hihoCoder #1079 : 离散化 (线段树,数据离散化)
  3. JavaScript的语音识别
  4. UVA1665 Islands (并查集)
  5. codeforces Gym 100338E Numbers (贪心,实现)
  6. ios 使用NSRegularExpression解析正则表达式
  7. QT 图形视图框架
  8. Open Cascade:AIS_InteractiveContext如何调用函数选择AIS对象
  9. Mac如何让调整窗口大小更简单
  10. ios设备屏幕尺寸与分辨率