Problem
tandemSolution
Although the solution to this problem seems difficult at first it is very trivial. We have two different problems we could be asked to solve: 1. Find the min speed. 2. Find the max speed. The approach to each of them is very similar. If we are asked to find the min speed we should attempt to eliminate as many large numbers as possible. If we asked to find the max speed then we should maximize the large numbers. To solve problem 1 we need to match the largest speeds from town a with the largest speeds from town b. To solve problem 2 we need to match the largest speeds from town a with the smallest speeds from town b. This is precisely what my solution below does.
Code
q = int(input()) n = int(input()) dmoj = list(map(lambda x: int(x), input().split(" "))) peg = list(map(lambda x: int(x), input().split(" "))) speed = 0 dmoj.sort() # min - max peg.sort() # min - max if q == 1: # min total speed for i in range(n): speed += max(dmoj[i], peg[i]) else: # max total speed dmoj = dmoj[::-1] for i in range(n): speed += max(dmoj[i], peg[i]) print(speed)