#include <bits/stdc++.h>
using namespace std;
static int my_speed_up = []() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  return 0;
}();
using ll = long long;
using ull = unsigned long long;
template <class T>
using v = vector<T>;
template <class T>
using vv = vector<vector<T>>;
template <class T>
using vvv = vector<vector<vector<T>>>;
template <class T>
using vvvv = vector<vector<vector<vector<T>>>>;
vv<int> d4 = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
vv<int> d8 = {{1, 0}, {-1, 0}, {0, 1},  {0, -1},
              {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
bool is_valid_pos(const vv<int>& tb, int x, int y) {
  return x >= 0 && x < tb.size() && y >= 0 && y < tb[0].size();
}
int main() {
  int N;
  cin >> N;
  v<int> tb(N);
  for (int i = 0; i < (N); ++i) cin >> tb[i];
  int cur_max = tb[0];
  int res = 0;
  for (int i = 1; i < (tb.size()); ++i) {
    if (tb[i] < cur_max)
      res += cur_max - tb[i];
    else
      cur_max = tb[i];
  }
  cout << res << endl;
  return 0;
}
