#include <bits/stdc++.h>
using namespace std;
const char en = '\n';
constexpr long long INF = 1000000000;
constexpr long long MOD = 1000000007;
void add_edge(vector<vector<int>>& g, int from, int to, bool isDirected) {
  g[from].push_back(to);
  if (!isDirected) g[to].push_back(from);
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < (n); ++i) cin >> a[i];
  int height = a[0], ans = 0;
  for (int i = 0; i < (n); ++i) {
    ans += max(0, height - a[i]);
    height = max(height, a[i]);
  }
  cout << ans << en;
}
