Skip to content

Instantly share code, notes, and snippets.

@shvechikov
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shvechikov/f31d1599bc197529d897 to your computer and use it in GitHub Desktop.
Save shvechikov/f31d1599bc197529d897 to your computer and use it in GitHub Desktop.
def find(diamonds, share, shift=0, skip=()):
for i in range(shift, len(diamonds)):
if i in skip:
continue
d = diamonds[i]
if d > share:
continue
if d == share:
return (i,)
else:
result = find(diamonds, share - d, shift=i+1, skip=skip)
if result:
return (i,) + result
return None
def test(diamonds):
diamonds.sort(reverse=True)
share, mod = divmod(sum(diamonds), 4)
if mod:
return False
skip = ()
for step in range(1, 5):
if step == 4:
rest_indexes = set(range(len(diamonds))) - set(skip)
rest_diamonds = [diamonds[i] for i in rest_indexes]
if sum(rest_diamonds) == share:
return True
return False
indexes = find(diamonds, share=share, skip=skip)
if indexes:
found = [diamonds[i] for i in indexes]
skip += tuple(indexes)
else:
return False
return False
with open('input.txt') as inp, open('output.txt', 'wt') as out:
days_count, diamonds_count = map(int, inp.readline().split())
for _ in range(days_count):
line = inp.readline()
parts = line.split()
integers = map(int, parts)
diamonds = list(integers)
if not diamonds:
continue
result = test(diamonds)
print('YES' if result else 'NO', file=out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment