Help Cupid
InterviewTime limit3sMemory limit256 MB
Given N time zones, split everyone into pairs to minimize the total circular hour difference.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Sorting
- Solved
- No attempts yet
Problem
Cupid's workload keeps growing, so he is bringing in new technology. He put the best programmers on his staff on a project called Advanced Couples Matching (ACM). The project needs an algorithm that takes an even number of single people and splits them into couples, so that every person belongs to exactly one couple.
The data available about each person is thin. Gender, ethnicity, age and nationality are not sensible criteria for forming couples, so the programmers may use only data about each candidate's internet connection. For this stage they settled on time zones. People who live in nearby time zones find it easier to be online at the same moment, so the programmers decided to build the couples so that the total time difference is as small as possible.
A time zone is an integer from to that gives its offset in hours from Coordinated Universal Time (UTC). For two people living in time zones and , the time difference is the smaller of and . Given a partition of the candidates into couples, its total time difference is the sum of the time difference of every couple.
Write a program that reads the time zones of candidates and prints the minimum total time difference over all partitions of the candidates into couples.
Input
The first line contains an even integer (), the number of candidates to be coupled. The second line contains integers (), the time zones of the candidates.
Output
Print one line with an integer, the minimum total time difference over all partitions of the candidates into couples.