|
|
back to boardWhy my prog Crash? 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); } }
}
} |
|
|