Problem:


Score: 200 points

Problem Statement

We have N cards. A number a_i is written on the i-th card.

Alice and Bob will play a game using these cards. In this game, Alice and Bob alternately take one card. Alice goes first.

The game ends when all the cards are taken by the two players, and the score of each player is the sum of the numbers written on the cards he/she has taken. When both players take the optimal strategy to maximize their scores, find Alice's score minus Bob's score.

Constraints

N is an integer between 1 and 100 (inclusive).

a_i \ (1 \leq i \leq N) is an integer between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:  

N
a_1 a_2 a_3 ... a_N

Output

Print Alice's score minus Bob's score when both players take the optimal strategy to maximize their scores.

Sample Input 1

2
3 1

Sample Output 1

2

First, Alice will take the card with 3. Then, Bob will take the card with 1.
The difference of their scores will be 3 - 1 = 2.

Sample Input 2

3
2 7 4

Sample Output 2

5

First, Alice will take the card with 7. Then, Bob will take the card with 4. Lastly, Alice will take the card with 2. The difference of their scores will be 7 - 4 + 2 = 5. The difference of their scores will be 3 - 1 = 2.

Sample Input 3

4
20 18 2 18

Sample Output 3

18



Sample input:
2
3 1


Sample output:
2

Submission status: Accepted

Solution code:
n=int(input())
a=list(input().split())
for i in range(n):
    a[i]=int(a[i])
a.sort(reverse=True)
total=0
for i in range(n):
    if i%2==0:
        total=total+a[i]
    else:
        total=total-a[i]
print(total)

Submission status: Accepted

Solution code:
import numpy as np
import sys
n = int(input())
a = sorted(list(map(int,input().split())))[::-1]
print(sum(a[::2])-sum(a[1::2]))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
list.sort(a, reverse=True)

goukei = 0

if len(a) % 2 == 1:
	a.append(0)

for i in range(0,len(a),2):
	goukei = goukei + (a[i] - a[i + 1])
#	print(a[i],a[i+1])
print(goukei)

Submission status: Accepted

Solution code:
n=int(input())
a = list(map(int,input().split()))
a.sort(reverse = True)
A=0
B=0
for i in range(n//2+n%2):
    A+=a[i*2]
for i in range(n//2):
    B+=a[i*2+1]
print(A-B)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 0

a.sort(reverse=True)
a_sum = sum(a[0:len(a):2])
b_sum = sum(a[1:len(a):2])

ans = a_sum-b_sum

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
L = list(map(int, input().split()))
L.sort(reverse=True)
L1 = L[0::2]
L2 = L[1::2]
print(sum(L1)-sum(L2))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
n = range(N)
list_Alice = [0]
list_Bob = [0]
A.sort(reverse = True)
for i in n:
  if i%2 == 0:
    list_Alice.append(A[i])
  else:
    list_Bob.append(A[i])
print(sum(list_Alice) - sum(list_Bob))

Submission status: Accepted

Solution code:
n = input()
l = sorted(list(map(int, input().split())), reverse=True)

alice = 0
bob = 0
for key, value in enumerate(l):
    if key % 2 == 0:
        alice += value
    else:
        bob += value
print(alice - bob)

Submission status: Accepted

Solution code:
n=int(input())
a=sorted(list(map(int,input().split())),reverse=True)
A=0
B=0
for ai in range(n):
	if ai%2==0:
		A+=a[ai]
	else:
		B+=a[ai]
print(A-B)

Submission status: Accepted

Solution code:
n=int(input())
a=b=0
p=sorted(list(map(int,input().split())),key=int,reverse=True)
p.append(0)
for i in range(int((n+1)/2)):
    a+=p.pop(0)
    b+=p.pop(0)
print(a-b)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
a.sort()
a.reverse()
b = a[::2]
c = a[1::2]
print(sum(b)-sum(c))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
a.sort()
a.reverse()
alice = 0
bob = 0
for i in range(n):
    if i%2 == 0:
        alice += a[i]
    else:
        bob += a[i]
print(alice-bob)

Submission status: Accepted

Solution code:
N = int(input())
a = sorted([int(x) for x in input().split()], reverse=True)

Alice = sum(a[i] for i in range(0,N,2))
Bob = sum(a) - Alice
print(Alice-Bob)

Submission status: Accepted

Solution code:
N = int(input())
a = sorted(list(map(int, input().split())), reverse=True)

alice = sum(a[0::2])
bob = sum(a[1::2])
print(alice - bob)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))

a.sort()
a.reverse()

Alice=a[0::2]
Bob = a[1::2]

print(sum(Alice) - sum(Bob))

Submission status: Accepted

Solution code:
N = int(input())
numbers = list(map(int,input().split()))
numbers.sort(reverse=True)
Bob = 0
Alice = 0
for i,number in enumerate(numbers):
  if i == 0:
    Alice += number
  elif i%2 == 0:
    Alice += number
  else:
    Bob += number
ans = Alice - Bob
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
a_list.sort(reverse=True)

alice_sum = 0
bob_sum = 0
for i in range(n):
    if i % 2 == 0:
        alice_sum += a_list[i]
    else:
        bob_sum += a_list[i]

print(alice_sum - bob_sum)

Submission status: Accepted

Solution code:
n = int(input())
a = sorted(map(int, input().split()))[::-1]
print(sum(a[::2]) - sum(a[1::2]))

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
a.sort(reverse=True)
print(sum(a[::2]) - sum(a[1::2]))

Submission status: Accepted

Solution code:
N = int(input())
Cards = sorted(list(map(int, input().split())),reverse = True)
Alice = 0
Bob = 0

for i in range(N):
    if i % 2 != 0:
        Bob = Bob + Cards[i]
    else:
        Alice = Alice + Cards[i]

print(Alice - Bob)

Submission status: Accepted

Solution code:
N=int(input())
A=sorted(list(map(int,input().split())),reverse=True)
print(sum(A[0::2])-sum(A[1::2]))

Submission status: Accepted

Solution code:
N = int(input())
list_a = list(map(int, input().split()))

list_a.sort(reverse=True)
Alice_score = sum(list_a[::2])
Bob_score = sum(list_a[1::2])

print(Alice_score - Bob_score)

Submission status: Accepted

Solution code:
N = int(input())
list_a = list(map(int, input().split()))

Alice_score = 0
Bob_score = 0

while len(list_a) > 0:
    Alice_score += list_a.pop(list_a.index(max(list_a)))
    if len(list_a) > 0:
        Bob_score += list_a.pop(list_a.index(max(list_a)))

print(Alice_score - Bob_score)

Submission status: Accepted

Solution code:
_ = input()
A = sorted(list(map(int, input().strip().split(' '))),reverse=True)

print(sum(A[::2])-sum(A[1::2]))

Submission status: Accepted

Solution code:
N = int(input())

a = list(map(int, input().split()))

a.sort()
a.reverse()

Alice = 0
Bob = 0

# print(a)


for i in range(len(a)):
  if i % 2 == 0:
    Alice+=a[i]
  else:
    Bob+=a[i]

print(Alice -Bob)

Submission status: Accepted

Solution code:
# 自分の回答
n = int(input())
array = list(map(int, input().split()))

array.sort(reverse=True)

a_sum = 0
b_sum = 0

for i in range(n):
    if i % 2 == 0:
        a_sum += array[i]
    else:
        b_sum += array[i]

print(a_sum - b_sum)

Submission status: Accepted

Solution code:
n = int(input())
arr = sorted(list(map(int,input().split())), reverse = True)
print(sum(arr[0::2])-sum(arr[1::2]))

Submission status: Accepted

Solution code:
N = int(input())
A = map(int,input().split())

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

alice = 0
bob = 0

for i in range(N//2):
    alice += A[2*i]
    bob += A[2*i+1]
    
if N%2 == 1:
    alice += A[N-1]
    
print(alice-bob)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
sum1 = 0
sum2 = 0
a.sort(reverse=True)
for i in range(n):
	if i % 2 == 0:
		sum1 += a[i]
	else:
		sum2 += a[i]
print(sum1 - sum2)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
a.sort(reverse = True)
print(sum(a[0::2]) - sum(a[1::2]))

Submission status: Accepted

Solution code:
N = int(input())
a = sorted(list(map(int, input().split())), reverse=True)
Alice = [a[i] for i in range(N) if i % 2 == 0]

print(sum(Alice) * 2 - sum(a))

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    a = list(map(int, input().split()))
    a.sort(reverse=True)    #  降順で整列

    score_of_Alice = 0
    score_of_Bob = 0

    for i in range(N):
        if i % 2 == 0:
            score_of_Alice += a[i]
        else:
            score_of_Bob += a[i]

    print(score_of_Alice - score_of_Bob)

solve()

Submission status: Accepted

Solution code:
n = int(input())
a = sorted(list(map(int,input().split())),reverse=True)
print(sum(a[::2])-sum(a[1::2]))

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
mod = 10**9 + 7

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
sa = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]

n = ni()
a = na()
a.sort(reverse=True)
alice = sum(a[::2])
bob = sum(a[1::2])
print(alice - bob)

Submission status: Accepted

Solution code:
N = int(input())
an = list(map(int, input().split()))

alis,bob = 0, 0
for i in range(N):
  if i%2 == 0:
    alis += max(an)
    an[an.index(max(an))]= 0
  elif i%2 == 1:
    bob += max(an)
    an[an.index(max(an))] = 0

print(alis - bob)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(j) for j in input().split()]
a.sort()
a.reverse()
alice,bob=0,0
for i in range(n):
    if i%2==0:
        alice+=a[i]
    else:
        bob+=a[i]
print(alice-bob)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

# input
N = int(input())
a = list(map(int, input().split()))

# solve
a.sort(reverse=True)

# output
print(sum(a[::2]) - sum(a[1::2]))

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
A = []
B = []

if len(a) % 2 == 1:
	A.append(min(a))
	a.remove(min(a))

while len(a) > 0:
	A.append(max(a))
	a.remove(max(a))
	B.append(max(a))
	a.remove(max(a))

print(sum(A) - sum(B))

Submission status: Accepted

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
a.sort(reverse=True)

print(sum(a[::2]) - sum(a[1::2]))

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))

ptA = 0
ptB = 0

for i in range(N):
    if i % 2 == 0:
        temp = max(a)
        ptA += temp
        a.remove(temp)
    else:
        temp = max(a)
        ptB += temp
        a.remove(temp)

print(ptA-ptB)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort()
s = reversed(a)
print(abs(sum(a[0::2]) - sum(a[1::2])))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
alice = sum([a[i] for i in range(n) if i % 2 == 0])
bob = sum([a[i] for i in range(n) if i % 2 == 1])
print(alice-bob)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())

a_inputs = input().split()

a = np.zeros(N)
for i in range(N):
  a[i] = int(a_inputs[i])
  
a = np.sort(a)[::-1]

alice = a[0::2].sum()
bob   = a[1::2].sum()

print(int(alice - bob))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
alice = []
bob = []
a.sort()
a = a[::-1]
for i in range(n):
    if len(alice) > len(bob):
        bob.append(a[i])
    else:
        alice.append(a[i])
print(sum(alice) - sum(bob))

Submission status: Accepted

Solution code:
n = int(input())
A = sorted([int(num) for num in input().split()], reverse=True)
print(sum(A[::2]) - sum(A[1::2]))

Submission status: Accepted

Solution code:
N=int(input())
aList=sorted(list(map(int,input().split())))[::-1]

Alice=0
Bob=0

for i in range(1,N+1):
  if i%2 == 1:
    Alice+= aList[i-1]
  else :
    Bob+= aList[i-1]

print(Alice-Bob)

Submission status: Accepted

Solution code:
n = int( input())
a = list(map(int, input().split()))
a = list(reversed(sorted(a)))
first = 0
second = 0
for i in range(0, n, 2):
      first += a[i]
for i in range(1, n, 2):
     second += a[i]
print(first - second)

Submission status: Accepted

Solution code:
n = int(input())
a = list(reversed(sorted(list(map(int,input().split())))))
print(sum(a[::2]) - sum(a[1::2]))

Submission status: Accepted

Solution code:
#!/usr/bin/python
# coding: UTF-8

from math import *
import os
import sys

#import scipy.optimize
import numpy as np


##################################################################
if __name__ == "__main__":
    Input = [raw_input() for i in range(2)]
    N = int(Input[0])
    A = list(map(int,Input[1].split()))
    A.sort()
    A.reverse()
    Alise = sum([A[i] for i in np.arange(0,N,2)])
    Bob = sum(A)-Alise
    print Alise-Bob

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))

a.sort(reverse=True)
print(sum(a[:: 2]) - sum(a[1::2]))

Submission status: Accepted

Solution code:
n = int(input())
a = sorted(list(map(int, input().split())))
print(abs(sum(a[0::2]) - sum(a[1::2])))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
Alice = []
Bob = []
A.sort(reverse=True)

for i in range(N):
    if i%2 == 0:
        Alice.append(A[i])
    elif i%2 == 1:
        Bob.append(A[i])

ans = sum(Alice) - sum(Bob)
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
from fractions import gcd
# import queue
# from collections import Counter
# from itertools import accumulate
# from functools import reduce

def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

big_prime = 1000000007

"""
# 標準入力取得
## 文字列
 =  sys.stdin.readline().rstrip()
 =  list(sys.stdin.readline().rstrip())

## 数値
 =  int(sys.stdin.readline())
 =  map(int, sys.stdin.readline().split())
 =  list(map(int, sys.stdin.readline().split()))
 =  [list(map(int,list(sys.stdin.readline().split()))) for i in range(N)]
"""


N =  int(sys.stdin.readline())
L =  list(map(int, sys.stdin.readline().split()))

L.sort(reverse=True)

ali= 0
bob = 0
for i, x in enumerate(L):
    if i % 2 == 0:
        ali += x
    else:
        bob += x
print(ali - bob)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))

a.sort(reverse=True)

A = a[0:N:2]
B = a[1:N:2]

print(sum(A)-sum(B))

Submission status: Accepted

Solution code:
n = int(input())
a = sorted(map(int, input().split()), reverse=True)
print(sum(a[::2])-sum(a[1::2]))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))

A.sort(reverse=True)

print(sum(A[::2]) - sum(A[1::2]))

Submission status: Accepted

Solution code:
n = int(input())
a = sorted(map(int, input().split()),reverse=1)
print(sum(a[::2])-sum(a[1::2]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
print(sum(a[0::2])-sum(a[1::2]))

Submission status: Accepted

Solution code:
n=int(input())
a=sorted(list(map(int, input().split())),reverse=True)
x=sum(a[::2])
y=sum(a[1::2])
print(x-y)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
alice = 0
bob = 0

while True:
    alice += a.pop(a.index(max(a)))
    if len(a) <= 0:
        break
    bob += a.pop(a.index(max(a)))
    if len(a) <= 0:
        break

print(alice-bob)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
A = sorted(A, reverse=True)
print(sum(A[::2])-sum(A[1::2]))

Submission status: Accepted

Solution code:
N=int(input())
cards = list(map(int, input().split()))
cards.sort(reverse=True)
Alice=0
Bob=0
for i in range (len(cards)):
    if i%2==0:
        Alice+=cards[i]
    else:
        Bob+=cards[i]

print(Alice-Bob)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
Alice_score = 0
Bob_score = 0

a.sort(reverse = True)

for i in range(len(a)):
    if i % 2 == 0:
        Alice_score += a[i]
    else:
        Bob_score += a[i]

print(Alice_score - Bob_score)

Submission status: Accepted

Solution code:
N = int(input())
a =[int(i) for i in input().split()]
a.sort()
a.reverse()
x = a[::2] 
y = a[1::2]
print(sum(x)-sum(y))

Submission status: Accepted

Solution code:
N = int(input())
a = sorted(list(map(int, input().split())), reverse=True)

alice = 0
for i in range(0, N, 2):
  alice += a[i]

print(alice - sum(a) + alice)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))

a.sort(reverse=True)
score = [0, 0]
for i in range(n):
    score[i%2] += a[i]

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

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
B = sorted(A,reverse=True)

ans1=0
ans2=0
for i in range(n):
    if (i+2)%2==0:
        ans1+=B[i]
    else:
        ans2+=B[i]
print(int(ans1-ans2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))

a.sort()
alice = 0
bob = 0
for i in range(N):
    if i % 2 == 0:
        alice += a[N - 1 - i]
    else:
        bob += a[N - 1 - i]
        
print(alice - bob)

Submission status: Accepted

Solution code:
N=int(input())

a=list(map(int,input().split()))
a=sorted(a,reverse=True)
Alice=[]
Bob=[]

for index,num in enumerate(a):
    if index%2 == 0:
        Alice.append(num)
    else:
        Bob.append(num)
        

ans=sum(Alice)-sum(Bob)

print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N = int(input())
        a = list(map(int, input().split()))

        a.sort(reverse=True)

        alice = sum(a[::2])
        bob = sum(a[1::2])

        print(alice - bob)


if __name__ == '__main__':
    AtCoder().main()

Submission status: Accepted

Solution code:
n = int(input())
a = sorted(list(map(int,input().split())), reverse = True)

alice = 0
bob = 0

for i in range(len(a)):
    if i % 2 == 0:
        alice += a[i]
    else:
        bob += a[i]

print(alice - bob)

Submission status: Accepted

Solution code:
def ABC088B():    
    n = int(input())
    ai = list(map(int,input().split()))

    # リストを降順にソートする
    ai.sort(reverse=True)
    ans = sum(ai[0::2]) - sum(ai[1::2])

    print(ans)

if __name__ == '__main__' :
    ABC088B()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))

alice = []
bob = []

while A != []:
	alice.append(max(A))
	A.remove(max(A))
	if A != []:
		bob.append(max(A))
		A.remove(max(A))
	else: 
		break
print(sum(alice)-sum(bob))

Submission status: Accepted

Solution code:
N,A=open(0);A=sorted(map(int,A.split()));print(sum(A[::-2])-sum(A[-2::-2]))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
alice=0
bob=0
if n%2==0:
    while not len(a)==0:
        alice+=max(a)
        a.remove(max(a))
        bob+=max(a)
        a.remove(max(a))
    print(alice-bob)
else:
    while not len(a)==1:
        alice+=max(a)
        a.remove(max(a))
        bob+=max(a)
        a.remove(max(a))
    alice+=max(a)
    print(alice-bob)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort()
a.reverse()
print(sum(a[::2]) - sum(a[1::2]))

Submission status: Accepted

Solution code:
N = int(input())
card_values = list(map(int, input().split(" ")))
 
card_values_sorted = sorted(card_values, reverse=True)

Alice_score = 0
Bob_score = 0
for i in range(N):
    if i%2 == 0:
        Alice_score += card_values_sorted[i]
    else:
        Bob_score += card_values_sorted[i]
 
print(Alice_score - Bob_score)