#include <iostream>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <string>
#include <vector>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <cstdint>
#include <cassert>
#include <cmath>
#include <bitset>
#include <type_traits>
#include <iomanip>
#include <random>
#include <chrono>
// avoid TLE due to I/O bottleneck
#define __FAST_IO std::ios_base::sync_with_stdio(false), \
                  std::cin.tie(nullptr), \
                  std::cout.tie(nullptr), std::cerr << std::boolalpha;
// load any type of variables from std::cin for -std=c++17
// template<typename ...Args>
// void load_vars(Args&&... args) {
//     (std::cin >> ... >> args);
// }
// load any type of variables from std::cin for -std=c++11
template <typename T>
void q_load(T& t) {
    std::cin >> t;
}
template <typename Head, typename... Tail>
void q_load(Head& H, Tail&&... T) {
  std::cin >> H;
  q_load(T...);
}
// load 1d vector
template<typename T>
void load(std::vector<T>& v) {
    for (T& x: v) std::cin >> x;
}
// load 2d unweighted graph, shift is subtracted, both is for direction
template<typename T>
void load(std::vector< std::vector<T> >& v, int n_lines,
                    int shift=0, bool both=false) {
    for (int i = 0; i < n_lines; ++i) {
        T x, y; std::cin >> x >> y;
        x -= shift; y -= shift;
        v[x].push_back(y);
        if (both) { v[y].push_back(x); }
    }
}
// load 2d weighted graph, shift is subtracted, both is for direction
template<typename T, typename S>
void load(std::vector< std::vector< std::pair<T, S> > >& v, 
                      int n_lines, int shift=0, bool both=false) {
    for (int i = 0; i < n_lines; ++i) {
        T x; S y; int z; std::cin >> x >> y >> z;
        x -= shift; y -= shift;
        v[x].push_back({y, z});
        if (both) { v[y].push_back({x, z}); }
    }
}
// load 2 parallel vectors
template<typename T, typename S>
void load(std::vector<T>& v1, std::vector<S>& v2, int n_lines) {
    for (int i = 0; i < n_lines; ++i) {
        T x; S y; std::cin >> x >> y;
        v1[i] = x;
        v2[i] = y;
    }
}
// load bit string to a bit vector
void load(std::vector<bool>& v) {
    char ch;
    for (int i = 0; i < (int)v.size(); ++i) {
        std::cin >> ch;
        v[i] = bool(ch - '0');
    }
}

// random number generator
std::mt19937 rng(
    (unsigned int)(
        std::chrono::steady_clock::now().time_since_epoch().count()
    )
);
// random number generator
std::mt19937_64 rng_64(
    (unsigned long)(
        std::chrono::steady_clock::now().time_since_epoch().count()
    )
);
// std::uniform_int_distribution<int\int64_t> distrib(a, b);
// distrib(rng) to get the number from [a, b]

// helpful functions
namespace ds {
// some constants, not global, use ds::
const int32_t INF_INT32 = 1e9 + 42;
const int64_t INF_INT64 = 1e18 + 300;
const int MOD = 1e9 + 7;
const long double EPS = 1e-8;

    
inline int gcd(int a, int b) {
    return std::__gcd(a, b);
}

} // end namespace ds//#include "../codebase/debugging.hpp"

int main(int argc, char const *argv[])
{
    // turn_off_de_bug();
    __FAST_IO;
    
    int NUM_CASES = 1;
    // std::cin >> NUM_CASES;
    for (int caze = 1; caze < NUM_CASES+1; ++caze) {
        // std::cout << "Case #" << caze << ": "; std::cout << "\n";
    
    long long n, accum = 0;;
    long long max = -ds::INF_INT32;
    std::cin >> n;
    for (int i = 0; i < n; ++i) {
        long long a; std::cin >> a;
        if ( a > max) {
            max = a;
        } else
        {
             accum += max - a;
        }
//        debug_(a, max, accum)
        
    }
    std::cout << accum << '\n';
        
//        debug_s(HR);
    }

    return 0;
}


/*
g++ c.cpp && a < input\c
submit.bat c.cpp
*/

Measured memory_bytes: 3552