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: Runtime Error

Solution code:
def card_game():
    n = int(input())
    a_li = [int(i) for i in input().split()]
    ans = 0
    turn_switch = True

    li = a_li.sorted()

    for i in li:
        if turn_switch:
            ans += i
            turn_switch = False
        else:
            ans -= i
            turn_switch = True

    return ans


if __name__ == '__main__':
    print(card_game())

Submission status: Runtime Error

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

min=100000000000000
min_index=n+1
for i in range(len(h)):
  v = abs(t - h[i]*0.006)
  if v < min:
    min=v
    min_index=i
print(min_index+1)

Submission status: Runtime Error

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

min=100000000000000
min_index=n+1
for i in range(n):
  v = abs(t - h[i]*0.006)
  if v < min:
    min=v
    min_index=i
print(min_index+1)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

a.sort()[::-1]

for i in range(n):
    c = c + ((-1)**(i%2))*c[i]

print(c)

Submission status: Runtime Error

Solution code:
n = int(input())
a = int(input())

ans = 'No'
for i in range(a+1):
    if (n-i)%500 == 0:
        ans = 'Yes'
        break
print(ans)

Submission status: Runtime Error

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

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

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
 
ai = []
for i in range(n):
    ai.append(int(input()))

sorted_ai = sorted(ai, reverse=True)

alice = 0
bob = 0

for index, x in enumerate(sorted_ai):
    if (index % 2 == 0):
        alice += x
    else:
        bob += x
        
print(alice - bob)

Submission status: Runtime Error

Solution code:
n = int(input())

ai = []
for i in range(n):
    ai.append(int(input()))

ai = sorted(ai, reverse=True)

alice = 0
bob = 0

for index, x in enumerate(ai):
    if (index % 2 == 0):
        alice += x
    else:
        bob += x
        
print(alice - bob)

Submission status: Runtime Error

Solution code:
n = int(input())
for i in range(n):
    i = int(input())

ai = []
for i in range(n):
    ai.append(int(input()))

ai = sorted(ai, reverse=True)

alice = 0
bob = 0

for index, x in enumerate(ai):
    if (index % 2 == 0):
        alice += x
    else:
        bob += x
        
print(alice - bob)

Submission status: Runtime Error

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

Arr.sort(reverse=True)

i = 0
ans = 0
for a in arr:
  if i%2 == 0:
    ans += a
  else:
    ans -= a

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
alice = 0
bob = 0
tmp_value = 0
tmp_index = 0

while len(a_list) > 0:
    for i in range(len(a_list)):
        if tmp_value < a_list[i]:
            tmp_value = a_list[i]
            tmp_index = i
    alice += tmp_value
    del a_list[tmp_index]
    tmp_value, tmp_index = 0, 0
    if len(a_list) == 0:
        break
    for k in range(len(a_list)):                                                                                  
        if tmp_value < a_list[k]:
            tmp_value = a_list[k]
            tmp_index = k
    bob += tmp_value
    del a_list[tmp_index]

print(str(alice - bob))

Submission status: Runtime Error

Solution code:
input();
al=[int(i) for i in input().split()]
al.sort()[::-1]
print(sum(al[::2])-sum(al[1::2]))

Submission status: Runtime Error

Solution code:
N = int(input())
alist = map(int, input().split())
alist.sort()
alist.reverse()
score_a = sum(alist[::2])
score_b = sum(alist[1::2])
print(score_a - score_b)

Submission status: Runtime Error

Solution code:
i = int(input())
a = list(map(int, input().split()))
a.sort
a.reverse
alice = a[0::2]
bob = a[1::2]
print(alice - bob)

Submission status: Runtime Error

Solution code:
N=int( input())
a=list(set([int(j) for j in range(N)]))
print(a[::2]-a[1::2])

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(n) for n in input().split(" ")]

a.sort()

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

Submission status: Runtime Error

Solution code:
d = '2 7 4'
 d = input()
d = [int(i) for i in d.split()]
d.sort()
s1 = 0
s2 = 0
i = len(d)-1
while(i>=0):
    s1 = s1 + d[i]
    if((i-1)>=0):
        s2 = s2 + d[i-1]
    i = i-2
print(s1-s2)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N = int(input())
cards = map(int, input().split())
A, B = 0, 0
for idx in range(N):
  if idx % 2 == 0:
    A += cards[idx]
  else:
    B += cards[idx]
print(A - B)

Submission status: Runtime Error

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

#N枚のカードがあり、i枚目のカードにはaiという数が書かれています。
#AliceとBobはこれらのカードを使ってゲームを行います。ゲームでは2人が交互に1枚ずつカードを取っていきます。Aliceが先にカードを取ります。
#2人が全てのカードを取った時ゲームは終了し、取ったカードの数の合計がその人の得点になります。2人とも自分の得点を最大化するように最適戦略を取った時、AliceはBobより何点多く獲得できるかを求めてください。

#1<=N<=100, 1<=ai<=100, 入力は整数
#数値例 N=3, a=(2,7,4), Answer: 5

N = int(input())
numbers = list(map(int, input().split()))
Alice_point = 0
Bob_point = 0

numbers.sort()
print(numbers)

while True:
    Alice_point += numbers[0]
    try:
        Bob_point += numbers[1]
        numbers.remove(numbers[0])
        numbers.remove(numbers[0])
    except IndexError:
        break

print(Alice_point - Bob_point)

Submission status: Runtime Error

Solution code:
if __name__ == '__main__':
    n = int(input())
    cardlist = list(map(int, input().split()))
    cardlist.sort()

    alice_s, bob_s = 0, 0
    while cardlist != []:
        alice_s += cardlist.pop()
        bob_s += cardlist.pop()
    ans = alice_s - bob_s
    print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = map(int, input().split())
 
for i in A:
  A_score = 0
  A_score += max(A)
  del max(A)
  B_score = 0
  B_score += max(A)
  del max(A)
  if lens(A) = 0:
    break
print(A_score - B_score)

Submission status: Runtime Error

Solution code:
N = int(input())
A = map(int, input().split())
 
for i in A:
  A_score = 0
  A_score += max(A)
  del max(card_list)
  B_score = 0
  B_score += max(A)
  del max(A)
  if lens(A) = 0:
    break
print(A_score - B_score)

Submission status: Runtime Error

Solution code:
N = int(input())
A = map(int, input().split())
 
for i in A:
  A_score = 0
  A_score += max(card_list)
  del max(card_list)
  B_score = 0
  B_score += max(card_list)
  del max(card_list)
  if lens(card_list) = 0:
    break
print(A_score - B_score)

Submission status: Runtime Error

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

for i in A:
  A_score = 0
  A_score += max(card_list)
  del max(card_list)
  B_score = 0
  B_score += max(card_list)
  del max(card_list)
  if lens(card_list) = 0:
    break
print(A_score - B_score)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N = int(input())
A = int(input())
print(A)

Submission status: Runtime Error

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

for i in A:
  A_score = 0
  A_score += max(card_list)
  del max(card_list)
  B_score = 0
  B_score += max(card_list)
  del max(card_list)
  if lens(card_list) = 0:
    break
print(A_score - B_score)

Submission status: Runtime Error

Solution code:
N = int(input())
A = int(input())
card_list = A

Submission status: Runtime Error

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

for i in range(3):
  A_score = 0
  A_score += max(card_list)
  del max(card_list)
  B_score = 0
  B_score += max(card_list)
  del max(card_list)
  if lens(card_list) = 0:
  break
print(A_score - B_score)

Submission status: Runtime Error

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

for i in A:
  A_score = 0
  A_score += max(card_list)
  del max(card_list)
  B_score = 0
  B_score += max(card_list)
  del max(card_list)
  if lens(card_list) = 0:
  break
print(A_score - B_score)

Submission status: Runtime Error

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

for i in A:
  A_score = 0
  A_score += max(card_list)
  del max(A)
  B_score = 0
  B_score += max(card_list)
  del max(A)
  if lens(card_list) = 0:
  break

Submission status: Runtime Error

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

for i in A:
  A_score = 0
  A_score += max(card_list)
  del max(A)
  B_score = 0
  B_score += max(card_list)
  del max(A)
  
if lens(card_list) = 0:
  break

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
a = sorted(a)
alice,bob=0,0
cnt=0
for x in range(n):
    global alice
    global bob
    while cnt<n:
        alice+=a[cnt]
        cnt+=1
        bob+=a[cnt]
        cnt+=1
print(abs(alice-bob))

Submission status: Runtime Error

Solution code:
alice = 0
bob = 0
n = int(input())
a = sorted(list(map(int, input().split(' ')))).reverse()

for i in a[::2]: alice += i
for i in a[1::2]: bob += i
print(alice - bob)

Submission status: Runtime Error

Solution code:
alice = 0
bob = 0
n = int(input())
a = sorted(list(map(int, input().split(' ')))).reverse()

for i in asorted[::2]: alice += i
for i in asorted[1::2]: bob += i
print(alice - bob)

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
	int n;
	cin >>n;
	int a[110];
	for(int i=0;i<n;++i)cin >>a[i];
	sort(a,a+n,greater<int>());
	int alic=0;
	int bob=0;
	for(int i=0;i<n;++i){
		if(i%2==0){
			alic+=a[i];
		}
		else{
			bob+=a[i];
		}
	}
	cout <<alic-bob<<endl;
}

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(raw_input())
x = sorted([int(i) for i in raw_input().split(" ")],reverse=True)
print(sum(x[::2]) - sum(x[1::2]))

Submission status: Runtime Error

Solution code:
n=int(input())
a=[]
a.append(int(input()))
a.sort()
s=0
i=n-1
while i>=0:
 s+=a[i]
 i-=2
i=n-2
while i>=0:
 s-=a[i]
 i-=2
print(s)

Submission status: Runtime Error

Solution code:
n=int(input())
a=[]
a.append(int(input()))
a.sort()
s=0
i=n-1
while i>0:
 s+=a[i]
 i-=2
i=n-2
while i>0:
 s-=a[i]
 i-=2
print(s)

Submission status: Runtime Error

Solution code:
n=int(input())
a=[]
a.append(int(input()))
a.sort()
s=0
i=n-1
while i>0:

Submission status: Runtime Error

Solution code:
money = int(input())
A = int(input())

if (money % 500) > A:
    print('NO')
else:
    print('YES')

Submission status: Runtime Error

Solution code:
N=int(input())
a=sorted(map(int,input().split()),reversed=True)
ans=sum(((-1)**i)*x for i,x in enumerate(a))
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
a_i = list(map(int, input().split('')))
 
x = []
y = []
 
if N%2==0:
    for j in range(int(N/2)):
        x.append(max(a_i))
        a_i.remove(max(a_i))
        y.append(max(a_i))
        a_i.remove(max(a_i))
 
else:
    for j in range(int((N/2)+1)):
        x.append(max(a_i))
        a_i.remove(max(a_i))
        if a_i == []:
            break
        y.append(max(a_i))
        a_i.remove(max(a_i))
 
X = sum(x)
Y = sum(y)
 
 
if X >= Y:
    print(X - Y)
 
else:
    print(Y - X)

Submission status: Runtime Error

Solution code:
N = int(input())
a_i = []
for i in range(N):
    a_i.append(int(input()))

x = []
y = []

if N%2==0:
    for j in range(int(N/2)):
        x.append(max(a_i))
        a_i.remove(max(a_i))
        y.append(max(a_i))
        a_i.remove(max(a_i))

else:
    for j in range(int((N/2)+1)):
        x.append(max(a_i))
        a_i.remove(max(a_i))
        if a_i == []:
            break
        y.append(max(a_i))
        a_i.remove(max(a_i))

X = sum(x)
Y = sum(y)


if X >= Y:
    print(X - Y)

else:
    print(Y - X)

Submission status: Runtime Error

Solution code:
#coding:utf-8

int_over = float("inf")

class func():
	def MaxGetNum(list_1):
		int MAXnum = 0
		for i in range(len(list_1)+1):
			if MAXnum<list_1[i]:
				MAXnum = list_1[i]
	return MAXnum
	
	def MinGetNum(list_1):
		int_over=float("inf")
		int MINnum = 0
		for i in range(len(list_1)+1):
			if int_over>list_1[i]:
				MINnum = list_1[i]
	return MINnum

	def zenki():
		for i in range(A):
			pass
			for n in range(B):
				pass
				for m in range(C):
					pass
	return X

	
N = int(input())
a = list(map(int,input().split()))
a_sort = sort(a)
alice = 0
bob = 0
for i in range(N):
	alice += a_sort[i]
	a_sort.pop(a_sort[i])
	bob += a_sort[i]
	a_sort.pop(a_sort[i])
print(alice-bob)

Submission status: Runtime Error

Solution code:

n = int(input())
a = input().split(" ")

a.sort()
m = n % 2

alice = 0
bob = 0

while n > 0:
    if n % 2 == m:
        alice += a[-1]
    else:
        bob += a[-1]
    a.pop()
    n -= 1

print(alice - bob)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
a = list(map(int(input().strip().split())))

a.sorted(reverse=True)

alice = 0
bob = 0
for i in range(n-1):
    if i % 2 is 0:
        alice = alice + int(a[i])
    else:
        bob = bob + int(a[i])
        
print(alice - bob)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
a = list(map(int(input().strip().split())))

a.sort(reverse=True)

alice = 0
bob = 0
for i in range(n-1):
    if i % 2 is 0:
        alice = alice + int(a[i])
    else:
        bob = bob + int(a[i])

ans = alice - bob
        
print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
a = list(map(int(input().strip().split())))

a.sort(reverse=True)

alice = 0
bob = 0
for i in range(n-1):
    if i % 2 is 0:
        alice += a[i]
    else:
        bob += a[i]

print(alice - bob)

Submission status: Runtime Error

Solution code:
N=int(input())
a=[]
for i in range(N):
    a=a+[int(input())]

a=sorted(a,reverse=True)
alice=bob=0
for i in range(N):
    if i%2==0:
        alice=alice+a[i]
    else:
        bob=bob+a[i]
print(alice-bob)

Submission status: Runtime Error

Solution code:
#coding:utf-8

import sys
import deque

N = int(input())
M = list(map(int,input().split()))

alice = 0
bob = 0
for i in range(N):
	alice += max(M)
	M.pop(enumerate(max(M))[0])
	bob += max(M)
	M.pop(enumerate(max(M)))
	if len(M) == 0:
		print(alice-bob)
		sys.exit()

Submission status: Runtime Error

Solution code:
#coding:utf-8

import sys
import deque

N = int(input())
M = list(map,input().split())

alice = 0
bob = 0
for i in range(N):
	alice += max(M)
	M.pop(enumerate(max(M))[0])
	bob += max(M)
	M.pop(enumerate(max(M)))
	if len(M) == 0:
		print(alice-bob)
		sys.exit()

Submission status: Runtime Error

Solution code:
#coding:utf-8

import sys
import deque

N = int(input())
M = list(map,input(),split())

alice = 0
bob = 0
for i in range(N):
	alice += max(M)
	M.pop(enumerate(max(M))[0])
	bob += max(M)
	M.pop(enumerate(max(M)))
	if len(M) == 0:
		print(alice-bob)
		sys.exit()

Submission status: Runtime Error

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

input_lines=input()

input_lines=input()
tmp_lst1=input_lines.split()
tmp_lst2=list(map(int,tmp_lst))
tmp_lst3=tmp_lst2.sort()
tmp_lst=tmp_lst3.reverse()

alice=0
bob=0
for i in tmp_lst:
    if tmp_lst.index(i)%2=0:
        alice=alice+i
    else:
        bob=bob+i

Submission status: Runtime Error

Solution code:
n=int(input())
aaa=[input().split() for i in range(n)]
s=map(int,aaa)
s.reverse()
sum=0
for i in range(n):
    if i%2==0:
        sum+=s[i]
    else:
        sum-=s[i]
print(int(sum))

Submission status: Runtime Error

Solution code:
#金額
num   = int((int(input()) + 1 ) / 2)
retu    = input().split()
card = list( map((lambda x:int(x)),retu))
cardList = sorted(card, reverse=True)
Alice = 0
Bob = 0

for w in range(num):
    Alice = Alice + cardList[w*2]
    Bob = Bob + cardList[w*2+1]

print(Alice - Bob)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())
as = map(int, input().split())
alice = []
bob = []
for i, a in enumerate(as):
    if i%2==0:
        alice.append(a)
    else:
        bob.append(a)
print(sum(alice)-sum(bob))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

a.sort(reverse=True)

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

print(al-bo)