Problem :

https://www.hackerrank.com/challenges/luck-balance/problem


My Solution :

#!/usr/bin/env python3


def luckBalance(k, contests):
    T0, T1 = [], []
    for L, T in contests:
        if T == 1:
            T1.append(L)
        else:
            T0.append(L)
    T1.sort(reverse=True)
    return sum(T1[:k]) - sum(T1[k:]) + sum(T0)


n, k = map(int, input().split())
contests = []
for _ in range(n):
    contests.append(list(map(int, input().rstrip().split())))
result = luckBalance(k, contests)
print(result)