#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int prev = 0, ans = 0;
  for (int i = 1; i <= (n); i++) {
    int val;
    cin >> val;
    if (val < prev) ans += (prev - val), val = prev;
    prev = val;
  }
  cout << ans << endl;
}
