All problems

Problem 253

Two City Scheduling

Medium · 20 pts · Array, Greedy, Sorting

Problem

A company is interviewing 2N people and must fly exactly N of them to city A and the other N to city B. Flying person i to city A costs A[i], and to city B costs B[i]. Find the minimum total cost. Sort the people by A[i] - B[i], how much cheaper (or more expensive) city A is for them compared with city B. Send the first N people in this order to city A and the rest to city B.

Input

First line: N. Next 2N lines: A[i] and B[i].

Output

Print the minimum total cost.

Constraints

1 <= N <= 50; 1 <= A[i], B[i] <= 1000

Example input

2
10 20
30 200
400 50
30 20

Example output

110

Explanation

The differences A - B are -10, -170, 350 and 10. The two smallest belong to the first two people, who go to A for 10 + 30. The other two go to B for 50 + 20. The total is 40 + 70 = 110.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Minimum Cost to Connect Ropes Next: Largest Number