Cards
Time limit1sMemory limit128 MB
Given N double-sided cards, choose an order, a flip for each, and assign alternating plus/minus signs to minimize the resulting alternating sum.
Problem
Adam loves numbers. One day he found a stack of blank cards in his drawer, wrote one number on each of the two sides of every card, and came up with the following puzzle.
He lays all the cards in a row in any order he likes, and may turn any card over so that its other side faces up. Reading the up-facing numbers from left to right, call them . Adam then evaluates the alternating sum
Because the number of cards is even, the plus and minus signs split exactly in half. Adam wants to make this value as small as possible. Write a program that finds the smallest value he can obtain.
Input
The first line contains the number of cards (, and is even). Each of the next lines contains two integers and (), the numbers written on the two sides of the -th card.
Output
Print a single integer: the smallest value of the alternating sum that can be obtained by ordering and flipping the cards.