Cod sursa(job #66142)

Utilizator info_arrandrei gigea info_arr Data 16 iunie 2007 00:50:05
Problema Semne Scor 5
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.13 kb
{$IFDEF NORMAL}
  {$I-,Q-,R-,S-}
{$ENDIF NORMAL}
{$IFDEF DEBUG}
  {$I+,Q+,R+,S-}
{$ENDIF DEBUG}
{$IFDEF RELEASE}
  {$I-,Q-,R-,S-}
{$ENDIF RELEASE}

const MAX_N = 50000;
const MAX_V = 5000000;

//1-> +
//0-> -
var i,n,s:longint;
    a:array[1..50000] of longint;
    p:array[1..5000000] of char ;
    fi,fo:text;
    sp,sm:longint;

 procedure solve;
  var x:longint;
  begin

   while (sp-sm<>s) do
    begin
     randomize;
     x:=random(n)+1;
     if (x>n) or (x<1) then continue;
     if (sp-sm>s) and (p[a[x]]='1') then
      begin
       inc(sm,a[x]); dec(sp,a[x]);
       p[a[x]]:='0';
       continue;
      end;
     if (sp-sm<s) and (p[a[x]]='0') then
      begin
       inc(sp,a[x]); dec(sm,a[x]);
       p[a[x]]:='1';
       continue;
      end;
    end;
    for i:=1 to n do
     if (p[a[i]]='1')then write(fo,'+')
      else write(fo,'-');
  end;



begin
 randomize;
 assign(fi,'semne.in'); reset(fi);
 assign(fo,'semne.out'); rewrite(fo);
 readln(fi,n,s);
 for i:=1 to n do
  begin
   read(fi,a[i]);
   p[a[i]]:='1';
   sp:=sp+a[i];
  end;
 solve;
close(fi);
close(fo);
end.