/* competitive programming */
#include "bits/stdc++.h"
using namespace std;

/* using */
// long
using ll = long long;
using ld = long double;
// vector
using vi = vector<int>;
using vd = vector<double>;
using vll = vector<ll>;
using vld = vector<ld>;
using vs = vector<string>;

/* define */
// push_back系
#define pb(x) push_back(x)    // push_back
#define eb(x) emplace_back(x) // emplace_back
// all range
#define all(x) x.begin(), x.end() // all_range
// PI
#define PI M_PI // PI

/* 関数、テンプレート宣言 */
// 変数, リテラルのcin
template <typename T1>
void rd(T1 &a); // 1変数, リテラルのcin
template <typename T1, typename T2>
void rd(T1 &a, T2 &b); // 2変数, リテラルのcin
template <typename T1, typename T2, typename T3>
void rd(T1 &a, T2 &b, T3 &c); // 3変数, リテラルのcin
template <typename T1, typename T2, typename T3, typename T4>
void rd(T1 &a, T2 &b, T3 &c, T4 &d); // 4変数, リテラルのcin
template <typename T1, typename T2, typename T3, typename T4, typename T5>
void rd(T1 &a, T2 &b, T3 &c, T4 &d, T5 &e); // 5変数, リテラルのcin
// 変数, リテラルのcout
template <typename T1>
void wt(T1 &a); // 1変数, リテラルのcout
template <typename T1, typename T2>
void wt(T1 &a, T2 &b); // 2変数, リテラルのcout
template <typename T1, typename T2, typename T3>
void wt(T1 &a, T2 &b, T3 &c); // 3変数, リテラルのcout
template <typename T1, typename T2, typename T3, typename T4>
void wt(T1 &a, T2 &b, T3 &c, T4 &d); // 4変数, リテラルのcout
template <typename T1, typename T2, typename T3, typename T4, typename T5>
void wt(T1 &a, T2 &b, T3 &c, T4 &d, T5 &e); // 5変数, リテラルのcout
// max, min
template <typename T, size_t N>
inline size_t sizeOfArray(const T (&)[N]); // 配列の要素数を取得
template <typename T, size_t N>
T amax(const T (&a)[N]); // 配列 max(1次元)
template <typename T, size_t N>
T amin(const T (&a)[N]); // 配列 min(1次元)
template <typename T>
T vmax(vector<T> v); // vector max(1次元)
template <typename T>
T vmin(vector<T> v); // vector min(1次元)
// gcd, lcm
template <typename T>
T gcd(T a, T b); // 最大公約数
template <typename T>
T lcm(T a, T b); // 最小公倍数

/* main */
void Main(void)
{
    ll n;
    rd(n);
    vll a;
    for (ll i = 0; i < n; i++)
    {
        ll tmp;
        rd(tmp);
        a.pb(tmp);
    }
    ll ans = 0;
    for (ll i = 1; i < n; i++)
    {
        ll b = a[i - 1] - a[i];
        if (b > 0)
        {
            a[i] += b;
            ans += b;
        }
    }
    wt(ans);
}

int main(int argc, char *argv[])
{
    ll now_time = clock();
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    Main();
    if (argc == 2 && *argv[1] == 't')
    {
        // 時間計測
        cout << endl;
        cout << "Execution Time : " << 1.0 * (clock() - now_time) / CLOCKS_PER_SEC << "[sec]" << endl;
    }
    else
    {
        // 通常処理
    }

    return 0;
}

/* template */
template <typename T1>
void rd(T1 &a)
{
    cin >> a;
}

template <typename T1, typename T2>
void rd(T1 &a, T2 &b)
{
    cin >> a >> b;
}

template <typename T1, typename T2, typename T3>
void rd(T1 &a, T2 &b, T3 &c)
{
    cin >> a >> b >> c;
}

template <typename T1, typename T2, typename T3, typename T4>
void rd(T1 &a, T2 &b, T3 &c, T4 &d)
{
    cin >> a >> b >> c >> d;
}

template <typename T1, typename T2, typename T3, typename T4, typename T5>
void rd(T1 &a, T2 &b, T3 &c, T4 &d, T5 &e)
{
    cin >> a >> b >> c >> d >> e;
}

template <typename T1>
void wt(T1 &a)
{
    cout << a << endl;
}

template <typename T1, typename T2>
void wt(T1 &a, T2 &b)
{
    cout << a << " " << b << endl;
}

template <typename T1, typename T2, typename T3>
void wt(T1 &a, T2 &b, T3 &c)
{
    cout << a << " " << b << " " << c << endl;
}

template <typename T1, typename T2, typename T3, typename T4>
void wt(T1 &a, T2 &b, T3 &c, T4 &d)
{
    cout << a << " " << b << " " << c << " " << d << endl;
}

template <typename T1, typename T2, typename T3, typename T4, typename T5>
void wt(T1 &a, T2 &b, T3 &c, T4 &d, T5 &e)
{
    cout << a << " " << b << " " << c << " " << d << " " << e << endl;
}

template <typename T, size_t N>
inline size_t sizeOfArray(const T (&)[N])
{
    return N;
}

template <typename T, size_t N>
T amax(const T (&a)[N])
{
    return *max_element(a, a + N);
}

template <typename T, size_t N>
T amin(const T (&a)[N])
{
    return *min_element(a, a + N);
}

template <typename T>
T vmax(vector<T> v)
{
    return *max_element(all(v));
}

template <typename T>
T vmin(vector<T> v)
{
    return *min_element(all(v));
}

template <typename T>
T gcd(T a, T b)
{
    return __gcd(a, b);
}

template <typename T>
T lcm(T a, T b)
{
    return (a / gcd(a, b) * b);
}


Measured memory_bytes: 5404