#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
int main() {
  int N;
  cin >> N;
  int now = 0;
  int ans = 0;
  for (int i = 0; i < (N); i++) {
    int A;
    cin >> A;
    if (i == 0) {
      now = A;
      continue;
    }
    if (now > A) {
      ans += now - A;
    } else {
      now = A;
    }
  }
  cout << ans << endl;
  return 0;
}
