#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
long long int INF = 1001001001001001LL;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;
template <typename T1, typename T2>
inline void chmin(T1 &a, const T2 &b) {
  if (a > b) a = b;
}
template <typename T1, typename T2>
inline void chmax(T1 &a, const T2 &b) {
  if (a < b) a = b;
}
int main() {
  int cur = 0;
  int ans = 0;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (i == 0) {
      cur = a;
    }
    if (cur > a) {
      ans += cur - a;
    }
    cur = max(cur, a);
  }
  cout << ans << endl;
  return 0;
}
