#include <bits/stdc++.h>
using namespace std;
const int Mod = 1E9 + 7;
const int Inf = 1E9 + 7;
const int N = 2E5 + 10;
int n, a[N], res = 0;
void Input() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
}
void Solve() {
  for (int i = 2; i <= n; i++) {
    res += max(a[i - 1] - a[i], 0);
    a[i] = max(a[i], a[i - 1]);
  }
  cout << res;
}
int main() {
  if (fopen("test"
            ".inp",
            "r")) {
    freopen(
        "test"
        ".inp",
        "r", stdin);
    freopen(
        "test"
        ".out",
        "w", stdout);
  }
  int testcase = 1;
  while (testcase--) {
    Input();
    Solve();
  }
}
