time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard output

Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.

There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.

The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.

No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri.

Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.

Input

The first line contains single integer n (1 ≤ n ≤ 105) — the number of columns.

The next n lines contain the pairs of integers li and ri (1 ≤ li, ri ≤ 500) — the number of soldiers in the i-th column which start to march from the left or the right leg respectively.

Output

Print single integer k — the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.

Consider that columns are numbered from 1 to n in the order they are given in the input data.

If there are several answers, print any of them.

Examples

input

3

5 6

8 9

10 3

output

3

input

2

6 5

5 6

output

1

input

6

5 9

1 3

4 8

4 5

23 54

12 32

output

0

Note

In the first example if you don’t give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg — 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.

If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg — 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.

It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.

【题解】



预处理一下前缀和和后缀和;

枚举哪一个要交换;

O(1)获取交换后的两个和的差;

根据差更新最优解;

#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define lson L,m,rt<<1
#define rson m+1,R,rt<<1|1
#define LL long long using namespace std; const int MAXN = 2e5;
const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
const double pi = acos(-1.0); int n;
int l[MAXN],r[MAXN];
int ps[MAXN][2],as[MAXN][2]; void input_LL(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t) && t!='-') t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} void input_int(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)&&t!='-') t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
input_int(n);
for (int i = 1;i <= n;i++)
input_int(l[i]),input_int(r[i]);
ps[0][0] = ps[0][1] = 0;
as[n+1][0] = as[n+1][1] = 0;
for (int i = 1;i <= n;i++)
ps[i][0] = ps[i-1][0]+l[i],ps[i][1] = ps[i-1][1]+r[i];
for (int i = n;i >=1;i--)
as[i][0] = as[i+1][0]+l[i],as[i][1] = as[i+1][1]+r[i];
int perfect= abs(ps[n][0]-ps[n][1]),ans = 0;
//printf("%d\n",ps[n][0]);
for (int i = 1;i <= n;i++)
{
int temp1 = ps[i-1][0]+as[i+1][0]+r[i];
int temp2 = ps[i-1][1]+as[i+1][1]+l[i];
int temp3 = abs(temp1-temp2);
if (temp3>perfect)
{
ans = i;
perfect = temp3;
}
}
printf("%d\n",ans);
return 0;
}

最新文章

  1. debian 或者kali 安装git
  2. sublime text使用及常见问题
  3. Leetcode 231 Power of Two 数论
  4. Spring 一二事(4) - 单例
  5. MySQL占用内存过大的问题解决
  6. Day02_JAVA语言基础第二天
  7. 【转】android:layout_gravity和android:gravity的区别
  8. 进程控制块PCB学习
  9. appium的安装过程(图文界面)
  10. STL之algorithm、numeric、functional
  11. Android 举例说明自己的定义Camera图片和预览,以及前后摄像头切换
  12. SnackbarUtils:一行代码搞定Snackbar
  13. Warning: session_start() [function.session-start]: Cannot send session cache limiter
  14. socket端口外网无法连接解决方法
  15. 仿百度壁纸客户端(六)——完结篇之Gallery画廊实现壁纸预览已经项目细节优化
  16. 使用docker安装mysql和redis
  17. JS经典面试题汉诺塔
  18. 删除文件夹下面的文件的shell命令
  19. 课外知识----ini
  20. 洛谷.3374.[模板]树状数组1(CDQ分治)

热门文章

  1. Session丢失原因与解决方案
  2. Altium Designer如何设置pcb尺寸
  3. Eclipse手动配置svn
  4. apache与IIS共用80端口冲突解决方法
  5. POJ 2406 Power Strings KMP求周期
  6. percona-toolkit源码编译安装
  7. (十一)RabbitMQ消息队列-如何实现高可用
  8. Hadoop文件的基本操作 分类: A1_HADOOP 2014-12-05 11:36 392人阅读 评论(0) 收藏
  9. [Javascript] Case insensitive sorting for string arrays
  10. js进阶正则表达式10-分组-多行匹配-正则对象的属性(小括号作用:分组,将小括号里面的东西看成一个整体,因为量词只对前一个字符有效)(多行匹配:m)(属性使用:reg.global)