#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int arr[N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T;
  cin >> T;
  for (int i = 0; i < T; i++) {
    cin >> arr[i];
  }
  int total = 0;
  for (int i = 0; i < T; i++) {
    int j;
    for (j = i + 1; j < T; j++) {
      if (arr[j] < arr[i]) {
        total += arr[i] - arr[j];
      } else {
        i = j - 1;
        break;
      }
    }
    if (j == T) break;
  }
  cout << total;
}
