eolymp
bolt
Try our new interface for solving problems

Cards

Adam has a fancy for numbers. Once he found a batch of empty paper cards in his drawer, wrote random numbers on both sides of each card and thought of the following puzzle: what smallest possible value can be obtained by putting all cards in an arbitrary order (and upturned if necessary) to the expression of the following form: \includegraphics{https://static.e-olymp.com/content/32/325814e6db7a84e2e9c53f33f0285cfda814fe1f.jpg} After a while Adam came up with a solution. Could you do that too? Write a program to solve the puzzle described above. \InputFile The first line contains the number of cards \textbf{N} (\textbf{2} ≤ \textbf{N} ≤ \textbf{100000}, \textbf{N} is an even integer). Each of the following \textbf{N} lines contains two integers \textbf{a_i} and \textbf{b_i} (\textbf{-2000} ≤ \textbf{a_i}, \textbf{b_i} ≤ \textbf{2000}; \textbf{i} = \textbf{1..N}). These are the numbers written on separate sides of the \textbf{i}-th card. \OutputFile The first and the only line should contain the minimal value that can be obtained by putting all the cards to the expression as described above. \textbf{Comment} 1: Cards are put to the expression in this order: \textbf{1}^st, \textbf{2}^nd, \textbf{3}^rd, \textbf{5}^th, \textbf{4}^th, \textbf{6}^th. \textbf{(-8) - 5 + (-3) - 7 + (-7) - 4 = -34} 2: Cards are put to the expression in this order: \textbf{2}^nd, \textbf{1}^st, \textbf{4}^th, \textbf{3}^rd, \textbf{5}^th, \textbf{8}^th, \textbf{6}^th, \textbf{9}^th, \textbf{7}^th, \textbf{10}^th. \textbf{62 - 70 + 59 - 81 + 40 -- 76 + 35 -- 85 + 57 - 96 = -155}
Time limit 1 second
Memory limit 64 MiB
Input example #1
6
-8 12
0 5
7 -3
10 -7
-2 7
1 4
Output example #1
-34
Source BOI-2005