fork download
  1. program fence;
  2. uses Math;
  3. const MAXN = 10000;
  4.  
  5. { input data }
  6. var
  7. N, M, i, ans : longint;
  8. V,inizio, fine : array[0..MAXN-1] of int64;
  9.  
  10.  
  11. begin
  12. {
  13.   uncomment the following lines if you want to read/write from files
  14.   assign(input, 'input.txt'); reset(input);
  15.   assign(output, 'output.txt'); rewrite(output);
  16. }
  17.  
  18.  
  19. { read numbers N in a single line }
  20. readln(N);
  21. { read all numbers V[i] in the next line }
  22. for i:=0 to N-1 do
  23. read(V[i]);
  24. readln();
  25. ans:=0;
  26. for i:=0 to N-1 do
  27. begin
  28. inizio[i]:=V[i];
  29. fine[i]:=V[i];
  30. end;
  31. for i:=1 to N-1 do inizio[i]:=max(V[i],inizio[i-1]);
  32. for i:=N-2 downto 0 do fine[i]:=max(V[i],fine[i+1]);
  33. for i:=1 to N-1 do ans:=ans+min (inizio[i-1], fine[i]);
  34. writeln(ans);
  35. end.
Success #stdin #stdout 0s 5316KB
stdin
10
5 4 18 11 19 14 21 7 0 10

stdout
114