ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1005. Stone Pile

Why crash?
Posted by Alex 10 Oct 2011 19:58
import java.util.*;
public class main
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int arr[] = new int[n];
        int sum;
        int sum1 = 0;
        int sum2 = 0;
        for (int i=0; i<n; i++)
        {
            arr[i] = in.nextInt();
        }
        Arrays.sort(arr);
        sum1 = arr[n-1];
        sum2 = arr[n-2];
        for (int i=n-3; i >= 0; i--)
        {
            if (sum1 > sum2) sum2 += arr[i];
            else sum1 += arr[i];
        }
        if (sum1 > sum2) sum = sum1 - sum2;
        else sum = sum2 - sum1;
        System.out.println(sum);
    }
}