#include <bits/stdc++.h>
using namespace std;
int main() {
  int test = 1;
  while (test--) {
    int n;
    cin >> n;
    int a[n], store[n], tot = 0;
    for (int i = 0; i < n; i++) {
      cin >> a[i], tot += a[i];
    }
    int ans = 0;
    for (int i = 1; i < n; i++) {
      ans += max(a[i] - a[i - 1], 0);
      a[i] = max(a[i], a[i - 1]);
    }
    cout << ans << '\n';
  }
  return 0;
}
