{ Uþdavinio autorius: doc. Jonas Blonskis, KTU informatikos fakulteto dëstytojas;
  Sprendimà ir testus parengë: doc. Jonas Blonskis }

program Sumavimas;
uses Crt;
const L = 10;
type  Mas = array[ 1..L ] of integer;
{-------------------------------------}
procedure Pradeda( var A : Mas; var n : integer);
var i : integer;
begin
  WriteLn( ' Surinkite skaièius: ');
  n := 0;
  while not EoLn and ( n < L )do
  begin
    n := n + 1;
    Read( A[n] );
  end;
  ReadLn;
end;
{-------------------------------------}
procedure Prideda( var A : Mas; n : integer);
var i : integer;
    P : integer;
begin
   P := A[1];
   if A[1] = 0 then  A[1] := 1
               else  A[1] := 0;
   i := 2;
   while ( i <= n ) and ( P <> 0 ) do
     if A[i] = 0 then begin A[i] := 1; P := 0;   end
                 else begin A[i] := 0; i := i+1; end;
end;
{-------------------------------------}
function Dar( var A : Mas; n : integer) : boolean;
var  i : integer;
begin
  Dar := False;
  for i := 1 to n do
    if A[i] = 0 then Dar := True;
end;
{-------------------------------------}
function Suma( var A, V : Mas; n : integer): integer;
var S, i : integer;
begin
   S := 0;
   for i := 1 to n do
     if V[i] = 1 then S := S+A[i];
   Suma := S;
end;
{-------------------------------------}
procedure Rezultatas( var A, V : Mas; n : integer);
var i : integer; p : boolean;
begin
  p := True;
  for i := 1 to n do
   if V[i] = 1 then
     if p then begin p := False;  Write( A[i] );  end
          else Write( ' + ', A[i] );
  WriteLn;
end;
{-------------------------------------}
var A : Mas;  n : integer;
    V : Mas;
    i : integer;
    Jau : boolean;         `
    S : integer;
begin
 ClrScr;
 for i := 1 to L do V[i] := 0;
 Write( 'Kokia suma? '); ReadLn( S );
 Pradeda( A, n );
 Jau := False;
 while not Jau and  Dar( V, n ) do
 begin
   if Suma( A, V, n ) = S then Jau := True
                          else  Prideda( V, n );
 end;
   Write( S : 6, ' = ');
   if Jau then Rezultatas( A, V, n )
          else WriteLn( 'Tokios sumos negausime');
 ReadLn;
end.