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

Test 3#
Posted by Giuliana 23 Jan 2012 09:24
Why my code is wrong? Help me please!!


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n, total = 0, soma = 0, diferenca = 100001, pilha2 = 0;
        int weights[] = new int[20];
        n = scan.nextInt();
        if (n == 1) {
            System.out.println(scan.next());
            return;
        }
        if (n == 2) {
            System.out.println(Math.abs(scan.nextInt() - scan.nextInt()));
            return;
        }
        for (int i = 0; i < n; i++) {
            weights[i] = scan.nextInt();
            total += weights[i];
        }
        for (int i = 0; i < n; i++) {
            soma = weights[i];
            for (int j = 0; j < n; j++) {
                pilha2 = 0;
                if (i != j) {
                    soma += weights[j];
                    pilha2 = total - soma;
                    if (diferenca > (Math.abs(pilha2 - soma))) {
                        diferenca = (Math.abs(pilha2 - soma));
                    }
                }
            }
        }
        System.out.println(diferenca);
    }

}
Re: Test 3#
Posted by Noob 23 Jan 2012 10:23
Use bitmasks, greedy algorithm is wrong