#include<bits/stdc++.h>
using namespace std;
int n,ans,maxx=-0x3f3f3f3f;
int b[10005];
int main(){
cin>>n;
int tmp;
for(int i=0;i<n;i++){
cin>>tmp;
ans-=tmp;
maxx=max(tmp,maxx)==tmp?i:maxx;
b[i]=tmp;
}
tmp=-0x3f3f3f3f;
for(int i=0;i<n,i!=maxx;i++){
tmp=max(tmp,b[i]);
ans+=tmp;
//cout<<tmp<<" "<<ans<<endl;
}
tmp=-0x3f3f3f3f;
for(int i=n-1;i>0,i+1!=maxx;i--){
tmp=max(tmp,b[i]);
ans+=tmp;
//cout<<tmp<<" "<<ans<<endl;
}
cout<<ans;
}