Video Solution
By David Li
Video Solution Code
Explanation
For the tandem kayaks, we want to minimize the difference between the people in each kayak. To do this, we can sort the weights of the people and place adjacent people into the same kayak.
For the two single kayaks, we can remove every possible pair of individuals and compute the differences in the tandem kayaks.
Implementation
Time Complexity:
n = int(input()) * 2people = sorted(int(i) for i in input().split())assert len(people) == nmin_instability = float("inf")for i in range(n):for j in range(i + 1, n):new_people = [people[p] for p in range(n) if p != i and p != j]total_instability = 0for p in range(0, n - 2, 2):total_instability += new_people[p + 1] - new_people[p]min_instability = min(min_instability, total_instability)print(min_instability)
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!