#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int test_cases = 1;
  while (test_cases--) {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    long long int sum = 0;
    for (int i = 1; i < n; i++) {
      sum += long long int(max(a[i] - a[i - 1], 0));
    }
    cout << sum << "\n";
  }
}
