ACM: Problem 7372

Problem 7372 Excellence


/*
Problem 7372

Sample:
input:
4
1
2
3
5
2
18
16
4
13
12
19
14


output:
5
34
27
*/


import java.io.*;
import java.util.*;

class Main
{
    static String ReadLn (int maxLg)  // utility function to read from stdin
    {
        byte lin[] = new byte [maxLg];
        int lg = 0, car = -1;
        String line = "";

        try
        {
            while (lg < maxLg)
            {
                car = System.in.read();
                if ((car < 0) || (car == '\n')) break;
                lin [lg++] += car;
            }
        }
        catch (IOException e)
        {
            return (null);
        }

        if ((car < 0) && (lg == 0)) return (null);  // eof
        return (new String (lin, 0, lg));
    }

    public static void main (String args[])  // entry point from OS
    {
        Main myWork = new Main();  // create a dinamic instance
        myWork.Begin();            // the true entry point
    }

	void Begin()
	{
		String input;
		StringTokenizer idata;
		int contestants = 0;
		int cities=0;
		int count = 0;
		
		while ((input = Main.ReadLn (255)) != null)
		{
			idata = new StringTokenizer (input);	
			
			if(!idata.hasMoreElements())//handle blank line at the end of parsing
				break;
					
			contestants = Integer.parseInt (idata.nextToken()); 
			int[] arr = new int[contestants];
               	
			for(int i = 0; i < contestants; i++){
				input = Main.ReadLn (255);
				if(input.indexOf("\r") != -1)
					arr[i] = Integer.parseInt (input.substring(0, input.indexOf("\r")));
				else
					arr[i] = Integer.parseInt (input);
			}
			Arrays.sort(arr);
			
			int low = 10000000;
			for(int i = 0; i < contestants; i++){
				
				if(arr[i] + arr[contestants-1-i] < low)
					low = arr[i] + arr[contestants-1-i];
			}
			System.out.println(low);
			
		}
	}
}

Previous Post

Next Post

Leave a Reply