|
|
back to boardPlease anybody help me. Wrong answer. Java Posted by Sergey 22 Aug 2016 22:13 import java.util.*; public class StonesPile { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int[] pile = new int[a]; for (int i = 0; i < pile.length; i++) { pile[i] = sc.nextInt(); } Arrays.sort(pile); int pile1 = pile[pile.length - 1]; int pile2 = 0; for (int i = pile.length - 2; i >= 0; i--){ if (pile1 <= pile2){ pile1 += pile[i]; } else{ pile2 += pile[i]; } } System.out.println(Math.abs(pile1 - pile2)); } } Re: Please anybody help me. Wrong answer. Java Greedy is wrong, which you could find out by yourself by reading just a few threads below yours. A simple counter test is 5 5 5 4 3 3 (5+5=4+3+3, so the difference is 0) Re: Please anybody help me. Wrong answer. Java Posted by Sergey 23 Aug 2016 02:40 If you can help write, if not don't write and i am not greedy. USA Re: Please anybody help me. Wrong answer. Java Re: Please anybody help me. Wrong answer. Java Posted by Sergey 23 Aug 2016 14:21 sorry. I misunderstood |
|
|