Code 

Copy
Copy
#include<stdio.h>
int main()
{
  int n,i,d;
  long long sum;
  sum=0;
  scanf("%d", &n);
  
  int h[n];
  
  for (i=0;i<n;i++)
  {
	scanf("%d",&h[i]);    
  }
  
  for (i=0;i<n-1;i++)
  {
   if (h[i]>h[i+1])
   {
	d=h[i]-h[i+1];
	h[i+1]=h[i+1]+d;
    sum=sum+d;
   }
  }
  printf("%lld",sum);
  return 0;
}