def counts(nums: list[int]) -> dict[int, int]: ret = {} for n in nums: if n in ret: ret[n] += 1 else: ret[n] = 1 return ret if __name__ == '__main__': with open('1.input') as file: left, right = [], [] for line in file: l, r = line.split() l, r = int(l), int(r) left.append(l) right.append(r) # how many times does each number apear in each list right = counts(right) score = sum([0 if n in right else n * right[n] for n in left]) for n in left: if n not in right: continue score += n * right[n] print(score)