#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int INF = 1001001001;
using ll = long long;
using P = pair<int, int>;
template <typename T>
using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
int main() {
  int now = 0;
  int ans = 0;
  int n;
  cin >> n;
  for (int i = (0); i <= ((n)-1); ++i) {
    int a;
    cin >> a;
    if (i != 0) ans += max(now - a, 0);
    now = max(now, a);
  }
  cout << ans << endl;
  return 0;
}
