fork download
  1. class CandyStealer:
  2.  
  3. def __init__(self):
  4. # erase prev output file
  5. open('out', 'w').close()
  6.  
  7. @staticmethod
  8. def get_next_case():
  9. with open("in", "r") as fp:
  10. in_data = fp.read()
  11. # get rid of excess data
  12. in_data = in_data.strip()
  13. in_data = in_data.split("\n")[1:]
  14. for i in range(len(in_data)):
  15. if i % 2 != 0:
  16. t_case = in_data[i].split(" ")
  17. t_case = [int(x) for x in t_case]
  18. yield t_case
  19.  
  20. @staticmethod
  21. def sasha_add(n1, n2):
  22. n1 = bin(int(n1))[2:]
  23. n2 = bin(int(n2))[2:]
  24. return int(n1, 2) ^ int(n2, 2)
  25.  
  26. def get_sasha_heap_sum(self, heap):
  27. heap_sum = 0
  28. for num in heap:
  29. heap_sum = self.sasha_add(heap_sum, num)
  30. return heap_sum
  31.  
  32. @staticmethod
  33. def write_result(max_sum, case_num):
  34. with open("out", "a") as fp:
  35. addition = "NO" if max_sum == -1 else str(max_sum)
  36. fp.write("Case #" + str(case_num) + ": " + addition + "\n")
  37.  
  38. @staticmethod
  39. def get_heaps_by_mask(t_case, mask):
  40. heap1 = []
  41. heap2 = []
  42. for i in range(len(mask)):
  43. if mask[i] == "0":
  44. heap1.append(int(t_case[i]))
  45. else:
  46. heap2.append(int(t_case[i]))
  47. return heap1, heap2
  48.  
  49. def run(self):
  50. case_num = 0
  51. for t_case in self.get_next_case():
  52. case_num += 1
  53. combinations_count = (2 ** (len(t_case) - 1)) # all possible heaps
  54. default_mask = ["0" * len(t_case)]
  55. default_mask = "".join(default_mask)
  56. max_gain = -1
  57. for i in range(1, combinations_count):
  58. mask = bin(i)[2:]
  59. mask = default_mask[:-len(mask)] + mask
  60. heap1, heap2 = self.get_heaps_by_mask(t_case, mask)
  61. if self.get_sasha_heap_sum(heap1) == self.get_sasha_heap_sum(heap2):
  62. current_gain = max(sum(heap1), sum(heap2))
  63. if current_gain > max_gain:
  64. max_gain = current_gain
  65. self.write_result(max_gain, case_num)
  66.  
  67.  
  68. if __name__ == "__main__":
  69. CandyStealer().run()
Runtime error #stdin #stdout #stderr 0.01s 27712KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 69, in <module>
  File "./prog.py", line 5, in __init__
PermissionError: [Errno 13] Permission denied: 'out'