Cod sursa(job #147997)

Utilizator nod_softwareBudisteanu Ionut Alexandru nod_software Data 3 martie 2008 19:58:02
Problema Operatii Scor 40
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.2 kb
program operatii2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

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.