#include <bits/stdc++.h>
using namespace std;
const int INF = (int)0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double eps = 1e-9;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, mx = 0, ans = 0, tmp;
  cin >> n;
  for (int i = 0; i < n && cin >> tmp; i++)
    ans += max(0, mx - tmp), mx = max(mx, tmp);
  cout << ans;
  return 0;
}
