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:
_ - _ + _ - _ + _ - _ + ... - _
After a while Adam came up with a solution. Could you do that too? Write a program to solve the puzzle described above.
6
-8 12
0 5
7 -3
10 -7
-2 7
1 4Output:
-34Cards are put to the expression in this order: 1st, 2nd, 3rd, 5th, 4th, 6th.
(-8) - 5 + (-3) - 7 + (-7) - 4 = -34
10
70 70
62 73
81 65
59 77
99 40
35 88
80 57
76 67
85 57
53 96Output:
-155Cards are put to the expression in this order: 2nd, 1st, 4th, 3rd, 5th, 8th, 6th, 9th, 7th, 10th.
62 - 70 + 59 - 81 + 40 – 76 + 35 – 85 + 57 - 96 = -155
Submit solution
Coming laterThe grading service will be connected in a later migration step. You can inspect the task and your previous results now.