KSPREE - Triple Shoot

Tác giả: RR

Ngôn ngữ: Pascal

{$MODE OBJFPC}
{$R-,Q-}

uses math;
const
  FINP          =       '';
  FOUT          =       '';
  MAXN          =       22;

var
  f1,f2         :       text;
  n,res         :       longint;
  a             :       array[1..MAXN] of longint;
  sum           :       longint;
  dd            :       array[1..MAXN] of boolean;
  now           :       longint;

procedure openF;
    begin
      assign(f1,FINP); reset(f1);
      assign(f2,FOUT); rewrite(f2);
    end;

procedure closeF;
    begin
      close(f1); close(f2);
    end;

procedure inp;
    var
      i:longint;
    begin
      read(f1,n);
      for i:=1 to n do
        begin
          read(f1,a[i]);
          inc(sum,a[i]);
          dd[i]:=true;
        end;
    end;

procedure duyet(i:longint); inline;
    var
      x,y,z:longint;
      cx,cy,cz:boolean;
    begin
      for y:=1 to n do
        begin
          x:=y-1; if x=0 then x:=n;
          z:=y+1; if z>n then z:=1;
          cx:=dd[x]; cy:=dd[y]; cz:=dd[z];
          if cx or cy or cz=false then continue;
          if cx then begin dec(sum,a[x]); dd[x]:=false; end;
          if cy then begin dec(sum,a[y]); dd[y]:=false; end;
          if cz then begin dec(sum,a[z]); dd[z]:=false; end;

          inc(now,sum);

          if (now<res) and (sum>0) then duyet(i+1)
          else res:=min(res,now);

          dec(now,sum);

          if cx then begin inc(sum,a[x]); dd[x]:=true; end;
          if cy then begin inc(sum,a[y]); dd[y]:=true; end;
          if cz then begin inc(sum,a[z]); dd[z]:=true; end;
        end;
    end;

begin
  openF;
  inp;
  res:=high(longint);
  duyet(1);
  writeln(f2,res);
  closeF;
end.

Download