class Solution:
def twoCitySchedCost(self, costs: List[List[int]]) -> int:
N = len(costs) // 2
i, j = 0, 0
answer = 0
for a, b in sorted(costs, key = lambda x : abs(x[0] - x[1]), reverse = True):
if i == N:
answer += b
j += 1
elif j == N:
answer += a
i += 1
else:
if a < b:
answer += a
i += 1
else:
answer += b
j += 1
return answer