#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e15;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
const double PI = acos(-1.0);
mt19937 rng((int)chrono::steady_clock::now().time_since_epoch().count());
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int& x : a) cin >> x;
  long long ans = 0;
  for (int i = n - 1; i > 0; --i) ans += max(0, a[i] - a[i - 1]);
  cout << ans << "\n";
}
