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

Please 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
Posted by Oleg Baskakov 22 Aug 2016 22:34
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
Posted by Oleg Baskakov 23 Aug 2016 10:44
Uhh... the algo is greedy, not you https://en.wikipedia.org/wiki/Greedy_algorithm and it doesn't work for this task, so try bruteforce maybe; though i see you got ac already anyway.
Re: Please anybody help me. Wrong answer. Java
Posted by Sergey 23 Aug 2016 14:21
sorry. I misunderstood