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 my prog Crash?
Posted by Georgeek 30 Jun 2011 16:02
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;


public class PileOfStones {
    private static int sum = 0;
    private static int minSum = 0;
    private static int myMinSum;
    private static int min;
    private static int m=0;
    private static int error = 0;

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int pl = Integer.parseInt(in.readLine());
        if(pl>=1 && pl<=20){
            int[] stones = new int[pl];
            int k = stones.length-1;
            Scanner sc1=new Scanner(System.in);
            for (int i = 0; i < pl; i++) {
                stones[i] = sc1.nextInt();
                if(stones[i]<1 || stones[i]>100000){
                    error =1;
                    break;
                }
            }
            if(error!=1){
                for (int i = 0; i < stones.length; i++) {
                    sum+=stones[i];
                }
                Arrays.sort(stones);
                min=sum;
                while(k>0){
                    for (int i = k; i >= 0; i--) {
                        if((stones[i] + minSum)<=(sum/2)){
                            minSum+=stones[i];
                            k=i;
                        }
                    }
                    if((sum/2-minSum)<=min ){
                        myMinSum=minSum;
                    }
                    minSum-=stones[k];
                    if(k!=m){
                        m=k;
                    }
                    else{
                        k=-1;
                    }
                }
                System.out.println(sum-2*myMinSum);
            }
            }

        }


}