#include <bits/stdc++.h>
using namespace std;
struct XX {
  int a;
  int i;
};
class xxGreater {
 public:
  bool operator()(const XX& riLeft, const XX& riRight) const {
    if ((riLeft.a) == (riRight.a)) {
      return riLeft.i < riRight.i;
    }
    return (riLeft.a) < (riRight.a);
  }
};
vector<int> G[100001];
bool used[100001];
map<pair<int, int>, int> cnt;
void dfs(int s, int depth) {
  used[s] = true;
  for (int i = 0; i < G[s].size(); i++) {
    if (!used[G[s][i]]) {
      cnt[make_pair(s, G[s][i])] = depth;
      dfs(G[s][i], depth + 1);
    }
  }
}
int main(int argc, const char* argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  int a[200000];
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  int ans = 0;
  for (int i = 1; i < N; i++) {
    if (a[i - 1] > a[i]) {
      ans += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  cout << ans << endl;
  return 0;
}
