Created
March 4, 2022 11:54
-
-
Save theabbie/a564172bb0eab4bd8d11ff726ab1c7df to your computer and use it in GitHub Desktop.
Dcoder Cashback Question Incorrect Approach
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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