#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n, a, m = 0, b = 1, c, k = 0, i, j, l = 1e9 + 7;
  string s, r, y;
  cin >> n;
  long long ar[n];
  for (i = 0; i < n; i++) cin >> ar[i];
  for (i = 1; i < n; i++) {
    if (ar[i - 1] >= ar[i]) continue;
    k += (ar[i] - ar[i - 1]);
  }
  cout << k;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
