Last active
August 29, 2015 14:06
-
-
Save shvechikov/f31d1599bc197529d897 to your computer and use it in GitHub Desktop.
This file contains 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
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