#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
class Solve {
 public:
  void solution() {
    long long n;
    cin >> n;
    vector<int> array(n);
    for (int i = 0; i < n; i++) cin >> array[i];
    int stool = 0;
    int curmax = array[0];
    for (int i = 1; i < n; i++) {
      if (array[i] > curmax) {
        curmax = array[i];
      } else {
        stool += curmax - array[i];
      }
    }
    cout << stool << endl;
    return;
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  Solve s;
  s.solution();
  return 0;
}
