I’ve wrriten up a solution to double helix https://www.spoj.com/problems/ANARC05B/
My code is this:
from collections import defaultdict
def get_prefix_sum(a):
prefix_sum = defaultdict(int)
for i in range(len(a)):
prefix_sum[i] += a[i] + prefix_sum[i-1]
return prefix_sum
def biggest_sum(a, b):
common = []
seen = set()
index_of_a = {v: i for i, v in enumerate(a)}
index_of_b = {v: i for i, v in enumerate(b)}
for num in a:
seen.add(num)
for num in b:
if num in seen:
common.append(num)
prefix_sum_a = get_prefix_sum(a)
prefix_sum_b = get_prefix_sum(b)
mx = 0
for i, com in enumerate(common):
i_first = index_of_a[com]
j_first = index_of_b[com]
if i > 0:
i_prev = index_of_a[common[i-1]]
j_prev = index_of_b[common[i-1]]
mx += max(prefix_sum_a[i_first] - prefix_sum_a[i_prev],
prefix_sum_b[j_first]-prefix_sum_b[j_prev])
else:
mx += max(prefix_sum_a[i_first], prefix_sum_b[j_first])
if common == []:
mx += max(sum(a), sum(b))
else:
last_common = common[-1]
i_last = index_of_a[last_common]
j_last = index_of_b[last_common]
mx += max(sum(a[i_last+1:]), sum(b[j_last+1:]))
return mx
def main():
while True:
a = input()
if a == "0":
break
a = [int(i) for i in a.split()]
b = [int(i) for i in input().split()]
ans = biggest_sum(a[1:], b[1:])
print(ans)
if __name__ == "__main__":
main()
I’m not sure why I’m getting a runtime error. Is it because of my algorithmic logic, or is it because of how I’m parsing IO?
I suspect something’s wrong with IO; I had my function simply return 10
and I got the same NZEC error.
Could someone please explain to me how to fix this?