-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy path02 - Luck Balance.py
46 lines (31 loc) · 1.09 KB
/
02 - Luck Balance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# ========================
# Information
# ========================
# Direct Link: https://www.hackerrank.com/challenges/luck-balance/problem
# Difficulty: Easy
# Max Score: 20
# Language: Python
# ========================
# Solution
# ========================
import os
# Complete the luckBalance function below.
def luckBalance(k, contests):
con = contests
con = sorted(con, reverse=True, key=lambda con: con[con[1] == 0])
con_1 = [imp[0] for i, imp in enumerate(sorted(con)) if imp[1] == 0]
con_2 = [imp[0] for j, imp in enumerate(sorted(con[0:k], reverse=True)) if imp[1] == 1]
con_3 = [imp[0] for l, imp in enumerate(sorted(con[k:], reverse=True)) if imp[1] == 1]
max_luck = sum(con_1)+sum(con_2)-sum(con_3)
return max_luck
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
nk = input().split()
n = int(nk[0])
k = int(nk[1])
CONTESTS = []
for _ in range(n):
CONTESTS.append(list(map(int, input().rstrip().split())))
RESULT = luckBalance(k, CONTESTS)
fptr.write(str(RESULT) + '\n')
fptr.close()