#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

int main()
{
  	int n;
	cin >> n;
  	vector<int> data(n);
  	REP(i, n){
      cin >> data[i];
    }
  
  	int bf = 0;
  int sum = 0;
  
  REP(i,n){
    int current = data[i];
    int step = 0;
    if(bf > current){
      step = bf - current;
    }
    bf = current + step;
    sum += step;
  }
  
  cout << step;
    return 0;
}