#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const long long LINF = 1e18;
int n, m, a[maxn];
string second, ss;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  long long cnt = 0;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 1; i < n; i++)
    if (a[i] < a[i - 1])
      cnt += (long long)a[i - 1] - (long long)a[i],
          a[i] += (long long)a[i - 1] - (long long)a[i];
  cout << cnt << "\n";
}
