#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int N = 2e6 + 13, M = N;
vector<int> g[N];
int a[N];
int n, x, t, i;
void solve() {
  cin >> n;
  int tallest = -1;
  int stool = 0;
  for (i = 0; i < n; i++) {
    cin >> x;
    if (x > tallest) {
      tallest = x;
    } else {
      stool += (x - tallest);
    }
  }
  cout << stool;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
