#include <bits/stdc++.h>
#include <string.h>
using namespace std;
#define ll long long
int main() {
ll t,a,temp,ans=0;
cin>>t;
cin>>temp;
ll e=0;
while(--t){
cin>>a;
ans+=max(temp-a,e);
temp=max(temp,a);
}
cout<<ans;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBsbCBsb25nIGxvbmcKCmludCBtYWluKCkgewoJbGwgdCxhLHRlbXAsYW5zPTA7CgljaW4+PnQ7CgljaW4+PnRlbXA7CglsbCBlPTA7CndoaWxlKC0tdCl7CgkJY2luPj5hOwoJCWFucys9bWF4KHRlbXAtYSxlKTsKCQl0ZW1wPW1heCh0ZW1wLGEpOwoJfQoJY291dDw8YW5zOwpyZXR1cm4gMDsKfQ==