Advertisement
jusohatam

Untitled

Aug 15th, 2020
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. from functools import cmp_to_key
  2.  
  3.  
  4. def compare(x, y):
  5.     if x[1] < y[1]:
  6.         return 1
  7.     elif x[1] > y[1]:
  8.         return -1
  9.     else:
  10.         if x[2] < y[2]:
  11.             return -1
  12.         elif x[2] > y[2]:
  13.             return 1
  14.         else:
  15.             if x[0] < y[0]:
  16.                 return -1
  17.             elif x[0] > y[0]:
  18.                 return 1
  19.             else:
  20.                 return 0
  21.  
  22.  
  23. def solution(w, n, arr):
  24.     res = []
  25.     arr.sort(key=cmp_to_key(compare))
  26.     i = 0
  27.     while i < n and w:
  28.         if arr[i][2] <= w:
  29.             res.append(arr[i][0])
  30.             w -= arr[i][2]
  31.         i += 1
  32.     res.sort()
  33.     return res
  34.  
  35.  
  36. w = int(input())
  37. n = int(input())
  38. arr = [tuple(map(int, '{} {}'.format(i, input()).split())) for i in range(n)]
  39. print(*solution(w, n, arr))
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement