#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
#define int long long
int read(){
    int s = 0,ne = 1; char c = getchar();
    while(c < '0' || c > '9') ne = c == '-' ? -1 : 1, c = getchar();
    while(c >= '0' && c <= '9') s = (s << 1) + (s << 3) + c - '0', c = getchar();
    return s * ne;
}
int n, ai, sum = 0, mx = 0;
signed main(){
    n = read(); for(int i = 1; i <= n; i++) ai = read(), mx = max(mx, ai), sum += mx - ai;
    printf("%lld", sum);
}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+5;
int a[N];

int main(){
int n;cin>>n;
ll sum=0;
for(int i=0;i<n;i++){
    cin>>a[i];
}
for(int i=1;i<n;i++){
    if(a[i]<a[i-1])
        sum+=a[i-1]-a[i],a[i]=a[i-1];
}
cout<<sum<<endl;
return 0;
}