#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int MOD = 1e9 + 7;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int A[n];
  for (int i = 0; i < n; i++) cin >> A[i];
  int ans = 0;
  for (int i = 1; i < n; i++) {
    int stool = A[i] - A[i - 1];
    ans += max(0, stool);
    A[i] += max(0, stool);
  }
  cout << ans << endl;
  return 0;
}
