import java.util.*;
/**
* Created to solve http://c...content-available-to-author-only...e.com/q/23461/16293
* and submitted as http://c...content-available-to-author-only...e.com/a/23467/16293
*/
class EqualSums {
public static void main
(String[] args
) { final Scanner s
= new Scanner
(System.
in); // Read number of elements to divide
final int count = s.nextInt();
if (count % 2 == 1) {
}
// Read the elements to divide
final SortedList valueStack = new SortedList(count);
for (int i = 0; i < count; i++) {
valueStack.add(s.nextLong());
}
final SortedList targetOne = new SortedList(count / 2);
final SortedList targetTwo = new SortedList(count / 2);
// Divide elements into two groups
addInPairs(targetOne, targetTwo, valueStack);
// Try to ensure groups have equal value
retaliate(targetOne, targetTwo);
// Output result
System.
out.
println(targetOne
); System.
out.
println(targetTwo
); System.
out.
println("diff=" + Math.
abs(targetOne.
getSum() - targetTwo.
getSum())); }
private static void addInPairs(SortedList targetOne, SortedList targetTwo, SortedList valueStack) {
SortedList smallerTarget = targetOne;
SortedList biggerTarget = targetTwo;
while (!valueStack.isEmpty()) {
// Add biggest remaining value to small target
smallerTarget.add(valueStack.removeLast());
// Add second biggest remaining value to big target
biggerTarget.add(valueStack.removeLast());
// Flip targets if roles have changed
if (smallerTarget.getSum() > biggerTarget.getSum()) {
final SortedList temp = smallerTarget;
smallerTarget = biggerTarget;
biggerTarget = temp;
}
}
}
private static void retaliate(SortedList targetOne, SortedList targetTwo) {
long difference;
boolean changed;
outer:
do {
difference
= Math.
abs(targetOne.
getSum() - targetTwo.
getSum()); if (difference == 0) {
return;
}
changed = false;
// Try to find two values, that reduce the difference by changing them between targets
for (Long valueOne
: targetOne
) { for (Long valueTwo
: targetTwo
) { final Long tempOne
= targetOne.
getSum() + valueTwo
- valueOne
; final Long tempTwo
= targetTwo.
getSum() - valueTwo
+ valueOne
; if (Math.
abs(tempOne
- tempTwo
) < difference
) { targetOne.remove(valueOne);
targetTwo.add(valueOne);
targetTwo.remove(valueTwo);
targetOne.add(valueTwo);
changed = true;
continue outer;
}
}
}
} while (changed);
}
public static class SortedList extends AbstractList<Long> {
private final ArrayList<Long> list;
private long sum = 0;
public SortedList(int count) {
list = new ArrayList<>(count);
}
// the next functions access list-field directly
@Override
public Long get
(int index
) { return list.get(index);
}
@Override
public boolean add
(final Long t
) { if (i < 0) {
// No equal element present
list.add(-i - 1, t);
} else {
list.add(afterLastEqual(i, t), t);
}
sum += t;
return true;
}
@Override
public Long remove
(int index
) { final Long old
= list.
remove(index
); sum -= old;
return old;
}
@Override
public int size() {
return list.size();
}
// the next functions access list-field only through the functions above this point
// to ensure the sum is well kept
public long getSum() {
return sum;
}
private int afterLastEqual
(final int start,
Object o
) { int found = start;
while (found < size() && o.equals(get(found))) {
found++;
}
return found;
}
private int beforeFirstEqual
(final int start,
final Object o
) { int found = start;
while (found >= 0 && o.equals(get(found))) {
found--;
}
return found;
}
@Override
public int indexOf
(Object o
) { try {
if (i >= 0) {
return beforeFirstEqual(i, o) + 1;
}
// Object was not instance of Long
}
return -1;
}
@Override
public int lastIndexOf
(Object o
) { try {
if (i >= 0) {
return afterLastEqual(i, o) - 1;
}
// Object was not instance of Long
}
return -1;
}
@Override
public boolean remove
(Object o
) { if (o == null) {
return false;
}
final int i = indexOf(o);
if (i >= 0) {
remove(i);
return true;
}
return false;
}
public Long removeLast
() { return remove(size() - 1);
}
public Long removeFirst
() { return remove(0);
}
@Override
Iterator<Long> it = iterator();
if (!it.hasNext()) {
return "";
}
StringBuilder sb = new StringBuilder();
for (; ; ) {
sb.append(e);
if (!it.hasNext()) {
return sb.toString();
}
sb.append(' ');
}
}
}
}