#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
const int inf = 1e9 + 10;
const ll INF = 4e18 + 10;
const int dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
const int dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
const int p = 1000005;
int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int a[n];
  for (ll i = (0); i < (n); i++) cin >> a[i];
  int ans = 0;
  for (ll i = (0); i < (n - 1); i++) {
    if (a[i + 1] < a[i]) {
      ans += a[i] - a[i + 1];
      a[i + 1] = a[i];
    }
  }
  cout << ans << "\n";
  return 0;
}
