#include <bits/stdc++.h>
using namespace std;
int n, c = 0;
int heights[200000];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> heights[i];
  }
  for (int i = 1; i < n; i++) {
    if (heights[i] < heights[i - 1]) {
      c += heights[i - 1] - heights[i];
      heights[i] = heights[i - 1];
    }
  }
  cout << c;
}
