#include <bits/stdc++.h>
using namespace std;
vector<int> adj_list[100010];
bool visited[10010];
void dfs(int node) {
  visited[node] = true;
  for (int n : adj_list[node]) {
    if (!visited[n]) {
      dfs(n);
    }
  }
}
int main() {
  int n;
  cin >> n;
  int ma = 1000;
  int count = 0;
  for (int i = 0; i < (n); i++) {
    int a;
    cin >> a;
    if (i == 0) {
      ma = a;
    } else {
      if (ma > a) {
        count += ma - a;
      } else {
        ma = a;
      }
    }
  }
  cout << count << endl;
}
