#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  while (cin >> n) {
    int a[n];
    int ans = 0;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      if (i != 0) {
        ans += max(0, a[i] - a[i - 1]);
        a[i] = a[i] + max(0, a[i] - a[i - 1]);
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
