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

WA 5
Posted by Vadim Kostenko 26 Feb 2016 03:49
I don't know, why answer is wrong. Suggest me a solution. On the other side, all checking that I had done was success.
My java code :

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class task1005 {
    private static Scanner scanner;
    private static List<Long> mainHeap;
    public static void main(String[] args) {
        getData();
        balancedTwoHeap();
    }

    private static void getData() {
        scanner = new Scanner(System.in);
        mainHeap = new ArrayList<>();
        int stones = scanner.nextInt();
        for (int i = 0; i < stones; i++) {
            mainHeap.add(scanner.nextLong());
        }
    }

    private static void balancedTwoHeap() {
        Long heap1 = 0L;
        Long heap2 = 0L;
        while (!mainHeap.isEmpty()) {
            Long max = mainHeap.stream().max(Long::compareTo).get();
            if (heap1 > heap2) {
                heap2 += max;
            } else {
                heap1 += max;
            }
            mainHeap.remove(max);
        }
        System.out.println(Math.abs(heap1 - heap2));
    }
}
Re: WA 5
Posted by itm94lj 8 Jul 2016 15:00
i meet the same situation.assume the input weight is 5 4 3 3 3.
sort the input weight and arrange in order is not the suitable solution.
Re: WA 5
Posted by Chernobuk 19 Jul 2016 22:35
5
5 5 4 3 3
должно вернуть 0
5+5 = 0
4+3+3 = 0
Edited by author 20.07.2016 00:14

Edited by author 20.07.2016 00:15
Re: WA 5
Posted by Hopeless 11 Dec 2019 18:17
But... the ans is 2, isn't it?
I have the same solution(arrange in order) and it return 2 from this test(5 4 3 3 3)...
So, this test don't show that this way is wrong.

Edited by author 11.12.2019 18:17

Edited by author 11.12.2019 18:18