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

No subject
Posted by getuojian 15 Apr 2007 10:17
DFS
why it was wrong.....
on test#1
-.-


const
  oo=999999999;
var
  min:longint;
  stone:array[1..20]of longint;
  mid:longint;
  ls,rs:longint;
  n:longint;

procedure readin;
var
  i:longint;
  tot:longint;
begin
  readln(n);
  tot:=0;
  for i:=1 to n do begin
    readln(stone[i]);
    tot:=tot+stone[i];
  end;
  mid:=tot div n + 1;
  ls:=0; rs:=0;
end;

procedure solve(k:longint);
var
  i:longint;
begin
  if k=n+1 then begin
    if abs(ls-rs)<min then min:=abs(ls-rs);
    exit;
  end;

  for i:=1 to 2 do
    begin
      if i=1 then begin
        ls:=ls+stone[k];
        solve(k+1);
        ls:=ls-stone[k];
      end;

      if i=2 then begin
        rs:=rs+stone[k];
        solve(k+1);
        rs:=rs-stone[k];
      end;
    end;


end;

begin
  min:=oo;
  readin();
  solve(1);
  writeln(min);
end.