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:
# coding: utf-8
import sys
# import bisect
# import math
# import numpy as np


"""Template"""


class IP:
    """
    入力を取得するクラス
    """
    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        """
        1文字の取得に使います
        :return: int
        """
        return int(self.input())

    def S(self):
        """
        1文字の取得(str
        :return: str
        """
        return self.input()

    def IL(self):
        """
        1行を空白で区切りリストにします(int
        :return: リスト
        """
        return list(map(int, self.input().split()))

    def SL(self):
        """
        1行の文字列を空白区切りでリストにします
        :return: リスト
        """
        return list(map(str, self.input().split()))

    def ILS(self, n):
        """
        1列丸々取得します(int
        :param n: 行数
        :return: リスト
        """
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        """
        1列丸々取得します（str
        :param n: 行数
        :return: リスト
        """
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        """
        Some Int LineS
        横に複数、縦にも複数
        :param n: 行数
        :return: list
        """
        return [self.IL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        """
        Half enumeration
        半分全列挙です
        pの要素の和の組み合わせを作ります。
        ソート、重複削除行います
        :param p: list : 元となるリスト
        :return: list : 組み合わせられた和のリスト
        """
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        """
        bit_full_search2
        bit全探索の改良版
        全探索させたら2進数のリストと10進数のリストを返す
        :return: list２つ : １個目 2進数(16桁)  2個目 10進数
        """
        # 参考
        # https://blog.rossywhite.com/2018/08/06/bit-search/
        # https://atcoder.jp/contests/abc105/submissions/4088632
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    """右からj+1番目のiが1かどうか判定"""
                    # output.append(a[j])
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        """
        ソート関係行います。色々な設定あります。
        :param s: 元となるリスト
        :param r: reversするかどうか 0=False 1=True
        :param m: （2次元配列）何番目のインデックスのソートなのか
        :return: None
        """
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        """
        bit探索で使います。0以上のときにTrue出します
        自然数だからn
        :param a: int
        :param b: int
        :return: bool
        """
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        """
        bit探索で使います。1のときにTrue出すよ
        oneで1
        :param a: int
        :param b: int
        :return: bool
        """
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        """
        Round up
        小数点切り上げ割り算
        :param x: int
        :param y: int
        :return: int
        """
        return -(-x//y)

    def ave(self, a):
        """
        平均を求めます
        :param a: list
        :return: int
        """
        return sum(a) / len(a)


"""ここからメインコード"""


def main():
    # 1文字に省略
    r, e = range, enumerate
    ip = IP()
    id = Idea()

    """この下から書いてね"""
    n = ip.I()
    s = [[a, b, a + b] for a, b in ip.SILS(n)]

    id.S(s, 1, 2)

    ap = bp = 0
    for i, t in e(s):
        if i % 2 == 0:
            ap += t[0]
        else:
            bp += t[1]

    print(ap - bp)


main()

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
# import bisect
# import math
# import numpy as np


"""Template"""


class IP:
    """
    入力を取得するクラス
    """
    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        """
        1文字の取得に使います
        :return: int
        """
        return int(self.input())

    def S(self):
        """
        1文字の取得(str
        :return: str
        """
        return self.input()

    def IL(self):
        """
        1行を空白で区切りリストにします(int
        :return: リスト
        """
        return list(map(int, self.input().split()))

    def SL(self):
        """
        1行の文字列を空白区切りでリストにします
        :return: リスト
        """
        return list(map(str, self.input().split()))

    def ILS(self, n):
        """
        1列丸々取得します(int
        :param n: 行数
        :return: リスト
        """
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        """
        1列丸々取得します（str
        :param n: 行数
        :return: リスト
        """
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        """
        Some Int LineS
        横に複数、縦にも複数
        :param n: 行数
        :return: list
        """
        return [self.IL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        """
        Half enumeration
        半分全列挙です
        pの要素の和の組み合わせを作ります。
        ソート、重複削除行います
        :param p: list : 元となるリスト
        :return: list : 組み合わせられた和のリスト
        """
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        """
        bit_full_search2
        bit全探索の改良版
        全探索させたら2進数のリストと10進数のリストを返す
        :return: list２つ : １個目 2進数(16桁)  2個目 10進数
        """
        # 参考
        # https://blog.rossywhite.com/2018/08/06/bit-search/
        # https://atcoder.jp/contests/abc105/submissions/4088632
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    """右からj+1番目のiが1かどうか判定"""
                    # output.append(a[j])
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        """
        ソート関係行います。色々な設定あります。
        :param s: 元となるリスト
        :param r: reversするかどうか 0=False 1=True
        :param m: （2次元配列）何番目のインデックスのソートなのか
        :return: None
        """
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        """
        bit探索で使います。0以上のときにTrue出します
        自然数だからn
        :param a: int
        :param b: int
        :return: bool
        """
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        """
        bit探索で使います。1のときにTrue出すよ
        oneで1
        :param a: int
        :param b: int
        :return: bool
        """
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        """
        Round up
        小数点切り上げ割り算
        :param x: int
        :param y: int
        :return: int
        """
        return -(-x//y)

    def ave(self, a):
        """
        平均を求めます
        :param a: list
        :return: int
        """
        return sum(a) / len(a)


"""ここからメインコード"""


def main():
    # 1文字に省略
    r, e = range, enumerate
    ip = IP()
    id = Idea()

    """この下から書いてね"""
    n = ip.I()
    s = ip.SILS(n)

    for i in r(n):
        s[i].append(s[i][0] + s[i][1])

    id.S(s, 1, 2)

    ap = bp = 0
    for i, t in e(s):
        if i % 2 == 0:
            ap += t[0]
        else:
            bp += t[1]

    print(ap - bp)


main()

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
# import bisect
# import math
# import numpy as np


"""Template"""


class IP:
    """
    入力を取得するクラス
    """
    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        """
        1文字の取得に使います
        :return: int
        """
        return int(self.input())

    def S(self):
        """
        1文字の取得(str
        :return: str
        """
        return self.input()

    def IL(self):
        """
        1行を空白で区切りリストにします(int
        :return: リスト
        """
        return list(map(int, self.input().split()))

    def SL(self):
        """
        1行の文字列を空白区切りでリストにします
        :return: リスト
        """
        return list(map(str, self.input().split()))

    def ILS(self, n):
        """
        1列丸々取得します(int
        :param n: 行数
        :return: リスト
        """
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        """
        1列丸々取得します（str
        :param n: 行数
        :return: リスト
        """
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        """
        Some Int LineS
        横に複数、縦にも複数
        :param n: 行数
        :return: list
        """
        return [self.IL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        """
        Half enumeration
        半分全列挙です
        pの要素の和の組み合わせを作ります。
        ソート、重複削除行います
        :param p: list : 元となるリスト
        :return: list : 組み合わせられた和のリスト
        """
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        """
        bit_full_search2
        bit全探索の改良版
        全探索させたら2進数のリストと10進数のリストを返す
        :return: list２つ : １個目 2進数(16桁)  2個目 10進数
        """
        # 参考
        # https://blog.rossywhite.com/2018/08/06/bit-search/
        # https://atcoder.jp/contests/abc105/submissions/4088632
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    """右からj+1番目のiが1かどうか判定"""
                    # output.append(a[j])
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        """
        ソート関係行います。色々な設定あります。
        :param s: 元となるリスト
        :param r: reversするかどうか 0=False 1=True
        :param m: （2次元配列）何番目のインデックスのソートなのか
        :return: None
        """
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        """
        bit探索で使います。0以上のときにTrue出します
        自然数だからn
        :param a: int
        :param b: int
        :return: bool
        """
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        """
        bit探索で使います。1のときにTrue出すよ
        oneで1
        :param a: int
        :param b: int
        :return: bool
        """
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        """
        Round up
        小数点切り上げ割り算
        :param x: int
        :param y: int
        :return: int
        """
        return -(-x//y)

    def ave(self, a):
        """
        平均を求めます
        :param a: list
        :return: int
        """
        return sum(a) / len(a)


"""ここからメインコード"""


def main():
    # 1文字に省略
    r, e = range, enumerate
    ip = IP()
    id = Idea()

    """この下から書いてね"""
    n = ip.I()
    s = ip.SILS(n)

    for i in r(n):
        s[i].append(s[i][0] + s[i][1])

    id.S(s, 1, 2)

    print(sum(s[i][0] for i in r(0, n, 2)) - sum(s[i][1] for i in r(1, n, 2)))


main()

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
# import bisect
# import math
# import numpy as np


"""Template"""


class IP:
    """
    入力を取得するクラス
    """
    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        """
        1文字の取得に使います
        :return: int
        """
        return int(self.input())

    def S(self):
        """
        1文字の取得(str
        :return: str
        """
        return self.input()

    def IL(self):
        """
        1行を空白で区切りリストにします(int
        :return: リスト
        """
        return list(map(int, self.input().split()))

    def SL(self):
        """
        1行の文字列を空白区切りでリストにします
        :return: リスト
        """
        return list(map(str, self.input().split()))

    def ILS(self, n):
        """
        1列丸々取得します(int
        :param n: 行数
        :return: リスト
        """
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        """
        1列丸々取得します（str
        :param n: 行数
        :return: リスト
        """
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        """
        Some Int LineS
        横に複数、縦にも複数
        :param n: 行数
        :return: list
        """
        return [self.IL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        """
        Half enumeration
        半分全列挙です
        pの要素の和の組み合わせを作ります。
        ソート、重複削除行います
        :param p: list : 元となるリスト
        :return: list : 組み合わせられた和のリスト
        """
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        """
        bit_full_search2
        bit全探索の改良版
        全探索させたら2進数のリストと10進数のリストを返す
        :return: list２つ : １個目 2進数(16桁)  2個目 10進数
        """
        # 参考
        # https://blog.rossywhite.com/2018/08/06/bit-search/
        # https://atcoder.jp/contests/abc105/submissions/4088632
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    """右からj+1番目のiが1かどうか判定"""
                    # output.append(a[j])
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        """
        ソート関係行います。色々な設定あります。
        :param s: 元となるリスト
        :param r: reversするかどうか 0=False 1=True
        :param m: （2次元配列）何番目のインデックスのソートなのか
        :return: None
        """
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        """
        bit探索で使います。0以上のときにTrue出します
        自然数だからn
        :param a: int
        :param b: int
        :return: bool
        """
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        """
        bit探索で使います。1のときにTrue出すよ
        oneで1
        :param a: int
        :param b: int
        :return: bool
        """
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        """
        Round up
        小数点切り上げ割り算
        :param x: int
        :param y: int
        :return: int
        """
        return -(-x//y)

    def ave(self, a):
        """
        平均を求めます
        :param a: list
        :return: int
        """
        return sum(a) / len(a)


"""ここからメインコード"""


def main():
    # 1文字に省略
    r, e = range, enumerate
    ip = IP()
    id = Idea()

    """この下から書いてね"""
    n = ip.I()

    s = ip.SILS(n)

    for i in r(n):
        s[i].append(s[i][0] + s[i][1])

    id.S(s, 1, 2)

    ap = sum(s[i][0] for i in r(0, n, 2))
    bp = sum(s[i][1] for i in r(1, n, 2))

    print(ap - bp)


main()

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
# import bisect
# import math
# import numpy as np


"""Template"""


class IP:
    """
    入力を取得するクラス
    """
    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        """
        1文字の取得に使います
        :return: int
        """
        return int(self.input())

    def S(self):
        """
        1文字の取得(str
        :return: str
        """
        return self.input()

    def IL(self):
        """
        1行を空白で区切りリストにします(int
        :return: リスト
        """
        return list(map(int, self.input().split()))

    def SL(self):
        """
        1行の文字列を空白区切りでリストにします
        :return: リスト
        """
        return list(map(str, self.input().split()))

    def ILS(self, n):
        """
        1列丸々取得します(int
        :param n: 行数
        :return: リスト
        """
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        """
        1列丸々取得します（str
        :param n: 行数
        :return: リスト
        """
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        """
        Some Int LineS
        横に複数、縦にも複数
        :param n: 行数
        :return: list
        """
        return [self.IL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        """
        Half enumeration
        半分全列挙です
        pの要素の和の組み合わせを作ります。
        ソート、重複削除行います
        :param p: list : 元となるリスト
        :return: list : 組み合わせられた和のリスト
        """
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        """
        bit_full_search2
        bit全探索の改良版
        全探索させたら2進数のリストと10進数のリストを返す
        :return: list２つ : １個目 2進数(16桁)  2個目 10進数
        """
        # 参考
        # https://blog.rossywhite.com/2018/08/06/bit-search/
        # https://atcoder.jp/contests/abc105/submissions/4088632
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    """右からj+1番目のiが1かどうか判定"""
                    # output.append(a[j])
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        """
        ソート関係行います。色々な設定あります。
        :param s: 元となるリスト
        :param r: reversするかどうか 0=False 1=True
        :param m: （2次元配列）何番目のインデックスのソートなのか
        :return: None
        """
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        """
        bit探索で使います。0以上のときにTrue出します
        自然数だからn
        :param a: int
        :param b: int
        :return: bool
        """
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        """
        bit探索で使います。1のときにTrue出すよ
        oneで1
        :param a: int
        :param b: int
        :return: bool
        """
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        """
        Round up
        小数点切り上げ割り算
        :param x: int
        :param y: int
        :return: int
        """
        return -(-x//y)

    def ave(self, a):
        """
        平均を求めます
        :param a: list
        :return: int
        """
        return sum(a) / len(a)


"""ここからメインコード"""


def main():
    # 1文字に省略
    r, e = range, enumerate
    ip = IP()
    id = Idea()

    """この下から書いてね"""
    n = ip.I()

    s = ip.SILS(n)

    for i in r(n):
        s[i].append(s[i][0] + s[i][1])

    id.S(s, 1, 2)

    ap = bp = 0

    for i in r(n):
        if i % 2 == 0:
            ap += s[i][0]
        else:
            bp += s[i][1]

    print(ap - bp)


main()

Submission status: Accepted

Solution code:
print(-sum([v[k%2]*(k%2*2-1)for k,v in enumerate(sorted([list(map(int,input().split())) for _ in range(int(input()))],key=sum)[::-1])]))

Submission status: Accepted

Solution code:
N = int(input())
items = []
for _ in range(N):
    items.append(tuple(map(int, input().split())))

sum_list = []
for a, b in items:
    sum_list.append((a + b, a, b))

sum_list = sorted(sum_list, key=lambda x: -x[0])

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

Submission status: Accepted

Solution code:
print(-sum([v[k%2]*(k%2*2-1)for k,v in enumerate(sorted([[int(j) for j in input().split()] for i in range(int(input()))],key=lambda x:-x[0]-x[1]))]))

Submission status: Accepted

Solution code:
import sys
from operator import itemgetter


def r(n, ab):

    ss = [x + [x[0] + x[1]] for x in ab]
    ss.sort(key=itemgetter(2), reverse=True)
    
    scr = 0
    sente = True
    for s in ss:
        if sente:
            scr += s[0]
        else:
            scr -= s[1]
        sente = not sente

    return scr

def main():
    input = sys.stdin.readline
    n = int(input())
    ab = [list(map(int, input().split())) for _ in range(n)]
    print(r(n, ab))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
c = []
for i in range(n):#h:高さ
    c.append([int(m) for m in input().split()]+[i])
"""d = []
for i in range(n):#h:高さ
    d.append([c[i][1], c[i][0], i])"""
tans = 0
aans = 0
sumc = []

for i in range(n):
    sumc.append([c[i][0] + c[i][1], i])

sumc.sort(reverse=True)

for i in range(n):
    if i % 2 == 0:
        tans += c[sumc[i][1]][0]
    else:
        aans += c[sumc[i][1]][1]

print(tans -aans)

Submission status: Accepted

Solution code:
n = int(input())

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

d = sorted(c, key=lambda x: x[2], reverse=True)

ans = 0
for j in range(n//2):
    ans += d[j*2][0]
    ans -= d[j*2+1][1]

if n % 2 == 1:
    ans += d[n-1][0]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
# 仮に青木さん(B)が全て食べたとき、高橋(A)にとっては最悪な状況
# これを打破するためには、自分の得点と相手の得るはずだった得点の和を最大にするようなものについて食べれば良い
# 青木さんも同じことを考えるので、要は幸福度の和をソートして交互に食べていけば、互いに最善の行動を取ることとなる。
# 答えを出す際には、AがBから何点奪ったかだけを考えればよい。
# つまり、Aが最善で何点奪えるか　から　全てBさんが食べたとき　を引けばいい

sum_happiness = []
all_for_B = 0

for i in range(N):
    tmp1, tmp2 = list(map(int, input().split()))
    sum_happiness.append(tmp1 + tmp2)
    all_for_B += tmp2

sum_happiness.sort(reverse=True)


gain_A = 0
for i, h in enumerate(sum_happiness):
    if i % 2 == 0:
        gain_A += h
    # print(gain_A)


print(gain_A - all_for_B)

Submission status: Accepted

Solution code:
def dif(n,ab):
    c=[[sum(i),i] for i in ab]
    c.sort(reverse=True)

    a=[c[i][1][0] for i in range(n) if i%2==0]
    b=[c[i][1][1] for i in range(n) if i%2!=0]

    return sum(a)-sum(b)

n=int(input())
print(dif(n,[list(map(int,input().split())) for i in range(n)]))

Submission status: Accepted

Solution code:
n = int(input())
D = []
A = []
B = []
m = 1000000000
res = 0
for i in range(n):
    a,b = map(int,input().split())
    point = a+b
    D.append([point,i])
    A.append(a)
    B.append(b)
else:
    D.sort()
    D.reverse()

ans_a = 0
ans_c = 0
for i in range(n):
    if i%2 == 0:
        res += A[(D[i][1])]
        ans_a += A[(D[i][1])]
    else:
        res -= B[(D[i][1])]
        ans_c += B[(D[i][1])]
else:
    print(res)
    #print(ans_a-ans_c)

Submission status: Accepted

Solution code:
def dif(n,ab):
    c=[[sum(i),i] for i in ab]
    c.sort(reverse=True)

    ap=0
    bp=0
    for i in range(n):
        if i%2==0:
            ap+=c[i][1][0]
        else:
            bp+=c[i][1][1]


    return ap-bp

n=int(input())
print(dif(n,[list(map(int,input().split())) for i in range(n)]))

Submission status: Accepted

Solution code:
n = int(input())
D = []
A = []
B = []
m = 1000000000
res = 0
for i in range(n):
    a,b = map(int,input().split())
    point = a+b
    D.append([point,i])
    A.append(a)
    B.append(b)
else:
    D.sort()
    D.reverse()

ans_a = 0
ans_c = 0
for i in range(n):
    if i%2 == 0:
        res += A[(D[i][1])]
        ans_a += A[(D[i][1])]
    else:
        res -= B[(D[i][1])]
        ans_c += B[(D[i][1])]
else:
    #print(res)
    print(ans_a-ans_c)

Submission status: Accepted

Solution code:
N = int(raw_input())
L = []
for i in range(N):
  L.append(raw_input())
result = []
sumA = 0
sumB = 0
for i in range(N):
    L[i] = L[i].split()
    result.append([int(L[i][0]) + int(L[i][1]), i])
result.sort()
for i in range(N-1, -1, -2):
    index = result[i][1]
    sumA += int(L[index][0])
for i in range(N-2, -1, -2):
    index = result[i][1]
    sumB += int(L[index][1])
result_K = sumA - sumB
print(result_K)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
 
def main():
    N = int(input())
    AB = [tuple(map(int, input().split())) for _ in [0] * N]
 
    dish = sorted(AB, reverse=1, key=lambda x: sum(x))
    taka, aoki = 0, 0
    for i in range(N):
        aoki += dish[i][1]
        if not i % 2:
            taka += sum(dish[i])
    print(taka - aoki)
 
input = lambda: stdin.readline()
main()

Submission status: Accepted

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

def io_generator():
	while True:
		yield raw_input()

#+++++++++++++++++++

def main(io):
	a=int(io.next())
	diff=[]
	for i in range(a):
		b,c= map(int, io.next().split())
		diff.append((b,c,b+c))
	diff = sorted(diff, key=lambda a:-a[2])
	#print diff
	ret= sum([x[i%2]*(1-2*(i%2)) for i,x in enumerate(diff)])
	
	return ret


#++++++++++++++++++++

if __name__ == "__main__":
	io=io_generator()
	print main(io)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())

A = [0 for i in range(N)]
B = [0 for i in range(N)]
C = [0 for i in range(N)]

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

C.sort(reverse=True)

ans = 0

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

print(ans - sum(B))

Submission status: Accepted

Solution code:
n = int(input())
ab = []
b = []
for _ in range(n):
    tmp = list(map(int, input().split()))
    b.append(tmp[1])
    ab.append(sum(tmp))
ab.sort(reverse=True)
print(sum(ab[::2]) - sum(b))

Submission status: Accepted

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

H = sorted(H, reverse = True)
sum_c = 0
sum_a = 0

for i in range(n):
    if i % 2 == 0:
        sum_c += H[i][1]
    else:
        sum_a += H[i][2]

ans = sum_c - sum_a
print(ans)

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, B = [], []
for _ in range(N):
    a, b = map(int, input().split())
    A.append(a + b)
    B.append(b)

A.sort(reverse=True)
print(sum([ab for i, ab in enumerate(A) if i % 2 == 0]) - sum(B))

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])
    
ab.sort(reverse=True)
suma = 0
sumb = 0
for i in range(n):
    if i % 2 == 0:
        suma += ab[i][1]
    else:
        sumb += ab[i][2]
print(suma-sumb)

Submission status: Accepted

Solution code:
N = int(input())
info = []
for i in range(N):
    a,b = map(int,input().split())
    d = a + b
    info.append([d,a,b])
info = sorted(info,reverse=True)
score = 0
for i in range(N):
    if i % 2 == 0:
        score += info[i][1]
    elif i % 2 == 1:
        score -= info[i][2]
print(score)

Submission status: Accepted

Solution code:
n = int(input())
#z = [[int(x) for x in input().split()] for i in range(n)]
z = []
t = 0
for i in range(n):
    x, y = [int(x) for x in input().split()]
    t += y
    z.append(x + y)
z.sort(reverse=True)

s = 0
for i in range(n // 2):
    s += z[2 * i]
if n % 2 == 1:
    s += z[len(z) - 1]

print(s - t)

#アルゴリズムが間違っている

Submission status: Accepted

Solution code:
def main() -> None:
    n = int(input())
    menu = [[int(x) for x in input().split()] for _ in range(n)]

    for i, _ in enumerate(menu):
        menu[i] += [sum(menu[i])]

    menu.sort(key=lambda x: x[len(x) - 1], reverse=True)

    cnt = 0
    for i in range(n):
        if i % 2 == 0:
            cnt += menu[i][0]
        else:
            cnt -= menu[i][1]

    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Accepted

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