#include <bits/stdc++.h>
using namespace std;
uint32_t n;
vector<uint32_t> a;
void solve() {
  uint32_t sum = 0;
  uint32_t max_height = 0;
  for (int i = 0; i < n - 1; i++) {
    max_height = max(max_height, a[i]);
    if (max_height > a[i + 1]) {
      sum += max_height - a[i + 1];
    }
  }
  printf("%d\n", sum);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    uint32_t tmp = 0;
    scanf("%d", &tmp);
    a.push_back(tmp);
  }
  solve();
}
