例如,有3个人甲、乙、丙,他们单独过桥的时间分别为1、2、4,则总共最少需要的时间为7。具体方法是:甲、乙一起过桥到河的左岸,甲单独回到河的右岸将灯带回,然后甲、丙再一起过桥到河的左岸,总时间为2+1+4=7。
const size=100; infinity=10000; left=true; right=false; left_to_right=true; right_to_left=false; var n,i:integer; time:array [1..size] of integer; pos:array [1..size] of boolean; function max(a,b:integer):integer; begin if a>b then max:=a else max:=b; end; function go(stage:boolean):integer; var I,j,num,tmp,ans:integer; begin if (stage=right_to_lefft) then begin num:=0; ans:=0; for i:=1 to n do if pos[i]=right then begin inc(num); if time[i]>ans then ans:=time[i]; end; if 1 then begin go:=ans; exit; end; ans:=infinity; for i:=1 to n-1 do if pos[i]=right then for j:=i+1 to n do if pos[j]=right then begin pos[i]:=left; pos[j]:=left; tmp:=max(time[i],time[j])+ 2 ; if tmp<ans then ans:=tmp; pos[i]:=right; pos[j]:=right; end; go:=ans; else if (stage=left_to_right) then begin ans:=infinity; for i:=1 to n do if 3 then begin pos[i]:=right; tmp:= 4 ; if tmp<ans then ans:=tmp; 5 ; end; go:=ans; end else go:=0; end; begin readln(n); for i:=1 to n do begin read(time[i]); pos[i]:=right; end; writeln(go(right_to_left)); end.