#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using p = pair<int, int>;
#define INF 1001001001
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repX(i, n ,x) for (int i = x; i < (int)(n); i++)
#define repBack(i, n) for (int i = n; i >= 0; i--)
#define dup(x,y) (((x)+(y)-1)/(y))

int main() {
	int N;
	cin >> N;
	vector<int> A(N);
	ll ans = 0;
	ll maxA = 0;
	rep(i,N){
		cin >> A[i];
		maxA = max(maxA,A[i]);
		ans = A[i] - maxA;
	}
	cout << ans << endl;
	

	return 0;
}
