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()
val= []
for _ in xrange(N):
    a, b  = map(int, raw_input().split())
    val.append((a + b, a, b))
val.sort(reverse=True)

res = 0
for i, (v, a, b) in enumerate(val):
    res += a if i % 2 == 0 else -b
print res

Submission status: Accepted

Solution code:
data = input().split()

n = int(data[0])
a=[]
for i in range(n):
    line = input().split()
    tmp=[]
    tmp.append(int(line[1]) + int(line[0]))
    tmp.append(int(line[0]))
    tmp.append(int(line[1]))
    a.append(tmp)

res=0
a.sort(key=lambda x: x[0])
a.reverse()

flg=True

for e in a:
    if(flg):
        res +=e[1]
    else:
        res -=e[2]
    flg=not(flg)
    

print(res)

Submission status: Accepted

Solution code:
N = int(input())

A_list = []
B_list = []
dif_list = []
for i in range(N):
    A, B = map(int, input().split())
    A_list.append(A)
    B_list.append(B)
    dif_list.append(A + B)

my_list = [[dif, a, b] for dif, a, b in zip(dif_list, A_list, B_list)]
my_list.sort(reverse=True)

takahashi_score = 0
aoki_score = 0

for i, list_items in enumerate(my_list):
    a = list_items[1]
    b = list_items[2]
    if i % 2 == 0:
        takahashi_score += a
    else:
        aoki_score += b

print(takahashi_score - aoki_score)

Submission status: Accepted

Solution code:
n = int(input())
a = []
b = []
for i in range(n):
    ta, tb = map(int, input().split())
    a.append(ta)
    b.append(tb)
    
happya = 0
happyb = 0

def wa(a,b):
    return a+b

c = list(map(wa, a, b))
for i in range(n):
    c[i] = [c[i], i]
c.sort()
c.reverse()

d = []
for i in range(n):
    d.append(c[i][1])

for i in range(n):
    if(i % 2 == 0):
        happya += a[d[i]]
    else:
        happyb += b[d[i]]
        
print(happya-happyb)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
ab=[]
for _ in range(n):
    t,a=map(int,input().split())
    ab.append([t,a,t+a])
sorted_items = sorted(
  ab,
  key = lambda x: x[2],
  reverse=True
)
t=0
a=0
for i in range(n):
    if i%2==0:
        t+=sorted_items[i][0]
    else:
        a+=sorted_items[i][1]
print(t-a)

Submission status: Accepted

Solution code:
# Different Strokes
n = int(input())
a, b = list(), list()
ab = list()
for i in range(n):
    a_, b_ = map(int, input().split())
    a.append(a_)
    b.append(b_)
    ab.append((a_, b_))
ans = 0
ab.sort(key=(lambda x: sum(x)), reverse=True)
for i, ab_ in enumerate(ab):
    if i % 2 == 0:
        ans += ab_[0]
    else:
        ans -= ab_[1]
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
x = []
y = []
for i in range(N):
    a, b = [int(_) for _ in input().split()]
    x.append(a)
    y.append(b)

nx = np.array(x)
ny = np.array(y)
ns = nx + ny
arr = np.array([nx, ny, ns]).T
arr = arr[np.argsort(arr[:, 2])][::-1, :]
n = np.sum(arr[0::2, 0]) - np.sum(arr[1::2, 1])
print(n)

Submission status: Accepted

Solution code:
import numpy as np


def main():
    N = int(input())
    A = []
    B = []
    for i in range(0, N):
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)
    
    takahashi = 0
    aoki = 0
    # 足し算して一番大きいところからとる
    A = np.array(A)
    B = np.array(B)
    sum_AB = A + B
    l = sum_AB.argsort()[::-1]
    takahashi = sum([A[i] for i in [j for idx, j in enumerate(l) if idx % 2 == 0]])
    aoki = sum([B[i] for i in [j for idx, j in enumerate(l) if idx % 2 != 0]])

    print(int(takahashi - aoki))


main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
import sys
from operator import itemgetter
input = sys.stdin.readline
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
l = []
for i in range(N):
    a,b = IL()
    l.append([a,b,a+b])

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

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

print(chokudai-aoki)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from functools import lru_cache, reduce
from math import ceil, floor

import heapq
import itertools
import operator


def get_int():
    return int(input())


def get_str():
    return input().strip()


def get_int_list():
    return [int(i) for i in input().split()]


def get_char_list():
    return list(input().strip())


def main():
    n = get_int()
    A = []
    B = []
    for _ in range(n):
        a, b = get_int_list()
        A.append(a)
        B.append(b)

    sums = [(i, a + b) for i, (a, b) in enumerate(zip(A, B))]
    sums.sort(key=lambda x: x[1], reverse=True)
    takahashi = [A[i] for i, _ in sums[0::2]]
    aoki = [B[i] for i, _ in sums[1::2]]
    print(sum(takahashi) - sum(aoki))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())

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

#print(all_list)  
all_list.sort()
all_list.reverse()
#print(all_list)

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

Submission status: Accepted

Solution code:
n = int(input())
c = []
d = []
e = []

for i in range(0, n):
    a, b = map(int, input().split())
    c.append(a)
    d.append(b)
    e.append(a + b)

li1, li2 = sorted(e), sorted(range(len(e)), key=lambda k: e[k])

turn = 0
happy = 0

for i in range(n - 1, -1, -1):
    now = li1[i]
    if (turn == 0):
        max = 0
        maxKey = 0
        for j in range(i, -1, -1):
            if (now == li1[j] and max < c[li2[j]]):
                max = c[li2[j]]
                maxKey = j
            else:
                li2[i], li2[maxKey] = li2[maxKey], li2[i]
                break
        happy += c[li2[i]]
    else:

        max = 0
        maxKey = 0
        for j in range(i, -1, -1):
            if (now == li1[j] and max < d[li2[j]]):
                max = d[li2[j]]
                maxKey = j
            else:
                li2[i], li2[maxKey] = li2[maxKey], li2[i]
                break

        happy -= d[li2[i]]
    turn = (turn + 1) % 2
print(happy)

Submission status: Accepted

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

a = sorted(a, key=lambda x: -x[0])
taka = 0
aoki = 0
for i in range(n):
  if i % 2 == 0:
    taka += a[i][1]
  else:
    aoki += a[i][2]

print(taka - aoki)

Submission status: Accepted

Solution code:
import heapq

n = int(input())
dish = []
eaten = [False for _ in range(n)] 
for i in range(n):
	a, b = map(int, input().split())
	dish.append([a+b, a, b])

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

print(ans)

Submission status: Accepted

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

sum_list = []
for i in range(N):
    _, a, b = ab[i]
    sum_list.append((a + b, i))
sum_list = sorted(sum_list, key=lambda x:-x[0])

ans = 0

for k in range(N):
    if k % 2 == 0:
        su = sum_list[k]
        ans += ab[su[1]][1]
    else:
        su = sum_list[k]
        ans -= ab[su[1]][2]
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import numpy as np
n = int(input())

a = np.array([list(map(int,input().split())) for _ in range(n)])
t = np.argsort(a[:,0]+a[:,1])[::-1]
ans = 0

for i,j in zip(t,range(n)):
    if j%2 == 0:
        ans+=a[i][0]
    else:
        ans-=a[i][1]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
b = sorted([[a[i][0] + a[i][1], a[i][0]] for i in range(n)])
taka = 0
aoki = 0

for i in range(n):
  if i % 2 == 0:
    taka += b[n - i - 1][1]
  else:
    aoki += b[n - i - 1][0] - b[n - i - 1][1]

print(taka - aoki)

Submission status: Accepted

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

for i in range(0,N):
    t,a=map(int,input().split(" "))
    d=t+a
    flist.append([d,t,a])
#print(flist)

count=0
score=0
number=0
flist.sort(reverse=True)
#print(flist)

for count in range(0,N):
    if count%2==0:
        score+=flist[count][1]
    else:
        score-=flist[count][2]

    #print(score)
print(score)

Submission status: Accepted

Solution code:
n=int(input())
a=[0]*n
b=[0]*n
c=[]
for i in range(n):
	a[i],b[i]=map(int,input().split())
	c.append((a[i]+b[i],i))
d=sorted(c,reverse=True)
ta=0
ao=0
turn=0
for i in range(n):
	if turn==0:
		ta+=a[d[i][1]]
		turn=1
	else:
		ao+=b[d[i][1]]
		turn=0
print(ta-ao)

Submission status: Accepted

Solution code:
n=int(input())
s=[list(map(int,input().split(" "))) for k in range(n)]
aa=sorted(s,key=lambda x:x[0]+x[1],reverse=True)
c=0
for l in range(n):
  if l%2==0:
    c+=aa[l][0]
  else:
    c-=aa[l][1]
print(c)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N = int(input())
lst =[]
#lst = [list(map(int,input().split())) for i in range(N)]
for i in range(N):
    a,b = map(int,input().split())
    lst.append([a,b,a+b])

lst.sort(key=lambda x:(x[2],x[0],x[1]), reverse=True)
#print(lst)

count = 1
Takahashi = 0
Aoki = 0
for i in lst:
    if count % 2 == 1:
        Takahashi += i[0]
    else:
        Aoki += i[1]
    count+=1
print(Takahashi-Aoki)

Submission status: Accepted

Solution code:
n = int(input())
ab = [[int(_) for _ in input().split()] for __ in range(n)]
s = [[sum(e), i] for i, e in enumerate(ab)]
s.sort(reverse=True)
t = 0

for j, x in enumerate(s):
    t += ab[x[1]][0] if j % 2 == 0 else -ab[x[1]][1]
print(t)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

def solve():
    N = int(input())
    A, B = np.vstack([np.array(list(map(int, input().split()))) for _ in range(N)]).T
    C = A+B
    C.sort()
    res = (C[-1::-2]).sum() - B.sum()
    return str(res)
if __name__ == '__main__':
    print(solve())

Submission status: Accepted

Solution code:
n = int(input())

points = []
for i in range(n):
	temp =  list(map(int, input().split()))
	points.append(temp)

points.sort(key = lambda x: x[0]+x[1], reverse=True)

print(
	sum([x[0] for x in points[0::2]] )
	-
	sum([x[1] for x in points[1::2]] )
	)

Submission status: Accepted

Solution code:
from sys import stdin
input = stdin.readline

n = int(input())
nums = []
for _ in range(n):
    a, b = map(int, input().split())
    nums.append([b+a, a, b])
nums.sort(reverse = True)

if n % 2 == 1:
    nums.append([0, 0, 0])
    
A = 0
B = 0
for i in range(0, n, 2):
    A += nums[i][1]
    B += nums[i+1][2]
    
print(A - B)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
Alist = []
Blist = []
sumlist = []

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

index = np.argsort(sumlist)

sumlist = np.sort(sumlist)




counter = 0
takahasi = 0
aoki = 0

for i in range(N)[::-1]:
    if counter == 0:
        takahasi += Alist[index[i]]
        counter = 1
        continue
    if counter == 1:
        aoki += Blist[index[i]]
        counter = 0
        continue

print(takahasi-aoki)

Submission status: Accepted

Solution code:
import math
from operator import itemgetter

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

C = [(A[i], B[i], A[i] + B[i]) for i in range(N)]
C = sorted(C, key=itemgetter(2), reverse=True)

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

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import random
import re
import string
import sys

def solve(N, ABs):
    Cs = []
    for n in range(N):
        a, b = ABs[n]
        Cs.append((a + b, a, b))
    Cs.sort()
    Cs.reverse()
    
    taka = 0
    aoki = 0
    for i, c in enumerate(Cs):
        if i % 2 == 0:
            taka += Cs[i][1]
        else:
            aoki += Cs[i][2]
    return taka - aoki

def main():
    N = int(input())
    ABs = []
    for n in range(N):
        a, b = map(int, input().split())
        ABs.append((a, b))
    print(solve(N, ABs))
main()

# def testcase(N, ABs, expected):
#     mine = solve(N, ABs)
#     if mine != expected:
#         print("error!")
#         print("  N = ", N)
#         print("  ABs = ", ABs)
#         print("  expected = ", expected)
#         print("  mine = ", mine)

# testcase(3, [(10,10),(20,20),(30,30)], 20)
# testcase(3, [(20,10),(20,20),(20,30)], 20)
# testcase(6, [(1,1000000000)]*6, -2999999997)
# testcase(3, [(10,1000),(10,10),(30,30)], 20 - 30)
# testcase(4, [(10,1000),(10,10),(30,30)], 20 - 30)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
AB = np.zeros((3, N))

for i in range(N):
    tmpA, tmpB = map(int, input().split())
    AB[0, i] = tmpA + tmpB
    AB[1, i] = tmpA
    AB[2, i] = tmpB

AB = AB[:, np.argsort(AB[0, :])][:, ::-1]

point = 0
for i in range(N):
    if i%2==0:
        point += AB[1, i]
    else:
        point -= AB[2, i]
 
print(int(point))

Submission status: Accepted

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [long(input_raw[0])]
    A_list=[]
    B_list=[]
    All_list = []
    sum_list = []
    abs_list = []

    eated_set = set([])
    for i in range(N):
        input_raw = read_func().strip().split()
        [A, B] = [long(input_raw[0]), long(input_raw[1])]
        A_list.append(A)
        B_list.append(B)
        All_list.append((A, i))
        All_list.append((B, i))
        sum_list.append((A + B, i))
        abs_list.append((abs(A - B), i))

    All_list.sort(reverse = True)

##    is_t_turn = True
##    t_sum = 0L
##    a_sum = 0L
##    for i in range(len(All_list)):
##        #if i in t_eated_set or i in a_eated_set:
##        if All_list[i][1] in eated_set:
##            continue
##        if is_t_turn == True:
##            t_sum += A_list[All_list[i][1]]
##
##        else:
##            a_sum += B_list[All_list[i][1]]
##
##        eated_set.add(All_list[i][1])
##        if is_t_turn == True:
##            is_t_turn = False
##        else:
##            is_t_turn = True
##    sum1 =  t_sum - a_sum

    is_t_turn = True
    t_sum = 0L
    a_sum = 0L
    eated_set = set([])
    sum_list.sort(reverse=True)
    for i in range(len(sum_list)):

        if is_t_turn == True:
            t_sum += A_list[sum_list[i][1]]

        else:
            a_sum += B_list[sum_list[i][1]]

        if is_t_turn == True:
            is_t_turn = False
        else:
            is_t_turn = True
    sum2 =  t_sum - a_sum

##    is_t_turn = True
##    t_sum = 0L
##    a_sum = 0L
##    abs_list.sort(reverse=True)
##    for i in range(len(abs_list)):
##
##        if is_t_turn == True:
##            t_sum += A_list[abs_list[i][1]]
##
##        else:
##            a_sum += B_list[abs_list[i][1]]
##
##        if is_t_turn == True:
##            is_t_turn = False
##        else:
##            is_t_turn = True
##    sum3 =  t_sum - a_sum

    print sum2


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def C2():
    N = int(input())
    AB = []
    for i in range(N):
        a, b = map(int, input().split())
        AB.append([a + b, a, b])
    AB.sort()
    AB = AB[::-1]
    i = 0
    ans = 0
    while i < N:
        ans += AB[i][1]
        i += 1
        if i >= N:
            break
        ans -= AB[i][2]
        i += 1
    print(ans)
C2()

Submission status: Accepted

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

li2 = sorted(li, key=lambda x: (x[0] + x[1]), reverse=True)
v = 0
for i, (a, b) in enumerate(li2):
    if i % 2 == 0:
        v += a
    else:
        v -= b

print(v)

Submission status: Accepted

Solution code:
import numpy as np
N=int(input())
AB=np.empty((N,2))
sum=np.empty(N)

for i in range(N):
    AB[i]=list(map(int,input().split()))
    sum[i]=AB[i][0]+AB[i][1]
lis = np.argsort(sum)
taka=0
aoki=0
for i in lis:
    if N%2==0:
        if i%2!=0:
            taka+=AB[lis[i]][0]
        else:
            aoki+=AB[lis[i]][1]
    else:
        if i%2==0:
            taka+=AB[lis[i]][0]
        else:
            aoki+=AB[lis[i]][1]

print(int(taka-aoki))

Submission status: Accepted

Solution code:
N = int(input())

check = []
for i in range(N):
    inp = input().split()
    check.append([i, int(inp[0]), int(inp[1]), int(inp[0])+int(inp[1])])

check = sorted(check, key=lambda x:x[3])
check.reverse()
#print(check)

count = 0
ans = 0
while(count+1 < N):
    ans += check[count][1]
    ans -= check[count+1][2]
    count += 2

if count+1==N:
    ans += check[count][1]

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=[]
b=[]
c=[]
for i in range(n):
    s=input().split()
    A,B=int(s[0]),int(s[1])
    a.append(A)
    b.append(B)
    c.append(A+B)
c.sort(reverse=True)
s1=0
s2=0
for i in range(n):
    s1+=a[i]
    if i%2:
        s2+=c[i]
print(s1-s2)

Submission status: Accepted

Solution code:
n = int(input())
x = [[0]*3 for i in range(n)]
for i in range(n):
    a,b = map(int,input().split())	
    x[i] = [a+b,a,b]
x.sort()
C = [[0]*3 for i in range(n)]
for i in range(n):
    C[i]=x[n-1-i]
x = C

A = 0
T = 0
for i in range(n):
    if i%2 == 0:
        A+=x[i][1]
    else:
        T+=x[i][2]
print(A-T)

Submission status: Accepted

Solution code:
N = int(input())
dish = [[0 for _ in range(3)] for _ in range(N)]
Takahashi = []
Aoki = []
for i in range(N):
    dish[i][0],dish[i][1] = [int(x) for x in input().split()]
    dish[i][2] = dish[i][0]+dish[i][1]
dish.sort(key=lambda x:(x[2],x[0],x[1]),reverse=True)
for i in range(N):
    if(i%2==0):
        Takahashi.append(dish[i][0])
    else:
        Aoki.append(dish[i][1])

print(sum(Takahashi)-sum(Aoki))

Submission status: Accepted

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

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

A.sort(key=lambda x:x[1] + x[0],reverse=True)


print(sum([a[0] for a in A[0::2]]) - sum([a[1] for a in A[1::2]]))

Submission status: Accepted

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

for i in range(n):
    ab[i].append(ab[i][0] + ab[i][1])
ab.sort(key=lambda x: x[2], reverse = True)
takahashi = 0
aoki = 0
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:
from operator import itemgetter
n=int(input())
li=[]
x=[0,0]
for i in range(n):
  a, b = list(map(int, input().split()))
  li.append([a,b,a+b])

li.sort(reverse=True, key=itemgetter(2))
for i in range(n):
  x[i % 2] += li[i][i % 2]

print(x[0]-x[1])

Submission status: Accepted

Solution code:
import math
from operator import itemgetter
n = int(input())
m =[]
l1 =[]
l2 =[]
for i in range(n):
    m.append(input().split())
    l1.append([int(m[i][0])+int(m[i][1]),int(m[i][0]),int(m[i][1])])
l1.sort(key=itemgetter(0),reverse=True)
ans = 0
if n%2 == 0:
    m = math.floor(n/2)
    for i in range(m):
        ans += l1[2*i][1]
        ans -= l1[(2*i)+1][2]
else:
    ans += l1[0][1]
    m = math.floor((n-1)/2)#n=3 m=1
    for i in range(1,m+1):
        ans -= l1[(2*i)-1][2]
        ans += l1[(2*i)][1]

print(ans)

Submission status: Accepted

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

rec = sorted(rec, reverse=True)
for i in range(N):
    if i % 2 == 0:
        num += rec[i]
print(num)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

li = list(sorted(box, key=lambda x: sum(x), reverse = True))

taka = []
aoki = []

for i in range(len(li)):
    taka.append(li[0][0])
    del li[0]
    if li == []:
        print(sum(taka) - sum(aoki))
        quit()
    aoki.append(li[0][1])
    del li[0]
    if li ==[]:
        print(sum(taka) - sum(aoki))
        quit()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

n = int(input())
A = [tuple(int(x) for x in input().split()) for i in range(n)]
A.sort(key=lambda ab: -(ab[0] + ab[1]))
print(sum(a for a, _ in A[::2]) - sum(b for _, b in A[1::2]))

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline()
def inpl(): return [int(i) for i in input().split()]

N = int(input())
AB = [inpl() for _ in range(N)]
ABS = [(i+j, num) for num, (i, j) in enumerate(AB)]
ABS.sort(reverse = True)
ans = 0
ctr = True
for _, i in ABS:
    ans += (2*ctr -1)*AB[i][not ctr]
    ctr = not ctr
print(ans)

Submission status: Accepted

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

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

diff = []
for _ in range(N):
    diff.append(B[_] + A[_])

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

sort_index = diff.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:
N = int(input())
A = []
B = []
su = []
for i in range(N):
    a, b = (int(i) for i in input().split())
    A.append(a)
    B.append(b)
    su.append( (a + b, a - b, a, b) )
su.sort()
su.reverse()

cc = 0
for i in range(N):
    if i < N-1 and su[i][0] == su[i+1][0]:
        cc += 1
    else:
        if cc:
            temp = []
            for j in range(i-cc, i+1):
                temp.append(su[j])
            temp2 = []
            for j in range(i-cc, i+1):
                if j % 2 == 0:
                    temp2.append(temp.pop(0))
                else:
                    temp2.append(temp.pop())
            for j in range(i-cc, i+1):
                su[j] = temp2[j-(i-cc)]
            cc = 0
ans = 0
for i in range(N):
    if i%2 == 0:
        ans += su[i][2]
    else:
        ans -= su[i][3]

print(ans)

Submission status: Accepted

Solution code:
def run(N, A, B):
    sum_AB = {}
    for i in range(N):
        sum_AB[i] = A[i] + B[i]
    sum_AB = sorted(sum_AB.items(), key=lambda x: x[1], reverse=True)
    turn_A = True
    sum_A = 0
    sum_B = 0
    for tmp in sum_AB:
        if turn_A:
            sum_A += A[tmp[0]]
        else:
            sum_B += B[tmp[0]]
        turn_A = not turn_A
    return sum_A - sum_B
    '''
    diff = {}
    for i in range(N):
        diff[i] = A[i] - B[i]
    diff = sorted(diff.items(), key=lambda x: x[1])
    print(diff)
    li = 0
    ri = N - 1
    sum_A = 0
    sum_B = 0
    turn_A = True
    for _ in range(N):
        if -diff[li][1] >= diff[ri][1]:
            if turn_A:
                sum_A += A[li]
            else:
                sum_B += B[li]
            li += 1
        else:
            if turn_A:
                sum_A += A[ri]
            else:
                sum_B += B[ri]
            ri -= 1
        turn_A = not turn_A
    return sum_A - sum_B
    '''


def main():
    N = int(input())
    A = []
    B = []
    for _ in range(N):
        tmp = list(map(int, input().split()))
        A.append(tmp[0])
        B.append(tmp[1])
    print(run(N, A, B))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
C = [-1] * N
for i in range(N):
    ai, bi = [int(_) for _ in input().split()]
    C[i] = (ai + bi, ai, bi)

C.sort(reverse=True)

result = 0
for i, ci in enumerate(C):
    if i % 2 == 0:
        result += ci[1]
    else:
        result -= ci[2]

print(result)

Submission status: Accepted

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


#%%
# n = 3
# ab = [
# [20, 10, 30],
# [20, 20, 40],
# [20, 30, 50]]


#%%
ab.sort(key=lambda x: -x[2])


    


#%%
takahashi = 0
for i in ab[0::2]:
    takahashi += i[0]

aoki = 0
for i in ab[1::2]:
    aoki += i[1]

#%%
print(takahashi - aoki)

#%%

Submission status: Accepted

Solution code:
N = int(input())
ABS = []
for _ in range(N):
    a, b = map(int, input().split())
    ABS.append((a, b, a + b))
ABS.sort(key=lambda x: x[2], reverse=True)
print(sum([x[0] for x in ABS[::2]]) - sum([x[1] for x in ABS[1::2]]))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def select(list_, selected_set):
    while True:
        s = list_.pop()
        if not s[0] in selected_set:
            return s


n = int(input())
ab = []

for i in range(n):
    ai, bi = map(int, input().split())
    ab.append((i, ai, bi))

flag = True
list_a = sorted(ab, key=lambda x: (x[1] + x[2]))
list_b = sorted(ab, key=lambda x: (x[2] + x[1]))
a = 0
b = 0
selected = set()
for _ in range(n):
    s = None
    if flag:
        s = select(list_a, selected)
        a += s[1]
    else:
        s = select(list_b, selected)
        b += s[2]
    selected.add(s[0])

    flag = not(flag)

print(a - b)

Submission status: Accepted

Solution code:
n = int(input())
a,b,c=[],[],[]
for i in range(n):
    a_b = list(map(int, input().split()))
    a.append(a_b[0])
    b.append(a_b[1])
    c.append(a_b[0]+a_b[1])
d = sorted(enumerate(c), key=lambda x: x[1], reverse=True)
taka,aoki=0,0
for i in range(n):
    if i % 2 ==0:
        taka = taka + a[d[i][0]]
    else:
        aoki = aoki + b[d[i][0]]
print(taka-aoki)

Submission status: Accepted

Solution code:
N = int(input())
ABC = [[0] * 3 for i in range(N)]
for i in range(N):
    ABC[i][0], ABC[i][1] = list(map(int, input().split()))
    ABC[i][2] = - (ABC[i][0] + ABC[i][1])

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

taka = 0
aoki = 0
for i in range(N):
    if i % 2 == 0:
        taka += ABC[i][0]
    else:
        aoki += ABC[i][1]

print(taka - aoki)

Submission status: Accepted

Solution code:
N = int(input())
A = [0] * N
B = [0] * N
Sum = [[0] * 2 for i in range(N)]

for i in range(N):
    A[i], B[i] = map(int, input().split())
    Sum[i][0] = i
    Sum[i][1] = A[i] + B[i]
    
a = 0
b = 0
turn = 0

Sum.sort(key=lambda x:x[1], reverse=True)
    
for i in range(N):
    if turn == 0:
        a += A[Sum[i][0]]
        turn = 1
    else:
        b += B[Sum[i][0]]
        turn = 0

print(a - b)

Submission status: Accepted

Solution code:
#A
"""
n, a, b = map(int, input().split())
ab = n / 2
ansa = min(a, b)
ansb = max((a+b)-n, 0)
print(ansa,ansb)
"""

#B
"""
n = int(input())
a = input()
b = input()
c = input()
ans = 0
for i in range(n):
    bf = [a[i], b[i], c[i]]
    bf = set(bf)
    ans += len(bf) - 1
print(ans)
"""

#C 

n = int(input())
ab = []
aba = []
abb = []
for i in range(n):
    a,b = map(int, input().split())
    ab.append([a + b, i])
    aba.append(a)
    abb.append(b)
ab.sort(reverse = True, key = lambda x:x[0])
ans = 0
for i in range(n):
    if i % 2 == 0:
        ans += aba[ab[i][1]]
    else:
        ans -= abb[ab[i][1]]
print(ans)


#D
"""
n, m = map(int, input().split())
ab = [[] for _ in range(n)]
for i in range(n + m - 1):
    a,b = map(int, input().split())
    ab[a-1].append(b-1)
print(ab)
ans = [0 for i in range(n)]
def yosozero(i):
    if ans[i]
"""

Submission status: Accepted

Solution code:
from operator import itemgetter
N = int(input())
AB = [list(map(int, input().split())) for _ in range(N)]
A, B = zip(*AB)
ABC = [[A[i], B[i], A[i]+B[i]] for i in range(N)]
ABC.sort(key=itemgetter(2, 0, 1), reverse=True)
ans = 0
for i in range(N):
    if i % 2 == 0:
        ans += ABC[i][0]
    else:
        ans -= ABC[i][1]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

A = [[] for i in range(N)]

for i in range(N):
  a, b = [int(j) for j in input().split()]
  c = a + b
  A[i] = [a, b, c]

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

diff = 0

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

print(diff)

Submission status: Accepted

Solution code:
import sys

n = int(input())
ab = [tuple(map(int, line.split())) for line in sys.stdin.readlines()]
abd = [(a + b, a, b) for a, b in ab]
abd.sort(reverse=True)
# print(abd)
print(sum(x[1] for x in abd[::2]) - sum(x[2] for x in abd[1::2]))