program Znyzhka; {$APPTYPE CONSOLE} uses SysUtils; var a:array of integer;s:integer; function n:integer; begin Result:=Length(a) end; function ReadInt:Integer; begin Read(Result) end; function FindMax(Not1:Integer=-1;Not2:Integer=-1;Not3:Integer=-1):Integer; var i:integer; begin if n=0 then begin Result:=-1;Exit end; Result:=0; for i:=1 to n-1 do begin if (a[i]>=a[Result])and(i<>Not1)and(i<>Not2)and(i<>Not3) then Result:=i; end; if (Result=Not1)or(Result=Not2)or(Result=Not3) then Result:=-1; end; procedure Step; var i1,i2,i3,i4:integer; procedure Process(i:integer;AndSum:boolean=True); begin if i=-1 then Exit; if AndSum then Inc(S,a[i]); a[i]:=0 end; begin i1:=FindMax; i2:=FindMax(i1); i3:=FindMax(i1,i2); i4:=FindMax(i1,i2,i3); Process(i1); Process(i2); Process(i3); Process(i4,False); end; function Done:boolean; var i:integer; begin for i:=0 to n-1 do if a[i]<>0 then begin Result:=False;Exit end; Result:=True end; var i:integer; begin { TODO -oUser -cConsole Main : Insert code here } Assign(Input,'DISCOUNT.DAT');Reset(Input); SetLength(a,ReadInt); for i:=0 to n-1 do a[i]:=ReadInt; Close(Input); s:=0; while not Done do Step; Assign(Output,'DISCOUNT.SOL');Rewrite(Output); writeln(s); Close(Output) end.