#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  int ans[n];
  for (int i = 0; i < n; ++i) {
    cin >> ans[i];
  }
  int count = 0;
  int maxNum = ans[0];
  for (int i = 0; i < n; ++i) {
    maxNum = max(ans[i], maxNum);
    count += maxNum - ans[i];
  }
  cout << count << endl;
}
