Problem:


Score : 400 points

Problem Statement

There are N dishes of cuisine placed in front of Takahashi and Aoki.
For convenience, we call these dishes Dish 1, Dish 2, ..., Dish N.

When Takahashi eats Dish i, he earns A_i points of happiness; when Aoki eats Dish i, she earns B_i points of happiness.

Starting from Takahashi, they alternately choose one dish and eat it, until there is no more dish to eat.
Here, both of them choose dishes so that the following value is maximized: "the sum of the happiness he/she will earn in the end" minus "the sum of the happiness the other person will earn in the end".

Find the value: "the sum of the happiness Takahashi earns in the end" minus "the sum of the happiness Aoki earns in the end".

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Output

Print the value: "the sum of the happiness Takahashi earns in the end" minus "the sum of the happiness Aoki earns in the end".

Sample Input 1

3
10 10
20 20
30 30

Sample Output 1

20

In this sample, both of them earns 10 points of happiness by eating Dish 1, 20 points by eating Dish 2, and 30 points by eating Dish 3.

In this case, since Takahashi and Aoki have the same "taste", each time they will choose the dish with which they can earn the greatest happiness. Thus, first Takahashi will choose Dish 3, then Aoki will choose Dish 2, and finally Takahashi will choose Dish 1, so the answer is (30 + 10) - 20 = 20.

Sample Input 2

3
20 10
20 20
20 30

Sample Output 2

20

In this sample, Takahashi earns 20 points of happiness by eating any one of the dishes 1, 2 and 3, but Aoki earns 10 points of happiness by eating Dish 1, 20 points by eating Dish 2, and 30 points by eating Dish 3.

In this case, since only Aoki has likes and dislikes, each time they will choose the dish with which Aoki can earn the greatest happiness. Thus, first Takahashi will choose Dish 3, then Aoki will choose Dish 2, and finally Takahashi will choose Dish 1, so the answer is (20 + 20) - 20 = 20.

Sample Input 3

6
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000

Sample Output 3

-2999999997

Note that the answer may not fit into a 32-bit integer.



Sample input:
3
10 10
20 20
30 30


Sample output:
20

Submission status: Accepted

Solution code:
N=input()

L=[]
for i in range(N):
	a,b=map(int,raw_input().split())
	L.append( (a+b,a,b) ) 


L.sort(key=lambda x: -x[0])


ans=0
for i in range(N):
	if i%2==0:
		ans+=L[i][1]
	else:
		ans-=L[i][2]

print ans

Submission status: Accepted

Solution code:
n=int(input())
l=[list(map(int,input().split())) for i in range(n)]
L=[]
for j in range(n):
  L.append([l[j][0]+l[j][1],j])
L.sort()
L.reverse()
c=0
for k in range(n):
  if k%2==0:
    c+=l[L[k][1]][0]
  else:
    c-=l[L[k][1]][1]
print(c)

Submission status: Accepted

Solution code:
N = int(input())
A = []
AB = []
for i in range(N):
    tenpA, tenpB = map(int, input().split())
    A.append(tenpA)
    AB.append(tenpA + tenpB)
AB.sort(reverse=True)
print(sum(A)-sum(AB[1::2]))

Submission status: Accepted

Solution code:
N = int(input())
lst = []

for _ in range(N):
  l = [int(i) for i in input().split()]
  lst.append([sum(l)] + l)

lst.sort()

result = 0
count = 0
for i in lst[::-1]:
  result += i[count + 1] * (-1) ** count
  count = (count + 1) % 2

print(result)

Submission status: Accepted

Solution code:
N = int(input())
sara = []
t_s = 0
a_s = 0
for i in range(N):
    A, B = map(int, input().split())
    sara.append([A, B])
sara.sort(key=lambda x:x[0]+x[1], reverse=True)
while True:
    if len(sara) == 0:
        break
    t_s += sara.pop(0)[0]
    if len(sara) == 0:
        break
    a_s += sara.pop(0)[1]
print(t_s - a_s)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
x = []
for i in range(N):
    x.extend(input().split())

a = np.array(x, dtype='int').reshape((N, 2))
s = a[:,0:1] + a[:,1:2]
a = np.hstack((a, s))
a = a[np.argsort(a[:, 2])]
n = np.sum(a[(N + 1)%2::2, 0]) - np.sum(a[N % 2::2, 1])
print(n)

Submission status: Accepted

Solution code:
res = 0
n = int(input())
l = []
for i in range(n):
    a, b = map(int,input().split())
    l.append([a + b, a, b])
l = sorted(l)[::-1]
for i in range(n):
    if i % 2 == 0:
        res += l[i][1]
    else:
        res -= l[i][2]
print(res)

Submission status: Accepted

Solution code:
N = int(input())
src = [tuple(map(int,input().split())) for i in range(N)]
src.sort(key=lambda x:sum(x))

a = b = 0
for i,(x,y) in enumerate(reversed(src)):
    if i%2:
        b += y
    else:
        a += x
print(a-b)

Submission status: Accepted

Solution code:
res = 0
n = int(input())
l = []
for i in range(n):
    a, b = map(int,input().split())
    l.append([a + b, a, -b])
l = sorted(l)[::-1]
for i in range(n):
    if i % 2 == 0:
        res += l[i][1]
    else:
        res += l[i][2]
print(res)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
x = []
for i in range(N):
    x.extend(input().split())

a = np.array(x, dtype='int').reshape((N, 2))
s = a[:,0:1] + a[:,1:2]
a = np.hstack((a, s))
a = a[np.argsort(a[:, 2])][::-1, :]
n = np.sum(a[0::2, 0]) - np.sum(a[1::2, 1])
print(n)

Submission status: Accepted

Solution code:
N = int(input())
A, B = [], []
for i in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)

AB = sorted([i for i in range(N)], key=lambda x: A[x] + B[x], reverse=True)
x, y = 0, 0
for (cnt, i) in enumerate(AB):
    if cnt % 2 == 0: 
        x += A[i]
    else:
        y += B[i]
print(x - y)

Submission status: Accepted

Solution code:
N = int(input())

l=[]

for i in range(N):
    a, b = map(int, input().split())
    l.append((a + b, a, b))

l.sort()
l.reverse()
tak = 0
aok = 0


for i in range(N):
    if i % 2 == 0:
        tak += l[i][1]
    else:
        aok += l[i][2]
print(tak-aok)

Submission status: Accepted

Solution code:
N = int(input())

l= []
for i in range(N):

	a,b = map(int, input().split())
	l.append([a,b,a+b])

l.sort(key=lambda x:x[2])
l.reverse()


ans = 0
for i in range(N):
	if i%2 == 0:
		ans+= l[i][0]
	else:
		ans-= l[i][1]

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
ps=[]
tp=[]
ap=[]
for i in range(N):
  p=input().split()
  ps.append([int(p[0])+int(p[1]),i])
  tp.append(int(p[0]))
  ap.append(int(p[1]))
    
def is_even(num):
  if num % 2 == 0:
    return True
  else:
    return False

T=0
A=0
calc=sorted(ps,reverse=True)
#print(calc)
i=0
for p,j in calc:
  if is_even(i):
    T+=tp[j]
  else:
    A+=ap[j]
  i+=1
print(T-A)

Submission status: Accepted

Solution code:
d=sorted([list(map(int,input().split()))for _ in[0]*int(input())],key=sum);print(sum(t[0]for t in d[-1::-2])-sum(t[1]for t in d[-2::-2]))

Submission status: Accepted

Solution code:
N = int(input())
D=[]
b=0
for _ in range(N):
  A,B=map(int, input().split())
  D.append([A,B,A+B])
  b+=B
D.sort(reverse=True, key=lambda x:x[2])

ans=0
while D!=[]:
  ans+=D[0][2]
  D.pop(0)
  if D!=[]:
    D.pop(0)
  
print(ans-b)

Submission status: Accepted

Solution code:
n = int(input())
happiness_hash = {}
for i in range(n):
    a, b = tuple(map(int, input().split(' ')))
 
    happiness_hash[i] = (a, b)
 
sorted_happiness = sorted(happiness_hash.items(), key = lambda x : x[1][0] + x[1][1], reverse = True)
 
total = 0
i = 0
for k, v in sorted_happiness:
    happiness, selected = (0, k)
    if i % 2 == 0:
        happiness = v[0]
        total += happiness
    else:
        happiness = v[1]
        total -= happiness
    i += 1
 
print(total)

Submission status: Accepted

Solution code:
N=int(input())

goukei=[]

for i in range(N):
  A,B=map(int,input().split())
  goukei.append((A+B,A,B))

goukei.sort(key=lambda x:x[0],reverse=True)

Asum=0
Bsum=0
for i in range(N):
  if i%2==0:
    Asum+=goukei[i][1]
  else:
    Bsum+=goukei[i][2]
    
print(Asum-Bsum)

Submission status: Accepted

Solution code:
n=int(input())
d=sorted([list(map(int,input().split()))for _ in[0]*n],key=lambda x:sum(x))[::-1]
t=d[::2]
a=d[1::2]
print(sum(v[0]for v in t)-sum(v[1]for v in a))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    ab = [[int(i) for i in input().split()] for _ in range(N)]
    ab = sorted(ab, key=lambda x: x[0] + x[1], reverse=True)
    ans = 0
    for i in range(N):
        if i % 2 == 0:
            ans += ab[i][0]
        else:
            ans -= ab[i][1]
    print(ans)


main()

Submission status: Accepted

Solution code:
N = int(input())
e = [tuple(map(int, input().split())) for _ in range(N)]
e.sort(key=lambda x: x[0] + x[1], reverse=True)

ans = 0
for i in range(N):
    if i % 2 == 0:
        ans += e[i][0]
    else:
        ans -= e[i][1]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = [[0]*3 for _ in range(n)]
for i in range(n):
    a, b = map(int, input().split())
    l[i] = [a+b, a, b]
l.sort(reverse=True)
ans = 0
for i in range(n):
    if i % 2 == 0:
        ans += l[i][1]
    else:
        ans -= l[i][2]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ans = 0
li = []
for i in range(n):
    a, b = map(int, input().split())
    li.append([a+b, a, b])

li = sorted(li)
li.reverse()
for i in range(n):
    if i%2 == 0:
        ans += li[i][1]
    else:
        ans -= li[i][2]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = [[] for i in range(n)]
A, B = 0, 0
for i in range(n):
    l[i] = list(map(int, input().split()))
    l[i].append(l[i][0]-l[i][1])
l.sort(key = lambda x: (x[0]+x[1]), reverse = True)
#print(l)
for i in range(n):
    if(i%2 == 0):
        A += l[i][0]
    else:
        B += l[i][1]
print(A - B)

Submission status: Accepted

Solution code:
N = int(input())

A = [None] * N
B = [None] * N
C = [None] * N

for n in range(N):
    a, b = map(int, input().split())
    A[n] = a
    B[n] = b
    C[n] = a + b

Z = zip(A, B, C)
Z = sorted(Z, key=lambda x: [x[2]], reverse=True)

A, B, C = map(list, zip(*Z))

T = True
R = 0

for i in range(N):
    if T:
        R += A[i]
        T  = False
    else:
        R -= B[i]
        T  = True

print(R)

Submission status: Accepted

Solution code:
n = int(input())
l = []
ans = 0
for i in range(n):
  a, b = map(int, input().split())
  l.append(a + b)
  ans -= b
l.sort(reverse=True)
for i in range(n):
  if (i % 2 == 0):
    ans += l[i]
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10000000)

N = int(input())
A = {}
B = {}
AplusB =[]

for i in range(N):
	a,b = map(int,input().split())
	A[i] = a
	B[i] = b
	AplusB.append((a+b,i))

AplusB.sort()
AplusB.reverse()


takahashi = 0
aoki = 0

for j in range(N):
	if(j%2 == 0):
		takahashi += A[AplusB[j][1]]
	else:
		aoki += B[AplusB[j][1]]

print(takahashi-aoki)

Submission status: Accepted

Solution code:
from sys import stderr, setrecursionlimit
setrecursionlimit(2147483647)
def getInt():
    return int(input())
def getInts():
    return [int(i) for i in input().split()]
def getIntLines(n=1):
    res = []
    for _ in range(n):
        res.append(getInt())
    return res
def getIntsLines(n=1):
    res = []
    for _ in range(n):
        res.append(getInts())
    return res
def debug(*args, **kwargs):
    print(*args, file=stderr, **kwargs)
    
n = getInt()
dish = []
bsum = 0
for _ in range(n):
    a, b = getInts()
    bsum += b
    dish.append(a+b)
dish.sort(reverse=True)
print(sum(dish[::2]) - bsum)

Submission status: Accepted

Solution code:
N = int(input())
ABa = []
ans = 0
for i in range(N):
    a, b = list(map(int, input().split()))
    ABa.append(a+b)
    ans -= b
ABa.sort(reverse = True)
for i in range(N):
    if i % 2 == 0:
        ans += ABa[i]
print(ans)

Submission status: Accepted

Solution code:
from functools import reduce
from math import ceil

def solve(N, fortune):
  value = reduce(lambda a, b: a - b[1], fortune, 0)
  fortune.sort(key=lambda f: f[0] + f[1], reverse=True)
  for i in range(0, N, 2):
    value = value + fortune[i][0] + fortune[i][1]
  return value

N = int(input())
fortune = [tuple(map(int, input().split())) for _ in range(N)]

print(solve(N, fortune))

Submission status: Accepted

Solution code:
n = int(input())
m = [[0] * 3 for i in range(n)]

for i in range(n):
    a, b = map(int, input().split())
    m[i][0] = a + b
    m[i][1] = a
    m[i][2] = b

m.sort(reverse=True)

takahashi = 0
aoki = 0

for i in range(n):
    if i % 2 == 0:
        takahashi += m[i][1]
    else:
        aoki += m[i][2]

print(takahashi - aoki)

Submission status: Accepted

Solution code:
n = int(input())
AB = [tuple(map(int,input().split())) for _ in range(n)]
all_b = sum([b for _,b in AB])
AB = sorted([a + b for a,b in AB],reverse=True)
ans = 0
for i in range(n):
    if i % 2 == 0:
        ans += AB[i]
print(ans - all_b)

Submission status: Accepted

Solution code:
N = int(input())
L = [[int(v) for v in input().split()] for i in range(N)]
L = sorted(L, key=lambda x: x[0] + x[1], reverse=True)
ans = 0
for i, (a, b) in enumerate(L):
    if i % 2: ans -= b
    else: ans += a
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
dd = []
sb = 0
for _ in range(n):
    a, b = map(int, input().split())
    dd.append(a+b)
    sb -= b

abd = sorted(dd, reverse=True)

res = 0
for i in range(n):
    d = abd.pop(0)
    if i % 2 == 0:
        res += d

print(res+sb)

Submission status: Accepted

Solution code:
N = int(input())
ABab = []
for i in range(N):
    a, b = list(map(int, input().split()))
    ABab.append([a+b, a, -1*b])
ABab.sort(reverse = True)
ans = 0
for i in range(N):
    ans += ABab[i][i%2 + 1]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
lis = []
for i in range(n):
  a,b = map(int,input().split())
  ke = a + b
  lis.append([a,b,ke])
lis.sort(key = lambda x:x[2],reverse = True)
takahashi = 0
aoki = 0
for i in range(n):
  if i % 2 == 0:takahashi += lis[i][0]
  else:aoki += lis[i][1]
print(takahashi -aoki)

Submission status: Accepted

Solution code:
N = int(input())
L = []
ans = 0
for i in range(N) :
    a,b = map(int,input().split())
    L.append([a,b,a+b])
L = sorted(L,key=lambda x:x[2],reverse=True)
takapoint = 0
aokipoint = 0
for i in range(N) :
    if i % 2 == 0 :
        takapoint += L[i][0]
    else :
        aokipoint += L[i][1]
ans = takapoint - aokipoint
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

A = []
b_sum = 0

for i in range(N):
    a,b = map(int,input().split())
    A.append((a+b, a, b))
    b_sum += b

A.sort(reverse=True)

#print(A)
count = 0


for i in range(0,N,2):
    count += A[i][0]

print(count-b_sum)

Submission status: Accepted

Solution code:
from operator import add


def read():
    N = int(input().strip())  # N <= 100,000
    A = []
    B = []
    for i in range(N):
        a, b = input().strip().split()
        A.append(int(a))
        B.append(int(b))
    return N, A, B


def solve(N, A, B):
    f = map(add, A, B)
    food = reversed(sorted(f))
    score = 0
    for i, f in enumerate(food):
        if i % 2 == 0:
            score += f
    return score - sum(B)


if __name__ == '__main__':
    inputs = read()
    print("%d" % solve(*inputs))

Submission status: Accepted

Solution code:
N = int(input())
L = []
sumB = 0
for i in range(N):
 A, B = map(int, input().split())
 sumB -= B
 L.append(A + B)
L.sort(reverse=True)
for i in range(N):
  if i % 2 == 0:
    sumB += L[i]
print(sumB)

Submission status: Accepted

Solution code:
n = int(input())
a,b = [],[]
for _ in range(n):
    x,y = map(int, input().split())
    a.append(x+y)
    b.append(y)

a = sorted(a)[::-1]

print(sum([a for i,a in enumerate(a) if i%2==0])-sum(b))

Submission status: Accepted

Solution code:
n = int(input())
ab = []
x = 0
for i in range(n):
  a,b = map(int,input().split())
  x -= b
  ab.append(a+b)
ab.sort()
ab.reverse()
for i in range(n):
  if i%2==0:
      x+=ab[i]
print(x)

Submission status: Accepted

Solution code:
import math
import copy

def nCast(number):
    if type(number)==str:
        return int(number)
    for idx in range(0,len(number)):
        if type(number[idx])==str:

            number[idx]=int(number[idx])
        else:
            nCast(number[idx])
    return number
    
def inputArr(w):
    l=list()
    for idx in range(0,w):
        l.append(input())
    return l
def inputArr1(w):
    l=list()
    for idx in range(0,w):
        l.append(input().split())
    return l

n=nCast(input())
a=nCast(inputArr1(n))

def akey(a):
    return a[0]+a[1]
list.sort(a,key=akey,reverse=True)


def func(a):
    pa,pb=0,0
    tag=True
    for idx in range(0,len(a)):
        if tag:
            pa+=a[idx][0]
        else:
            pb+=a[idx][1]
        tag=not tag
    return pa-pb
    
print(func(a))

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A, B = [], []
Z = []
 
for i in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
 
    Z.append(-a - b)
 
indexes = np.argsort(Z)
ans = 0
for cnt, i in enumerate(indexes):
    if cnt % 2 == 0:
        ans += A[i]
    else:
        ans -= B[i]
 
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    H = [0] * N
    retval = 0
    for i in range(N):
        a, b = tuple(map(int, input().split()))
        retval -= b
        key = a+b
        H[i] = key
    H.sort(reverse=True)
    retval += sum(H[::2])

    print(retval)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
base = int(input())

data = [[0 for i in range(3)] for j in range(base)]

for num in range(base):
    _num = input()
    _num = _num.split()
    data[num][0] = int(_num[0])+int(_num[1])
    data[num][1] = int(_num[0])
    data[num][2] = int(_num[1])

takahashi = 0
aoki = 0
data.sort()
data.reverse()

_num = 0
for num in range(base):
    if(_num == 0):
        takahashi += data[num][1]
        _num = 1

    elif(_num == 1):
        aoki += data[num][2]
        _num = 0

print(takahashi-aoki)

Submission status: Accepted

Solution code:
import numpy
import collections
N = int(input())
arr = []
for i in range(N):
    a,b = map(int,input().split())
    arr.append([a+b,a,b])

arr = sorted(arr,reverse=True)
ansa,ansb = 0 , 0
for i in range(N):
    if i % 2 == 0:
        ansa += arr[i][1]
    else:
        ansb += arr[i][2]
print(ansa - ansb)

Submission status: Accepted

Solution code:
N = int(input())

AB = []
sum = 0
for i in range(N):
    a, b = [int(i) for i in input().split()]
    sum -= b
    AB.append(a+b)

AB.sort(reverse=True)

count = 0
for _ in range(N//2):
    count += AB.pop(0)
    AB.pop(0)
if N%2 == 1:
    count += AB.pop(0)

print(count + sum)

Submission status: Accepted

Solution code:
N = input()

data = []

for i in range(N):
    A,B = map(int,raw_input().split())
    C = A+B
    data.append([C,A,B])

data.sort()

ans = 0
flag = True

while N > 0:
    if flag:
        ans += data[N-1][1]
        flag = False
    else:
        ans -= data[N-1][2]
        flag = True
    N -= 1

print ans

Submission status: Accepted

Solution code:
n=int(input())
array1=[]
c=[]
for i in range(n):
    a,b=map(int,input().split())
    array1.append([a,b])
array1.sort(reverse=True)
#print(array1)    
for i in range(n):
    c=array1[i][0]+array1[i][1]
    array1[i].insert(0,c) 
array1.sort(key=lambda x: x[0],reverse=True)
sum1=0
sum2=0
for i in range(n):
    if(i%2==0):
        sum1+=array1[i][1]
    else:
        sum2+=array1[i][2]
#print(array1)            
print(sum1-sum2)

Submission status: Accepted

Solution code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os
import re
import sys
import math


n = int(input())

dishes = list()

for i in range(0, n):
    a, b = raw_input().split()
    a = int(a)
    b = int(b)
    sum = a + b
    dishes.append([a, b, sum])

takahashi = 0
aoki = 0
dishes.sort(key=lambda x: x[2], reverse=True)

for i in range(0, n):
    if i % 2 == 0:
        j = dishes[0]
        dishes.pop(0)
        takahashi = takahashi + j[0]
    else:
        j = dishes[0]
        dishes.pop(0)
        aoki = aoki + j[1]

print takahashi - aoki

Submission status: Accepted

Solution code:
n=int(input())
l=[[i]+list(map(int,input().split())) for i in range(n)]

l.sort(key=lambda x:-(x[1]+x[2]))
pa,pb=0,0
used=set()

v=0
while pa<n and pb<n:
  while pa<n and l[pa][0] in used:
    pa+=1
  if pa<n:
    used.add(l[pa][0])
    v+=l[pa][1]
  while pb<n and l[pb][0] in used:
    pb+=1
  if pb<n:
    used.add(l[pb][0])
    v-=l[pb][2]

print(v)

Submission status: Accepted

Solution code:
la=[]
lb=[]
n=int(input())
kari=[]
tak=0
ao=0
s=-1
for i in range(n):
    a,b=map(int,input().split())
    la.append(a)
    lb.append(b)
    kari.append(a+b)
    ao+=b
kari.sort()
if n%2==0:
    for v in range(int(n/2)):
        tak+=kari[s]
        s+=-2
else:
    for k in range(int(n/2)+1):
        tak+=kari[s]
        s+=-2
print(tak-ao)

Submission status: Accepted

Solution code:
import sys

import numpy as np

sys.setrecursionlimit(200000)


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):
    return [il(t) for _ in range(N)]


def solve():
    N = ii()
    AB = iml(N)
    ll = []
    for a, b in AB:
        ll.append(a + b)
    ind = np.argsort(ll)[::-1]
    t = [AB[i][0] for i in ind[::2]]
    a = [AB[i][1] for i in ind[1::2]]
    return sum(t) - sum(a)


if __name__ == "__main__":
    print(solve())

Submission status: Accepted

Solution code:
def cheat_solve(N, ABs):
    Ds = sorted([ab[0] + ab[1] for ab in ABs], reverse=True)
    bsum = sum([ab[1] for ab in ABs])
    dsum = sum(Ds[::2])
    return dsum - bsum

if __name__ == "__main__":
    N = int(input())
    ABs = [tuple(map(int, input().split(" "))) for _ in range(N)]
    print(cheat_solve(N, ABs))

Submission status: Accepted

Solution code:
N = int(input())
L = []
X = 0
for i in range(N):
    a, b = map(int, input().split())
    L += [a+b]
    X += b
L.sort(reverse=True)
print(sum(L[0::2])-X)

Submission status: Accepted

Solution code:
N = int(input())
dish = []
for i in range(N):
    a, b = map(int, input().split())
    dish.append((a+b, (a,b)))

# while True:
#     try:
#         a, b = map(int, input().split())
#     except EOFError:
#         break
#     dish.append((a+b, (a,b)))


l = sorted(dish, reverse=True)

t=a = 0
for i, (_, (a_i, b_i)) in enumerate(l):
    if i % 2 == 0:
        #takahashi
        t += a_i
    else:
        a += b_i

print(t-a)

Submission status: Accepted

Solution code:
N=int(input())

S=[]
bsum=0

for i in range(N):
    a,b = map(int,input().split())
    S.append(a+b)
    bsum+=b

S.sort(reverse=True)

ans=-bsum

for i in range(N):
    if i%2==0:
        ans+=S[i]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ab = [list(map(int, input().split())) for i in range(n)]
div = [[ab[i][1] + ab[i][0], ab[i][0], ab[i][1]] for i in range(n)]
div = sorted(div, key=lambda x: x[0], reverse=True)

counta = 0
countb = 0
for i in range(n):
    if i % 2 == 0:
        counta += div[i][1]
    else:
        countb += div[i][2]

print(counta - countb)

Submission status: Accepted

Solution code:
n = int(input())
dishes = []
for _ in range(n):
    a, b = map(int, input().split())
    dishes.append((a, b, a + b))
dishes.sort(key=lambda dish: dish[2], reverse=True)

x = y = 0
for i in range(n):
    a, b, _ = dishes[i]
    if i % 2 == 0:
        x += a
    else:
        y += b

print(x - y)

Submission status: Accepted

Solution code:
n = int(input())
AB = [list(map(int, input().split())) for _ in range(n)]
AB.sort(reverse = True,key = lambda x:x[0]+x[1])
ans = 0
for i in range(n):
    ans += (1-i%2)*AB[i][0] - (i%2)*AB[i][1]
print(ans)

Submission status: Accepted

Solution code:
N = int( input())
A = [0]*N
B = [0]*N
S = [(0,0)]*N
for i in range(N):
    a, b = map( int, input().split())
    A[i] = a
    B[i] = b
    S[i] = (-a-b,i)
S.sort()
ansa = 0
ansb = 0
for i in range(N):
    _, j = S[i]
    if i%2 == 0:
        ansa += A[j]
    else:
        ansb += B[j]
print( ansa-ansb)

Submission status: Accepted

Solution code:
def inpl(): return list(map(int, input().split()))


N = int(input())
data = [[0, 0] for _ in range(N)]
wa = [[0, 0] for _ in range(N)]
for i in range(N):
    a, b = inpl()
    data[i][0] = a
    data[i][1] = b
    wa[i][0] = a+b
    wa[i][1] = i

wa.sort(reverse=True)

ans = 0
turn = "a"
for a, i in wa:
    if turn=="a":
        turn="b"
        ans += data[i][0]
    else:
        turn="a"
        ans -= data[i][1]
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
AB=[]
takahashi=0
aoki=0
for i in range(N):
    a,b=map(int,input().split())
    AB.append([a,b,a+b])
AB.sort(key=lambda AB:(AB[2],AB[1],AB[0]),reverse=True)
for i in range(N):
    if i%2==0:
        takahashi+=AB[i][0]
    else:
        aoki+=AB[i][1]
print(takahashi-aoki)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())

A = []
B = []
val = []

for i in range(N):
    a, b = list(map(int, input().split()))
    A.append(a)
    B.append(b)
    val.append(a + b)

A = np.array(A)
B = np.array(B)
val = np.array(val)

sort_index = val.argsort()

A = (A[sort_index][::-1])
B = (B[sort_index][::-1])

taka = (A[0::2])
aoki = (B[1::2])

print(sum(taka) - sum(aoki))

Submission status: Accepted

Solution code:
# 2019/01/27
# NIKKEI Programming Contest 2019 - C
from operator import itemgetter

# Input
n = int(input())
ablist = list()

for idx in range(n):
    a, b = map(int, input().split())
    ablist.append([a, b, a+b])

ablist.sort(key=itemgetter(2), reverse=True)

tsum = 0
asum = 0

# Sum
for idx in range(n):
    if idx % 2 == 0:
        # Turn of Takahashi
        tsum += ablist[idx][0]
    else:
        # Turn of Aoki
        asum += ablist[idx][1]

ans1 = tsum - asum

# Output
print(ans1)

Submission status: Accepted

Solution code:
N = int(input())
D = [list(map(int,input().split()))+[k] for k in range(N)]

T = sorted(D, key = lambda x: x[0]+x[1], reverse = True)
A = sorted(D, key = lambda x: x[1]+x[0], reverse = True)
U = [1 for k in range(N)]

ans = 0
l = 0
m = 0
for k in range(N):
    if k%2 == 0:
        while U[T[l][2]] == 0:
            l += 1
        ans += T[l][0]
        U[T[l][2]] = 0
    else:
        while U[A[m][2]] == 0:
            m += 1
        ans -= A[m][1]
        U[A[m][2]] = 0

print(ans)

Submission status: Accepted

Solution code:
N = int(input().strip())
arr = []
for n in range(N):
  A, B = list(map(int, input().split()))
  arr.append([ A + B , A, B])

arr = sorted(arr, reverse=True)
ta = sum([a[1] for a in arr[::2]])
ao = sum([a[2] for a in arr[1::2]])
print(ta - ao)

Submission status: Accepted

Solution code:
n = int(input())
a = []
for i in range(n):
    x,y = map(int,input().split())
    a.append([x,y,x+y])
a.sort(key = lambda x:x[2])
ans = 0
for i in range(n):
    x = a.pop()
    if i %2==0:
        ans+=x[0]
    else:
        ans-=x[1]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = []
B = []
v = []
res = 0

for i in range(N):
    a,b = map(int,input().split())
    A.append(a)
    B.append(b)
    v.append((i,a+b))

a=sorted(v,key = lambda x:x[1],reverse = True)

for i in range(N):
    tmp=a.pop(0)[0]
    if i%2==0:
        res += A[tmp]
    else:
        res -= B[tmp]

print(res)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
ApB = []

for i in range(N):
	temp1, temp2 = map(int, input().split())
	s = temp1 + temp2
	ApB.append([s, temp1, temp2, i])

ApB.sort()
score = 0
for i in range(N):
	if i % 2 == 0:
		score += ApB.pop()[1]
	else:
		score -= ApB.pop()[2]

print(score)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
lst =  np.array([list(map(int,input().split())) for i in range(N)])
A = np.array([lst[i][0] for i in range(N)])
B = np.array([lst[i][1] for i in range(N)])
AB = np.array([A[i] + B[i] for i in range(N)])

loop = np.insert(lst,0,AB,axis=1)
loop = loop[loop[:,0].argsort()[::-1], :]
#loop = sorted(lst,reverse=True)

sumA = [i[1] for i in loop[0::2]]
sumB = [i[2] for i in loop[1::2]]

#print(lst)
#print(A)
#print(B)
#print(loop)
#print(sumA)
#print(sumB)

#print(loop)
print(sum(sumA)-sum(sumB))

Submission status: Accepted

Solution code:
n = int(input())

a = [0  for i in range(n)]
b = [0  for i in range(n)]
x = [[0,0,0]  for i in range(n)]

for i in range(n):
    a[i],b[i]=map(int,input().split())
    x[i][0]=abs(a[i]+b[i])
    x[i][1]=a[i]
    x[i][2]=b[i]

s=sorted(x,reverse=True)
#print(s)

nu=n//2
mod=n%2

count=0
for i in range(nu):
    count+=s[i*2][1]
  #  print(count)
    count-=s[i*2+1][2]
 #   print(count)

if mod==1:
    count+=s[2*nu][1]

    

    

print(count)

Submission status: Accepted

Solution code:
n = int(input())
hoge = []
for i in range(n):
    a, b = list(map(int, input().split()))
    hoge.append([a, b, a+b])
hoge.sort(key=lambda x: x[2], reverse=True)
ans = 0
for ind, l in enumerate(hoge):
    if ind%2 == 0:
        ans += l[0]
    else:
        ans -= l[1]
print(ans)

Submission status: Accepted

Solution code:
from operator import itemgetter

def main(N, A, B, C):
    a, b = 0, 0
    aindex = list(i[-1] for i in sorted(zip(C, A, B, range(N)), key=itemgetter(0, 1, 2), reverse=True))
    bindex = list(i[-1] for i in sorted(zip(C, B, A, range(N)), key=itemgetter(0, 1, 2), reverse=True))
    t = 0
    i = 0
    ai = 0
    bi = 0
    c = set()
    while i < N:
        if t == 0:
            while aindex[ai] in c:
                ai += 1
            a += A[aindex[ai]]
            c.add(aindex[ai])
        else:
            while bindex[bi] in c:
                bi += 1
            b += B[bindex[bi]]
            c.add(bindex[bi])
        i += 1
        t += 1
        t %= 2
    print(a - b)


if __name__ == "__main__":
    N = int(input())
    A, B = [], []
    C = []
    for _ in range(N):
        a, b = (int(i) for i in input().split())
        A.append(a)
        B.append(b)
        C.append(a+b)
    main(N, A, B, C)

Submission status: Accepted

Solution code:
import math
n = int(input())
s = 0
t = 0
x = []
for I in range(n):
 a,b = map(int,input().split())
 x.append(a+b)
 s += b
x.sort(reverse = True)
for I in range(math.ceil(n/2)):
 t += x[2*I]
print(t-s)

Submission status: Accepted

Solution code:
n = int(input())
a = []
b = []
apb = []
for i in range(n):
    ai,bi = map(int, input().split())
    a.append(ai)
    b.append(bi)
    apb.append(ai + bi)

il = sorted(range(n), key=lambda k: apb[k], reverse=True)
at = 0
bt = 0
for i in range(len(il)):
    selected = il[i]
    if i % 2 == 0:
        at += a[selected]
    else:
        bt += b[selected]
print(at - bt)

Submission status: Accepted

Solution code:
n = int(input())

dish_list = []
for i in range(n):
    ai, bi = list(map(int, input().split()))
    dish_list.append([ai, bi, ai+bi])


dish_list.sort(key = lambda x:x[2], reverse=True)


a, b =0, 0
turn = 1
for i in range(n):
    if turn ==1:
        a+=dish_list[i][0]
    else:
        b+=dish_list[i][1]
    turn*=-1

# print(a, b)
# a, b = 0, 0
# def dfs(dish_list, turn):
#     if dish_list==[]:
#         return
#     # print("=================")
#     # print(dish_list)
#     global a
#     global b
#
#
#     #評価値が最大の皿をみつける
#     max_id = 0
#     max_p = 0
#     for i, dish in enumerate(dish_list):
#         if dish[2] > max_p:
#             max_p = dish[2]
#             max_id = i
#
#     #幸福度を加算
#     # print(max_id, max_p)
#     if turn == -1:
#         a+=dish_list[max_id][0]
#     elif turn == 1:
#         b+=dish_list[max_id][1]
#
#     #評価値最大のさらを削除した新しい皿のリストを作る
#     new_list = [list for i, list in enumerate(dish_list) if i!=max_id]
#     dfs(new_list, turn*(-1))
#
# dfs(dish_list, -1)

# print(a, b, a-b)

print(a-b)