import java.io.*; import java.util.*; import java.awt.geom.*; /** * Solution to Excellence * * @author vanb */ public class excellence_vanb { public Scanner sc; public PrintStream ps; /** * Driver. * @throws Exception */ public void doit() throws Exception { sc = new Scanner( System.in ); ps = System.out;; int n = sc.nextInt(); int students[] = new int[n]; for( int i=0; i0. // Let y be the worst. The second worst is y+b, b>0. // Here's the order: // x // x-a // y+b // y // our answer is min( x+y, (x-a)+(y+b) ). // // Suppose we paired them differently: x with y+b, y with x-a // Then our answer would be min( x+y+b, x+y-a ) = x+y-a (since a>0, b>0, so b>-a) // but since a>0 and b>0, x+y-a has to be worse than x+y, // and it also has to be worse than x+y-a+b. // // Any ordering you can come up with can be obtained from the claimed optimal solution // by a series of such swaps, each one making the answer worse. So, the claimed optimal // solution is, indeed, optimal. Arrays.sort( students ); int minimum = Integer.MAX_VALUE; for( int i=0; i