#include <bits/stdc++.h>
    using namespace std;
     typedef long long int int64;
   
    #define SCD(t) scanf("%d",&t)
    #define SCLD(t) scanf("%ld",&t)
    #define SCLLD(t) scanf("%lld",&t)
    #define SCC(t) scanf("%c",&t)
    #define SCS(t) scanf("%s",t)
    #define SCF(t) scanf("%f",&t)
    #define SCLF(t) scanf("%lf",&t)
    #define MEM(a, b) memset(a, (b), sizeof(a))
    #define FOR1(i,n, k,a) for (int i=n ; i<k ; i+=1) cin>>a[i];
    #define FOR(i,n, k) for (long long int i=n ; i<k ; i+=1)
    #define RFOR(i, j, k, in) for (int i=j ; i>=k ; i-=in)
    #define REP(i, j) FOR(i, 0, j, 1)
    #define RREP(i, j) RFOR(i, j, 0, 1)
    #define all(cont) cont.begin(), cont.end()
    #define rall(cont) cont.end(), cont.begin()
    #define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
    #define IN(A, B, C) assert( B <= A && A <= C)
    #define MP make_pair
    #define PB push_back
    #define PB1 pop_back
    #define INF (int)1e18
    #define EPS 1e-9
    #define PI 3.1415926535897932384626433832795
    #define MOD 1000000007
    #define read(type) readInt<type>()
    #define all1(m) m.begin(),m.end()
    const double pi=acos(-1.0);
    typedef pair<int, int> PII;
    typedef vector<int> VI;
    typedef vector<string> VS;
    typedef vector<PII> VII;
    typedef vector<VI> VVI;
    typedef map<int64,int64> MPII;
    typedef set<int> SETI;
    typedef multiset<int> MSETI;
    typedef long int int32;
    typedef unsigned long int uint32;

    typedef unsigned long long int  uint64;
    //STL
    /*
    SET
    set<int> s;
    s.insert(3);
    s.count(3);
    s.erase(3);
    s.size();
    multiset<int> s;
    s.erase(5); alll
    s.erase(s.find(5)); first
    
    MAP
    if (m.count("aybabtu")) {
      // key exists
    }
    ITTERATE IN MAP
    for (auto x : m) {
    cout << x.first << " " << x.second << "\n";
    }


    BITSET
    bitset<10> s(string("0010011010"));
    cout << s.count() << "\n"; // 4
    The following code shows examples of using bit operations:
    bitset<10> a(string("0010110110"));
    bitset<10> b(string("1011011000"));
    cout << (a&b) << "\n"; // 0010010000
    cout << (a|b) << "\n"; // 1011111110
    cout << (a^b) << "\n"; // 1001101110

    DEQUE
    deque<int> d;
    d.push_back(5); // [5]
    d.push_back(2); // [5,2]
    d.push_front(3); // [3,5,2]
    d.pop_back(); // [3,5]
    d.pop_front(); // [5]
    d.back()
    
    PRIORITY QUEUE
    priority_queue<int> q;
    q.push(3);
    q.push(5);
    q.push(7);
    q.push(2);
    cout << q.top() << "\n"; // 7
    q.pop();
    cout << q.top() << "\n"; // 5
    q.pop();
    q.push(6);
    cout << q.top() << "\n"; // 6
    q.pop();
    If we want to create a priority queue that supports finding and removing the
    smallest element, we can do it as follows:
    priority_queue<int,vector<int>,greater<int>> q;







  */// C++ Program to print prime factors and their 
  // powers using Sieve Of Eratosthenes 

  // Using SieveOfEratosthenes to find smallest prime 
  // factor of all the numbers. 
  // For example, if N is 10, 
  // s[2] = s[4] = s[6] = s[10] = 2 
  // s[3] = s[9] = 3 
  // s[5] = 5 
  // s[7] = 7 






   
    int main(){
      ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
       
        #ifndef ONLINE_JUDGE  
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
      
    #endif
        int t;
        t=1;
     //cin>>t;
      
      while(t--){

      int64 n;
      cin>>n;
      int64 a[n];
      FOR1(i,0,n,a)
    int64 ans=0;
    FOR(i,1,n){
        if(a[i]<a[i-1]){
            ans+=a[i-1]-a[i];
            a[i]=a[i-1];
;        }
    }
    cout<<ans<<"\n";
  
        
    }
  }

Measured memory_bytes: 5172