#include <bits/stdc++.h>
using namespace std;
int main() {
  int t = 1;
  while (t--) {
    int n;
    cin >> n;
    int mx = 0;
    int ans = 0;
    int temp;
    for (int i = 0; i < n; i++) {
      cin >> temp;
      mx = max(temp, mx);
      ans += mx - temp;
    }
    cout << ans << endl;
  }
}
