Cod sursa(job #137122)

Utilizator nod_softwareBudisteanu Ionut Alexandru nod_software Data 16 februarie 2008 22:38:15
Problema Operatii Scor 40
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.04 kb
program NOD_SOFTWARE;
var c,n,max,i,j:longint;
    v:array [1..1000000] of longint;
    fin,fout:text;
{*------------------------*}
procedure citire;
begin
     assign(fin,'operatii.in'); reset(fin);
     assign(fout,'operatii.out'); rewrite(fout);
     readln(fin,n);
     max:=-1;

     for i:=1 to n do
     begin
          read(fin,v[i]);
          if max < v[i] then max:=v[i];
     end;
     close(fin);
end;
{*------------------------*}
function cauta:integer;
begin
     cauta:=0;
     for i:=1 to n do
         if v[i] = max then
         begin
              cauta:=i;
              exit;
         end;
end;
{*----------MAIN----------*}
begin
     citire;
     inc(max);
     c:=0;
     while max>1 do
     begin
          dec(max);
          while cauta > 0 do
          begin
               while v[i]=max do
               begin
                    inc(i);
                    dec(v[i-1]);
               end;
               inc(c);
          end;
     end;
     writeln(fout,c);
     close(fout);
end.