Skip to content

Instantly share code, notes, and snippets.

@theabbie
Created March 4, 2022 11:54
Show Gist options
  • Save theabbie/a564172bb0eab4bd8d11ff726ab1c7df to your computer and use it in GitHub Desktop.
Save theabbie/a564172bb0eab4bd8d11ff726ab1c7df to your computer and use it in GitHub Desktop.
Dcoder Cashback Question Incorrect Approach
from functools import cmp_to_key
n = int(input())
costs = []
for _ in range(n):
c, x = input().split()
costs.append((int(c), int(x)))
def getCost(seq):
curr = 0
debt = 0
for c, x in seq:
curr -= c
if curr < 0:
debt -= curr
curr = 0
curr += x - curr
return debt
def cmp(p1, p2):
c1, x1 = p1
c2, x2 = p2
f = c1 + max(c2 - x1, 0)
s = c2 + max(c1 - x2, 0)
if f < s:
return -1
elif f > s:
return 1
return -1 if x1 - c1 >= x2 - c2 else 1
costs.sort(key = cmp_to_key(cmp))
print(getCost(costs))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment