// warm heart, wagging tail,and a smile just for you!
//                         _ _ _ __ __ __  __ _ _ __ _ _ __ _ __ ____ _ _ _ _ _ _ _ _ 
//                          ||__|| || ||\\//||  //__\\  ||\\ || ||__   ||__||  ||  ||
//                          ||  || || ||    || //    \\ || \\||  ___|| ||  ||  \\__//
//                                                    _ ___ _ _ _ _  _ __ _ _ __ _ __ _ __ _ _ __ ___ __ _ _ _ _ ___ _ _ ___
//                                                    ||__   ||__||  //__\\  ||\\ || ||// ||__|| ||  ]] ||__||  //__\\  ||__))
//                                                    ____|| ||  || //    \\ || \\|| ||\\ ||  || ||__]] ||  || //    \\ || \\
//                                                                     ¦¦¦¦¦¦¦¦¦¦¦
//                                                                   ¦¦¦++++++++++¦¦¦
//                                                                ¦¦¦+++++¦¦¦¦++++++¦¦¦
//                                            ¦¦¦¦¦¦¦¦¦¦¦       ¦¦+++++¦¦¦¦++¦¦¦¦+++++¦¦
//                                      ¦¦¦¦¦¦¦¦¦+++++¦¦¦¦¦¦¦¦¦¦¦¦+++++¦¦++++++¦¦¦+++++¦¦
//                               ¦¦¦¦¦¦¦¦++++++++++++++++++++++¦¦¦¦¦¦¦¦¦++++++¦¦+++++++¦¦
//                             ¦¦¦¦+¦¦+++++++++++++++++++++++++++++¦¦¦¦¦¦¦¦¦+++++++++++¦¦                 
//                           ¦¦¦+++¦++++++++++++++++++++++++++++++++++++¦¦++¦¦¦+++++++¦¦¦¦¦                                        
//                         ¦¦¦+++++++++++++++++++++++++++++++++++++++++++¦¦+++¦¦¦¦¦¦¦¦+++++¦¦                             
//                       ¦¦¦++++++++++++++++++++++++++++++++++++++++++++++++++++¦¦¦+++++++++¦¦¦              
//                     ¦¦¦++++++++++++++++++++++++++++++++++++++++++++++++++++++++¦¦¦¦¦+++++++¦¦             
//                 ¦¦¦¦++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++¦¦¦¦+++++¦¦¦¦
//     ¦¦¦¦¦¦¦¦¦¦¦¦¦++++++++¦¦+++++¦¦¦¦+++++++++++++++++¦¦¦¦¦+++++++++++++++++++++++++++¦¦¦++++¦¦¦¦¦¦            
//   ¦¦¦¦+++++++++++++++++++¦¦++¦¦¦¦¦¦+++++++++++++++++++++¦¦¦¦¦¦+++++++¦¦¦¦¦¦¦¦¦¦¦++++++++¦¦+++¦¦+++¦¦
// ¦¦¦++++++++++++++++++++¦¦+++++++++++++++++¦¦+++++++++++++++++++++¦¦¦¦+++++++++++¦+++++++¦¦++++++++¦¦
// ¦¦+++++++++++++++++++++¦¦++++¦¦¦¦¦¦+++¦¦¦¦++¦¦¦¦+++++++¦¦¦¦¦¦¦¦¦¦+++++++++++++++++++++++¦¦+++++++¦¦¦
// ¦¦+++++++++++++++++++++¦¦¦¦¦¦¦¦¦¦¦¦¦+++++++++++++++++++¦¦¦¦¦¦¦¦¦++++++++++++++++++++++++¦¦++++¦¦¦¦¦
// ¦¦¦++++++++++++++++++¦¦¦+++++¦¦++++++++++++++++++++++++++++¦¦¦¦¦++++++++++++++++++++++++¦¦¦¦¦¦¦¦
//   ¦¦¦+++++++++++++¦¦¦¦¦++++++++¦¦++++++++++++++++++++++++¦¦¦++¦¦+++++++++++++++++++++++¦¦
//       ¦¦¦¦¦¦¦¦¦¦¦¦¦¦  ¦¦¦¦++++++¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦+++++¦¦+++++++++++++++++++¦¦¦¦
//                         ¦¦¦¦¦¦¦                           ¦¦¦¦¦  ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
//
//#include<boost/multiprecision/cpp_int.hpp>
#include <bits/stdc++.h>
const long double pi = 3.1415926535897932384626433832795;
#define scn(n) scanf("%d",&n)

#define lscn(n) scanf("%lld ",&n)

#define scn2(n1,n2) scanf("%d%d",&n1,&n2)

#define lscn2(n1,n2) scanf("%lld%lld",&n1,&n2)

#define lpri(n) printf("%lld ",n)

#define lpri2(n1,n2) printf("%lld %lld\n",n1,n2)

#define pri(n) printf("%d ",n)

#define pri2(n1,n2) printf("%d %d\n",n1,n2)

#define pln() printf("\n")

#define priln(n) printf("%d\n",n)

#define lpriln(n) printf("%lld\n",n)

#define RAL(i,x,n) for(int i=x;i<n;i++)

#define RALR(i,x,n) for(int i=x-1;i>=n;i--)
//insert function in vector is used to insert a value at given index
//ex: v.insert(v.begin(),10); inserts 10 at 0th position in vector 

//vector.resize(3) is used to resize the size of given array 
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORd(i, a, b) for (int i = (a) - 1; i >= (b); --i)
#define mp make_pair

#define F first

#define S second
#define sq(a) (a)*(a)
#include<unordered_set>
#define lli long long int
#define fastio ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
#define test lli t;cin>>t;while(t--)
#define arrin lli n;cin>>n;lli a[n];for(lli i=0;i<n;i++)cin>>a[i]       // a%b=a-b*floor(a/b);
//#define arrin(x) lli n,x;cin>>n>>x;lli a[n];for(lli i=0;i<n;i++)cin>>a[i]  
#define floop for(lli i=0;i<n;i++)
#define mod 1000000007
#define defvec vector<lli>v
#define ub upper_bound
#define pb push_back
#define pob pop_back
#define mp make_pair
#define emp emplace_back
#define bs binary_search
#define pi 3.1415926536
#define INF ll(8230219982008199610)
#define MINF ll(-8230219982008199610)
#define ones(n) __builtin_popcountll(n)
#define focc(n) __builtin_ffsll(n) //to count index of first 1 from right in binary representation of a number starting from index 1
#define leadingzero(n) __builtin_clzll(n)
#define trailingzeroes(n) __builtin_ctzll(n)
#define loop0(i,n) for(int i=0; i<n; i++)
#define loop1(i,n) for(int i=1; i<=n; i++)
#define rev0(i,n)  for(int i=n-1; i>=0; i--)
#define rev1(i,n)  for(int i=n; i>0; i--)
#define print(v) for(auto x:v)cout<<x<<" "
#define prints for(lli i=0;s[i];i++)cout<<s[i]
//using namespace boost::multiprecision ;
//typedef long long ll;
//typedef vector<lli> vi;
//typedef pair<lli,lli> pi;
using namespace std;

//all_of(a,a+n,ispositive());  //to check all the elements in array are positive
//any_of(a,a+n,ispositive());  //to check any element in array is positive
//none_of(a,a+n,ispositive());  //to check none of the elements in array is positive
//copy_n(a,n,b);  //to copy all the elements from a to b
//iota(a,a+n,10); //this will print consequtive n terms starting from 10 //ex if n=5 than 10,11,12,13,14
//same can be used for character array

//a number is divisible by 8 if last three digits of it is divisible by 8
//ex. 1512 as 512 is divisible by 8 so 1512 is also divisible by 8

/* NUMBER OF WAYS TO PLACE TWO QUEENS ON A BOARD OF SIZE n*n SUCH THAT THEY DO NOT ATTACK ON EACH OTHER
   count=(pow(n,4)/2)-(5*(pow(n,3)/3)+(3*pow(n,2)/2)-n/3;
*/
/* bitset<10>s;
  s[0]=1;
  s[3]=1;
  s[5]=1;
  s[7]=1;
  cout<<s;
 output is 1001010100
  cout<<s.count();
  output is frequency of 1 so 4

*/

// a number is power of 4 if num>0 && log2(num)/2==(int)log2(num)/2

#define INT_BITS 16
//if(abs(a-b)<1e-9)                  //TO COMPARE FLOATING POINT INTEGER
//{  
//    a and b are equal
//}

//string s=R"(HIMANS&\nSHAN\t)"; we can print a whole string same as given by doing this
//vector<int>v={1,2,3,4};
//vector<int>w=move(v);
//after this v={};
//w={1,2,3,4};
// to reverse a string reverse(str.begin(), str.end());
//  OR
// string rev = string(str.rbegin(),str.rend());

//q.front() operation works in queue but not works in priority_queue
//so for priority_queue q.top() is used

//for a priority_queue in increasing order priority_queue<int ,vector<int>,greater<int>>q;

// ~x=-(x+1)  ex: ~29=-30;

unsigned lli leftRotate(unsigned lli n, unsigned lli d) 
{ 
    return (n << d)|(n >> (INT_BITS - d)); 
} 
unsigned lli rightRotate(unsigned lli n,unsigned lli d) 
{ 
    return (n >> d)|(n << (INT_BITS - d)); 
} 
//TO SET Kth BIT AS ONE n|(1<<k) while n&(~(1<<k)) sets the kth bit of n to 0
//and n^(1<<k) inverts the kth bit of n

//Every composite number has at least one prime factor less than or equal to square root of itself
int log2(int x)
{
  int res=0;
  while(x>>=1)
  res++;
  return res;
}
lli inverse(lli a,lli m)
{
    return 1<a?m-inverse(m%a,a)*(m/a):1;    //for fastly calculate inverse modulo
}
lli issorted(lli arr[], lli n) 

{ 
 if (n == 1 || n == 0) 
        
return 1; 
  
 
if(arr[n - 1] <= arr[n - 2]) 
       
return 0; 
  


return issorted(arr, n - 1); 

} 

int computeXOR(int n)
{
  if(n%4==0)
  return n;                        //TO CALCULATE XOR FROM 1 TO N
  if(n%4==1)
  return 1;
  if(n%4==2)
  return n+1;
  else
  return 0;
}

lli count(string s)//count distinct element in string
{
    lli ans=1,i,j;
    for(i=1;i<s.length();i++)
    {
        for(j=0;j<i;j++)
        {
            if(s[i]==s[j])
            break;
        }
        if(i==j)
        ans++;
    }
    return ans;
}

bool isPerfectSquare(long double x) 

{   
  // Find floating point value of  
  
// square root of x. 
  
long double sr = sqrt(x); 
  
  
// If square root is an integer 
  
return ((sr - floor(sr)) == 0); 

} 

lli countarray(lli a[],lli n)//count distinct element in array
{
  unordered_set<lli>s;
for(lli i=0;i<n;i++)
s.insert(a[i]);
return s.size();
}



bool check (int N)          //to check whether ith bit of n is 1 or 0
    {  lli i;
        if( N & (1 << i) )
            return true;
        else
            return false;
    }



bool isvowel(char ch)      
{
    string s = "aeiouAEIOU";
    for (char c : s)
    {
        if (c == ch)
        {
            return true;
        }
    }
    return false;
}



bool isPrime(lli n) 
{ 
    if(n<=1)  return false; 
    if(n<=3)  return true; 
    if(n%2==0||n%3==0) return false; 
    for(lli i=5; i*i<=n; i=i+6) 
    if (n%i == 0 || n%(i+2) == 0) 
    return false; 
    return true; 
}



struct interval

{
    int start,end;
   
 
};
                                             //SORTING IN A SPECIAL ORDER
bool compare(interval x,interval y)

{
    return (x.start<y.start);
}


bool isSubstring(string s1, string s2) 
{ 
    int M = s1.length(); 
    int N = s2.length(); 
  
    /* A loop to slide pat[] one by one */
    for (int i = 0; i <= N - M; i++) { 
        int j; 
  
        /* For current index i, check for 
 pattern match */
        for (j = 0; j < M; j++) 
            if (s2[i + j] != s1[j]) 
                break; 
  
        if (j == M) 
            return true; 
    } 
  
    return false; 
} 

int seive(int n)  //to count number of primes from 1 to n
{
   
bool isprime[n+1];
    
for(int i=2;i<=n;i++)
    
isprime[i]=true;
    
isprime[0]=false;
    
isprime[1]=false;
    
for(int i=2;i<=sqrt(n);i++)
    
{   
if(isprime[i]==true)
       
{
           
for(int j=i*i;j<=n;j+=i)
        
{
           
isprime[j]=false;
               
}

}
      
    
}
    
int count=0;
    
for(int i=0;i<=n;i++)
    
{
        
if(isprime[i]==true)
        
count++;
    
}
    
return count;

}



void multiply(int a[2][2],int m[2][2])

{                                                 //
int him[2][2];
                                    //   MATRIX EXPONENTIATION
for(int i=0;i<2;i++)
                              //
{   
                                              //
for(int j=0;j<2;j++)
        
{   him[i][j]=0;
            
for(int k=0;k<2;k++)
            
{
                
him[i][j]+=(a[i][k]*m[k][j]);
            
}
            
        
}
    
}
    
for(int i=0;i<2;i++)
    
{
        for(int j=0;j<2;j++)
        
{
            a[i][j]=him[i][j];
        
}
    
}
}
void power(int a[2][2],int n)

{   if(n==0||n==1)
    
return;
    
power(a,n/2);
    
multiply(a,a);
    
if(n%2!=0)
    
{  
int m[2][2]={{1,1},{1,0}};
        
multiply(a,m);
    
}

}
int fib(int n)

{
    
int a[2][2]={{1,1},{1,0}};
    
if(n==0)
    
return 0;
    
power(a,n-1);
    
return a[0][0];

}



int factmod(int n, int p)   //to calculate (n!)%p
{
    
int res = 1;
    
while (n > 1) 
{
        
res = (res * ((n/p) % 2 ?  p-1 : 1)) % p;
        
for (int i = 2; i <=(n%p);i++)
            
res = (res * i) % p;
        
n /= p;
    
}
    
return res % p;

}



/*int bubblesort(int a[],int n)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < n-1; j++) {
if (a[j] > a[j+1]) {
swap(a[j],a[j+1]);
} 
} 
}
}*/



lli modularexponentiation(lli a,lli n) 
{ 
    lli res = 1;      
   
    while (n > 0) 
    { 
        if (n& 1) 
            res = ((res%mod)*(a%mod))%mod; 
        n/=2; 
        a=((a%mod)*(a%mod))%mod;
    } 
    return res; 
}



lli largest_power(lli N)
    {                                              //LARGEST POWER OF 2 JUST LESS THAN OR EQUAL TO A GIVEN 16 BIT NUMBER
        //changing all right side bits to 1.
        N = N| (N>>1);
        N = N| (N>>2);
        N = N| (N>>4);
        N = N| (N>>8);
        N = N| (N>>16);  

    //as now the number is 2 * x-1, where x is required answer, so adding 1 and dividing it by
                return (N+1)>>1;

    }

bool alternate(lli n)
{
    lli i;
    if((n^(n>>1))==(pow(2,i)-1))           //this is to check that in binary representation of n, n has alternate sequence or not
    return true;                             //ex.101010          if n^(n>>1) is like this 11111 or containing only set bits than yes otherwise no  
return false;
}

    lli countDivisors(lli n) 
{ 
    lli cnt = 0; 
    for (int i = 1; i <= sqrt(n); i++) { 
        if (n % i == 0) { 
            // If divisors are equal, 
            // count only one 
            if (n / i == i) 
                cnt++; 
  
            else // Otherwise count both 
                cnt = cnt + 2; 
        } 
    } 
    return cnt;
}


int highestpowerof2(int n)   //highest power of 2 which divides a given number
{
    return (n&(~(n-1)));
}

/*lli dectobin(lli n)
{                                           //DECIMAL TO BINARY FOR A 32 BIT INTEGER
for (int i = 31; i >= 0; i--) {
if (n&(1<<i)) cout << "1";
else cout << "0";
}
}*/
/*lli maxfrequency(lli v[],lli n)
{    lli n;
		cin >> n;
                lli a[n];
		vector<lli> cnt(n + 1);
		for (int i = 0; i < n; ++i) {
			cin>>a[i];
			++cnt[a[i]];
		}
		lli mx = *max_element(cnt.begin(), cnt.end());
		lli diff = n + 1 - count(cnt.begin(), cnt.end(), 0);//COUNT OF DIFFERENT ELEMENTS PRESENT IN ARRAY
return mx;
}*/
void lowertoupper(char ch)
{
   ch=ch&'_';
  cout<<ch<<" ";
}
void uppertolower(char ch)
{
  ch=ch|' ';
  cout<<ch<<" ";
}
lli primecounter(lli l,lli r)
{                              //this counts prime in range l to r both inclusive
   lli fact=1,count=0;
   for(lli i=l;i<=r;i++)
   {
      if(((fact-i)%((1LL<<i)-1))==0)
      count++;
    fact*=((1LL<<i)-1);
   }
  return count;
} 
lli counteprime(lli l,lli r)
{
   lli count=0,i,j;
  for(i=l;i<=r;i++)           //THIS WILL ALSO GIVE COUNT OF PRIMES IN RANGE L TO R
  {
     for(j=2;j<=(i/j);j++)
     {
       if(!(i%j))
       break;
     }
      if(j>(i/j))
      count++;
   }
return count;
}
void pairsort(int a[], char b[], int n)     //SORT AN ARRAY ACCORDING TO SECOND ARRAY
{ 
    pair<int, char> pairt[n]; 
  
    // Storing the respective array 
    // elements in pairs. 
    for (int i = 0; i < n; i++)  
    { 
        pairt[i].first = a[i]; 
        pairt[i].second = b[i]; 
    } 
  
    // Sorting the pair array. 
    sort(pairt, pairt + n); 
      
    // Modifying original arrays 
    for (int i = 0; i < n; i++)  
    { 
        a[i] = pairt[i].first; 
        b[i] = pairt[i].second; 
    } 
} 
lli fibo(lli n)
{
    long double him=(1+sqrt(5))/2;
  return round(pow(him,n)/sqrt(5));
}
lli binToDec(string c) 
{  
    lli count=0;
    for(lli i=0;i<c.length();i++)
   {
       if (c[i] == '1'){
            lli val = 1;
           for(lli j=0;j<i;j++)
           {
                val *= 2;
            }
            count += val;
        }
   }
   return count;
} 
/*void concatenate(int arr1[], int arr2[], int m, int n) 
{ 
   memcpy(arr1 + m, arr2, sizeof(arr2));    //TO CONCATENATE TWO ARRAYS
} */
int main()
{
   fastio;
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);

    arrin;
    lli sum=0;
    lli ma=a[0];
    for(lli i=1;i<n;i++)
    {
        ma=max(ma,a[i]);
        if(a[i]<ma)
        sum+=ma-a[i];
        
    }
    cout<<sum<<endl;
}

Measured memory_bytes: 5196