#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

const int MAXN = 10000;

int a[MAXN], b[MAXN], f[MAXN];

int main() {
	int n;
	while (scanf("%d", &n) != EOF) {
		for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
		for (int i = 1; i <= n; i++) b[i] = 0;
		for (int i = 1; i <= n; i++) {
			while (a[i] % 2 == 0) {
				a[i] /= 2;
				b[i] ++;
			}
		}
		n++;
		a[n] = 1;
		b[n] = 0;
		// for (int i = 1; i <= n; i++) cout << a[i] << ' '; cout << endl;
		// for (int i = 1; i <= n; i++) cout << b[i] << ' '; cout << endl;
		memset(f, 63, sizeof(f)); f[1] = 0;
		for (int i = 1; i < n; i++)
			for (int j = i + 1; j <= n; j++)
				if (a[i] % a[j] == 0 && ((b[j] == 0) || (b[j] > 0 && b[i] + (j - i) >= b[j])))
				{
					// cout << i << ' ' << j << endl;
					f[j] = min(f[i] + (j - i - 1), f[j]);
				}
		// for (int i = 1; i <= n; i++) cout << f[i] << ' '; cout << endl;
		cout << f[n] << endl;
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

const int MAXN = 10000;

long long a[MAXN], b[MAXN], f[MAXN];

int check(int i, int j) {
	if (a[i] % a[j] != 0) return false;
	if (b[j] == 0) return true;
	if (b[i] + (j - i) == b[j]) return true;
	if (j - i - 1 >= b[j]) return true;
	return false;
}

int main() {
	int n;
	while (scanf("%d", &n) != EOF) {
		for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]);
		for (int i = 1; i <= n; i++) b[i] = 0;
		for (int i = 1; i <= n; i++) {
			while (a[i] % 2 == 0) {
				a[i] /= 2;
				b[i] ++;
			}
		}
		n++;
		a[n] = 1;
		b[n] = 0;
		for (int i = 1; i <= n; i++) f[i] = i - 1;
		for (int i = 1; i < n; i++)
			for (int j = i + 1; j <= n; j++)
				if (check(i, j)) {
					// cout << i << ' ' << j << endl;
					f[j] = min(f[i] + (j - i - 1), f[j]);
				}
		// for (int i = 1; i <= n; i++) cout << f[i] << ' '; cout << endl;
		cout << f[n] << endl;
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

const int MAXN = 10000;

int a[MAXN], b[MAXN], f[MAXN];

int main() {
	int n;
	while (scanf("%d", &n) != EOF) {
		for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
		for (int i = 1; i <= n; i++) b[i] = 0;
		for (int i = 1; i <= n; i++) {
			while (a[i] % 2 == 0) {
				a[i] /= 2;
				b[i] ++;
			}
		}
		n++;
		a[n] = 1;
		b[n] = 0;
		// for (int i = 1; i <= n; i++) cout << a[i] << ' '; cout << endl;
		// for (int i = 1; i <= n; i++) cout << b[i] << ' '; cout << endl;
		memset(f, 63, sizeof(f)); f[1] = 0;
		for (int i = 1; i < n; i++)
			for (int j = i + 1; j <= n; j++)
				if (a[i] % a[j] == 0 && ((b[j] == 0) || (b[j] > 0 && b[i] + (j - i) <= b[j])))
				{
					// cout << i << ' ' << j << endl;
					f[j] = min(f[i] + (j - i - 1), f[j]);
				}
		// for (int i = 1; i <= n; i++) cout << f[i] << ' '; cout << endl;
		cout << f[n] << endl;
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

const int MAXN = 10000;

int a[MAXN], b[MAXN], f[MAXN];

int main() {
	int n;
	while (scanf("%d", &n) != EOF) {
		for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
		for (int i = 1; i <= n; i++) b[i] = 0;
		for (int i = 1; i <= n; i++) {
			while (a[i] % 2 == 0) {
				a[i] /= 2;
				b[i] ++;
			}
		}
		// for (int i = 1; i <= n; i++) cout << a[i] << ' '; cout << endl;
		// for (int i = 1; i <= n; i++) cout << b[i] << ' '; cout << endl;
		memset(f, 63, sizeof(f)); f[1] = 0;
		for (int i = 1; i < n; i++)
			for (int j = i + 1; j <= n; j++)
				if (a[i] % a[j] == 0 && b[i] + (j - i) >= b[j]) {
					// cout << i << ' ' << j << endl;
					f[j] = min(f[i] + (j - i - 1), f[j]);
				}
		// for (int i = 1; i <= n; i++) cout << f[i] << ' '; cout << endl;
		cout << f[n] << endl;
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
#define ll long long
#define ld long double
#define pb push_back
#define ci const int&
#define inf 1000002012
#define Maxn 5013
#define Maxm

using namespace std;

int s[Maxn], a[Maxn], f[Maxn];
int N;

int main(){
	//freopen("D.in","r",stdin);freopen("D.out","w",stdout);

	scanf( "%d", &N );
	for ( int i = 1; i <= N; ++i ) scanf( "%d", s + i );
	for ( int i = N; i >= 1; --i )
	{
		scanf( "%d", s + i );
		a[i] = 0;
		f[i] = 1;
		while ( !(s[i]&1) ) s[i]>>=1, a[i]++;
		for ( int j = i + 1; j <= N; ++j )
		if ( a[j] )
		{
			if ( max(a[j]-(j-i),0) == a[i]&& s[i]%s[j] == 0 )
			f[i] = max( f[j] + 1, f[i] );
		}
		else
		{
			if ( s[i]%s[j] == 0 )
			f[i] = max( f[i], f[j] + 1 );
		}
	}
	int Max = 0;
	for ( int i = 1; i <= N; ++i ) Max = max( f[i], Max );
	cout<<N-Max;

	//fclose(stdin);fclose(stdout);
	return 0;
}


#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
#define ll long long
#define ld long double
#define pb push_back
#define ci const int&
#define inf 1000002012
#define Maxn 5013
#define Maxm

using namespace std;

ll s[Maxn], a[Maxn], f[Maxn];
int N;

int main(){
	//freopen("D.in","r",stdin);freopen("D.out","w",stdout);

	scanf( "%d", &N );
	for ( int i = 1; i <= N; ++i ) scanf( "%I64d", s + i );
	for ( int i = N; i >= 1; --i )
	{
		a[i] = 0;
		f[i] = 1;
		while ( !(s[i]&1) ) s[i]>>=1, a[i]++;
		for ( int j = i + 1; j <= N; ++j )
		if ( a[j] )
		{
			if ( a[j] - (j-i) >= 0 )
			{
				if ( a[j]-(j-i)== a[i]&& s[i]%s[j] == 0 )
				f[i] = max( f[j] + 1, f[i] );
			}
			else if ( s[i]%s[j] == 0 ) f[i] = max( f[j] + 1,f[i] );
		}
		else
		{
			if ( s[i]%s[j] == 0 )
			f[i] = max( f[i], f[j] + 1 );
		}
	}
	ll Max = 0;
	for ( int i = 1; i <= N; ++i ) Max = max( f[i], Max );
	cout<<N-Max;

	//fclose(stdin);fclose(stdout);
	return 0;
}


#include<iostream>
using namespace std;
long ans,a[5001],b[5001],n,i,j,k,m,l,r;
main()
{
      cin>>n;
      for (i=1;i<=n;i++)
      {
      cin>>a[i];
      b[i]=a[i];
      }

      for (i=2;i<=n;i++)
        if (a[i-1]>a[i])
          {
          if (a[i-2]<a[i]) a[i-1]=a[i]; else  a[i]=a[i-1];
             }

              for (j=1;j<=n;j++)
              ans=ans+abs(b[j]-a[j]);
cout<<ans<<endl;
}


#include<iostream>
using namespace std;
long long  ans,a[5001],b[5001];
long n;
long i,j,k,m,l,r;
main()
{
      cin>>n;
      for (i=1;i<=n;i++)
      {
      cin>>a[i];
      b[i]=a[i];
      }
      ans=0;
      for (i=2;i<=n;i++)
        if (a[i-1]>a[i])
          {
          if (a[i-2]<a[i]) a[i-1]=a[i]; else  a[i]=a[i-1];
          }

              for (j=1;j<=n;j++)
              if (b[j]>a[j]) ans=ans+(b[j]-a[j]); else ans=ans+(a[j]-b[j]);
              cout<<ans<<endl;
   }


#include<iostream>
using namespace std;
long ans,a[5001],b[5001],n,i,j,k,m,l,r;
main()
{
      cin>>n;
      for (i=1;i<=n;i++)
      {
      cin>>a[i];
      b[i]=a[i];
      }
      for (i=2;i<=n;i++)
         {
          if (a[i-1]>a[i])
          {
          if (a[i-2]<a[i]) {
                           a[i-1]=a[i];
                           }
          else {
               a[i]=a[i-1];
                 }
             }
             }
              for (j=1;j<=n;j++)
              ans=ans+abs(b[j]-a[j]);
cout<<ans<<endl;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;

const int maxn = 5*1000+1;

int n;
int a[maxn];
int b[maxn];
long long dp[2][maxn];

int main() {
	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
		b[i] = a[i];
	}
	sort(b, b+n);
	for (int i = 0; i < n; ++i)
		dp[0][i] = abs(a[0]-b[i]);
	for (int i = 1; i < n; ++i)
		dp[0][i] = min(dp[0][i], dp[0][i-1]);
	for (int i = 1; i < n; ++i) {
		dp[i%2][0] = abs(a[i]-b[0]) + dp[1-(i%2)][0];
		for (int j = 1; j < n; ++j)
			dp[i%2][j] = min(abs(a[i]-b[j]) + dp[1-(i%2)][j], dp[i%2][j-1]);
	}
	cout << dp[1-n%2][n-1] << endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

const int maxn = 5*1000+1;
const int inf = 1000*1000*1000+1000;

int n;
int a[maxn];
int b[maxn];
int dp[2][maxn];

int main() {
	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
		b[i] = a[i];
	}
	sort(b, b+n);
	for (int i = 0; i < 2; ++i)
		for (int j = 0; j < n; ++j)
			dp[i][j] = inf;
	for (int i = 0; i < n; ++i)
		dp[0][i] = abs(a[0]-b[i]);
	for (int i = 1; i < n; ++i)
		for (int j = 0; j < n; ++j) {
			dp[i%2][j] = inf;
			for (int k = 0; k <= j; ++k)
				dp[i%2][j] = min(dp[i%2][j], abs(a[i]-b[j]) + dp[1-i%2][k]);
		}
	int ans = inf;
	for (int i = 0; i < n; ++i) ans = min(ans, dp[(n-1)%2][i]);
	cout << ans << endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

const int maxn = 5*1000+1;

int n;
long long a[maxn];
long long b[maxn];
long long dp[2][maxn];

int main() {
	long long inf = 1000*1000;
	inf *= inf;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
		b[i] = a[i];
	}
	sort(b, b+n);
	for (int i = 0; i < n; ++i)
		dp[0][i] = abs(a[0]-b[i]);
	cerr << dp[0][0] << " " << dp[0][1] << endl;
	for (int i = 1; i < n; ++i)
		for (int j = 0; j < n; ++j) {
			dp[i%2][j] = inf;
			for (int k = 0; k <= j; ++k)
				if (abs(a[i]-b[j]) + dp[1-(i%2)][k] < dp[i%2][j])
					dp[i%2][j] = abs(a[i]-b[j]) + dp[1-(i%2)][k];
		}
	long long ans = inf;
	for (int i = 0; i < n; ++i) ans = min(ans, dp[(n-1)%2][i]);
	cout << ans << endl;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;

const int maxn = 5*1000+1;

int n;
int a[maxn];
int b[maxn];
long long dp[2][maxn];

int main() {
	long long inf = 1000*1000;
	inf *= inf;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		scanf("%d", &a[i]);
		b[i] = a[i];
	}
	sort(b, b+n);
	for (int i = 0; i < n; ++i)
		dp[0][i] = abs(a[0]-b[i]);
	for (int i = 1; i < n; ++i) {
		dp[i%2][0] = abs(a[i]-b[0]) + dp[1-(i%2)][0];
		for (int j = 1; j < n; ++j)
			dp[i%2][j] = min(abs(a[i]-b[j]) + dp[1-(i%2)][j], dp[i%2][j-1]);
	}
	cout << dp[1-n%2][n-1] << endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

const int maxn = 5*1000+1;
const int inf = 1000*1000*1000+1000;

int n;
int a[maxn];
int b[maxn];
int dp[maxn][maxn];

int main() {
	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
		b[i] = a[i];
	}
	sort(b, b+n);
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < n; ++j)
			dp[i][j] = inf;
	for (int i = 0; i < n; ++i)
		dp[0][i] = abs(a[0]-b[i]);
	for (int i = 1; i < n; ++i)
		for (int j = 0; j < n; ++j)
			for (int k = 0; k <= j; ++k)
				dp[i][j] = min(dp[i][j], abs(a[i]-b[j]) + dp[i-1][k]);
	int ans = inf;
	for (int i = 0; i < n; ++i) ans = min(ans, dp[n-1][i]);
	cout << ans << endl;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;

const int maxn = 5*1000+1;

int n;
int a[maxn];
int b[maxn];
long long dp[2][maxn];

int main() {
	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
		b[i] = a[i];
	}
	sort(b, b+n);
	dp[0][0] = a[0]-b[0];
	for (int i = 1; i < n; ++i)
		dp[0][i] = min(dp[0][i-1], abs(a[0]-b[i]));
	for (int i = 1; i < n; ++i) {
		dp[i%2][0] = abs(a[i]-b[0]) + dp[1-(i%2)][0];
		for (int j = 1; j < n; ++j)
			dp[i%2][j] = min(abs(a[i]-b[j]) + dp[1-(i%2)][j], dp[i%2][j-1]);
	}
	cout << dp[1-n%2][n-1] << endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

const int maxn = 5*1000+1;
const int inf = 1000*1000*1000+1000;

int n;
int a[maxn];
int b[maxn];
long long dp[2][maxn];

int main() {
	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
		b[i] = a[i];
	}
	sort(b, b+n);
	for (int i = 0; i < 2; ++i)
		for (int j = 0; j < n; ++j)
			dp[i][j] = inf;
	for (int i = 0; i < n; ++i)
		dp[0][i] = abs(a[0]-b[i]);
	for (int i = 1; i < n; ++i)
		for (int j = 0; j < n; ++j) {
			dp[i%2][j] = inf;
			for (int k = 0; k <= j; ++k)
				dp[i%2][j] = min(dp[i%2][j], abs(a[i]-b[j]) + dp[1-i%2][k]);
		}
	long long ans = inf;
	for (int i = 0; i < n; ++i) ans = min(ans, dp[(n-1)%2][i]);
	cout << ans << endl;
	return 0;
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <iterator>
#include <string>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <stdio.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define MP      make_pair
#define PB      push_back
#define SZ(v)   (v).size()
#define ALL(v)  (v).begin(),(v).end()
#define FOREACH(it,v) \
                       for (__typeof( v.begin() ) it = v.begin(); it!=v.end(); ++it)
#define FOR(i,a,b) \
                       for (int i=a; i<b; i++)
#define FORE(i,a,b) \
                       for (int i=a; i<=b; i++)

typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<int> vi;

#define MAX 5010
ll t[MAX];
ll diff[MAX];
int ndiff;
ll res = 0;
bool ok = false;
ll dp[2][MAX];
int n;

int main()
{
  cin>>n;
  set<ll> s;
  FOR(i,0,n){
    cin>>t[i];
    s.insert(t[i]);
  }
  FOREACH(it,s) {
    diff[ndiff++] = *it;
  }
  int who = 0;
  FOR(i,0,n) {
    ll last = 1LL<<62;
    FOR(j,0,ndiff) {
      if(i == 0) {
    dp[who][j] = abs(t[i] - diff[j]);
      } else {
    dp[who][j] = abs(t[i] - diff[j]) + min(last,dp[who^1][j]);
    last = min(last, dp[who^1][j]);
      }
    }
    who ^= 1;
  }
  res = dp[who^1][0];
  FOR(i,1,ndiff){
    if(dp[who^1][i] < res) {
      res = dp[who^1][i];
    }
  }
  cout<<res<<endl;

  return 0;
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <iterator>
#include <string>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <stdio.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define MP      make_pair
#define PB      push_back
#define SZ(v)   (v).size()
#define ALL(v)  (v).begin(),(v).end()
#define FOREACH(it,v) \
                       for (typeof( v.begin() ) it = v.begin(); it!=v.end(); ++it)
#define FOR(i,a,b) \
                       for (int i=a; i<b; i++)
#define FORE(i,a,b) \
                       for (int i=a; i<=b; i++)

typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<int> vi;

#define MAX 5010
ll t[MAX];

int main()
{
  int n;cin>>n;
  FOR(i,0,n)cin>>t[i];
  ll res = 0;
  bool ok = false;
  FOR(i,0,n) {
    ll tmp = 0;
    ll last = t[i];
    for(int j = i-1; j >= 0; j--) {
      if(t[j] > last) {
    tmp += t[j] - last;
      } else last = t[j];
    }
    last = t[i];
    FOR(j,i+1,n) {
      if(t[j] < last) {
    tmp += last - t[j];
      } else {
    last = t[j];
      }
    }
    if(!ok || tmp < res){
      res = tmp;
      ok = true;
    }
  }
  cout<<res<<endl;
  return 0;
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <iterator>
#include <string>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <stdio.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define MP      make_pair
#define PB      push_back
#define SZ(v)   (v).size()
#define ALL(v)  (v).begin(),(v).end()
#define FOREACH(it,v) \
                       for (typeof( v.begin() ) it = v.begin(); it!=v.end(); ++it)
#define FOR(i,a,b) \
                       for (int i=a; i<b; i++)
#define FORE(i,a,b) \
                       for (int i=a; i<=b; i++)

typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<int> vi;

#define MAX 5010
ll t[MAX];

ll res=0;
bool ok = false;
int n;

void test(int i, int inc) {
  ll tmp = inc;
    ll last = t[i];
    for(int j = i-1; j >= 0; j--) {
      if(t[j] > last) {
    tmp += t[j] - last;
      } else last = t[j];
    }
    last = t[i];
    FOR(j,i+1,n) {
      if(t[j] < last) {
    tmp += last - t[j];
      } else {
    last = t[j];
      }
    }

    if(!ok || tmp < res){
      res = tmp;
      ok = true;
    }
}

void solve() {
  vector<pair<ll,int> > diff;
  FOR(i,0,n-1) {
    diff.PB(MP(t[i+1] - t[i], i));
  }
  sort(ALL(diff));
  int len = diff.size();
  int who = diff[len / 2].second;
  ll v = diff[len / 2].first;
  t[who] -= v;
  test(who,abs(v));
  t[who] += v;
  who = diff[(len + 1) / 2].second;
  v = diff[(len + 1) / 2].first;
  t[who] -= v;
  test(who,abs(v));
  t[who] += v;
}

int main()
{
  cin>>n;
  FOR(i,0,n)cin>>t[i];

  FOR(i,0,n) {
    test(i,0);
    t[i]++;
    test(i,1);
    t[i]-=2;
    test(i,1);
    t[i]++;
  }
  if(n >= 2) {
    solve();
  }
  cout<<res<<endl;
  return 0;
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <iterator>
#include <string>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <stdio.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define MP      make_pair
#define PB      push_back
#define SZ(v)   (v).size()
#define ALL(v)  (v).begin(),(v).end()
#define FOREACH(it,v) \
                       for (typeof( v.begin() ) it = v.begin(); it!=v.end(); ++it)
#define FOR(i,a,b) \
                       for (int i=a; i<b; i++)
#define FORE(i,a,b) \
                       for (int i=a; i<=b; i++)

typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<int> vi;

#define MAX 5010
int t[MAX];

int main()
{
  int n;cin>>n;
  FOR(i,0,n)cin>>t[i];
  ll res = 0;
  bool ok = false;
  FOR(i,0,n) {
    ll tmp = 0;
    ll last = t[i];
    for(int j = i-1; j >= 0; j--) {
      if(t[j] > last) {
    tmp += t[j] - last;
      } else last = t[j];
    }
    last = t[i];
    FOR(j,i+1,n) {
      if(t[j] < last) {
    tmp += last - t[j];
      } else {
    last = t[j];
      }
    }
    if(!ok || tmp < res){
      res = tmp;
      ok = true;
    }
  }
  cout<<res<<endl;
  return 0;
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <iterator>
#include <string>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <stdio.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define MP      make_pair
#define PB      push_back
#define SZ(v)   (v).size()
#define ALL(v)  (v).begin(),(v).end()
#define FOREACH(it,v) \
                       for (typeof( v.begin() ) it = v.begin(); it!=v.end(); ++it)
#define FOR(i,a,b) \
                       for (int i=a; i<b; i++)
#define FORE(i,a,b) \
                       for (int i=a; i<=b; i++)

typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<int> vi;

#define MAX 5010
ll t[MAX];

ll res=0;
bool ok = false;
int n;

void test(int i, int l) {
  ll tmp = 0;
    ll last = l;
    for(int j = i-1; j >= 0; j--) {
      if(t[j] > last) {
    tmp += t[j] - last;
      } else last = t[j];
    }
    last = l;
    FOR(j,i,n) {
      if(t[j] < last) {
    tmp += last - t[j];
      } else {
    last = t[j];
      }
    }

    if(!ok || tmp < res){
      res = tmp;
      ok = true;
    }
}

int main()
{
  cin>>n;
  FOR(i,0,n)cin>>t[i];

  FOR(i,0,n) {
    test(i,0);
  }
  FOR(i,1,n) {
    test(i, (t[i-1] + t[i]) / 2);
    test(i, (t[i-1] + t[i] + 1) / 2);
  }
  cout<<res<<endl;
  return 0;
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <iterator>
#include <string>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <stdio.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define MP      make_pair
#define PB      push_back
#define SZ(v)   (v).size()
#define ALL(v)  (v).begin(),(v).end()
#define FOREACH(it,v) \
                       for (__typeof( v.begin() ) it = v.begin(); it!=v.end(); ++it)
#define FOR(i,a,b) \
                       for (int i=a; i<b; i++)
#define FORE(i,a,b) \
                       for (int i=a; i<=b; i++)

typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<int> vi;

#define MAX 5010
ll t[MAX];
ll diff[MAX];
int ndiff;
ll res = 0;
bool ok = false;
ll dp[MAX][MAX];
int n;

int main()
{
  cin>>n;
  set<ll> s;
  FOR(i,0,n){
    cin>>t[i];
    s.insert(t[i]);
  }
  FOREACH(it,s) {
    diff[ndiff++] = *it;
  }
  FOR(i,0,n) {
    ll last = 1LL<<62;
    FOR(j,0,ndiff) {
      if(i == 0) {
    dp[i][j] = abs(t[i] - diff[j]);
      } else {
    dp[i][j] = abs(t[i] - diff[j]) + min(last,dp[i-1][j]);
    last = min(last, dp[i-1][j]);
      }
    }
  }
  res = dp[n-1][0];
  FOR(i,1,ndiff){
    if(dp[n-1][i] < res) {
      res = dp[n-1][i];
    }
  }
  cout<<res<<endl;

  return 0;
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <iterator>
#include <string>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <stdio.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define MP      make_pair
#define PB      push_back
#define SZ(v)   (v).size()
#define ALL(v)  (v).begin(),(v).end()
#define FOREACH(it,v) \
                       for (typeof( v.begin() ) it = v.begin(); it!=v.end(); ++it)
#define FOR(i,a,b) \
                       for (int i=a; i<b; i++)
#define FORE(i,a,b) \
                       for (int i=a; i<=b; i++)

typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<int> vi;

#define MAX 5010
ll t[MAX];

ll res=0;
bool ok = false;
int n;

void test(int i, int inc) {
  ll tmp = inc;
    ll last = t[i];
    for(int j = i-1; j >= 0; j--) {
      if(t[j] > last) {
    tmp += t[j] - last;
      } else last = t[j];
    }
    last = t[i];
    FOR(j,i+1,n) {
      if(t[j] < last) {
    tmp += last - t[j];
      } else {
    last = t[j];
      }
    }

    if(!ok || tmp < res){
      res = tmp;
      ok = true;
    }
}

int main()
{
  cin>>n;
  FOR(i,0,n)cin>>t[i];

  FOR(i,0,n) {
    test(i,0);
    t[i]++;
    test(i,1);
    t[i]-=2;
    test(i,1);
    t[i]++;
  }
  cout<<res<<endl;
  return 0;
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <iterator>
#include <string>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <stdio.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define MP      make_pair
#define PB      push_back
#define SZ(v)   (v).size()
#define ALL(v)  (v).begin(),(v).end()
#define FOREACH(it,v) \
                       for (typeof( v.begin() ) it = v.begin(); it!=v.end(); ++it)
#define FOR(i,a,b) \
                       for (int i=a; i<b; i++)
#define FORE(i,a,b) \
                       for (int i=a; i<=b; i++)

typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<int> vi;

#define MAX 5010
ll t[MAX];

ll res=0;
bool ok = false;
int n;

void test(int i, int inc) {
  ll tmp = inc;
    ll last = t[i];
    for(int j = i-1; j >= 0; j--) {
      if(t[j] > last) {
    tmp += t[j] - last;
      } else last = t[j];
    }
    last = t[i];
    FOR(j,i+1,n) {
      if(t[j] < last) {
    tmp += last - t[j];
      } else {
    last = t[j];
      }
    }

    if(!ok || tmp < res){
      res = tmp;
      ok = true;
    }
}

void solve() {
  vector<pair<ll,int> > diff;
  FOR(i,0,n-1) {
    diff.PB(MP(t[i+1] - t[i], i));
  }
  sort(ALL(diff));
  int len = diff.size();
  int who = diff[len / 2].second;
  ll v = diff[len / 2].first;
  t[who] -= v;
  test(who,abs(v));
  t[who] += v;
  if((len + 1) / 2 < len) {
  who = diff[(len + 1) / 2].second;
  v = diff[(len + 1) / 2].first;
  t[who] -= v;
  test(who,abs(v));
  t[who] += v;
  }
}

int main()
{
  cin>>n;
  FOR(i,0,n)cin>>t[i];

  FOR(i,0,n) {
    test(i,0);
    t[i]++;
    test(i,1);
    t[i]-=2;
    test(i,1);
    t[i]++;
  }
  if(n >= 2) {
    solve();
  }
  cout<<res<<endl;
  return 0;
}


#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(v) ((v) > 0 ? (v) : -(v))

long long row_arr[5002] = {0};
long long ans_arr[5002] = {0};
int input_arr[5002] = {0};
int order_arr[5002] = {0};
int compare(const void* a, const void* b)
{
    return (*(int *)a - *(int* )b);
}

int main()
{
    int n, i, j, min;
    //freopen("a.txt", "r", stdin);
    scanf("%d", &n);
    for(i = 0; i < n; i++)
    {
        scanf("%d", &input_arr[i]);
        order_arr[i] = input_arr[i];
    }
    qsort(order_arr, n, sizeof(int), compare);
    row_arr[0] = ABS(input_arr[0] - order_arr[0]);
    for(i = 1; i < n; i++)
        row_arr[i] = MIN(row_arr[i - 1], ABS(input_arr[0] - order_arr[i]));
    for(i = 1; i < n; i++)
    {
        ans_arr[0] = row_arr[0] + ABS(input_arr[i] - order_arr[0]);
        for(j = 1; j < n; j++)
            ans_arr[j] = MIN(ans_arr[j - 1], row_arr[j] + ABS(input_arr[i] - order_arr[j]));
        for(j = 0; j < n; j++)
            row_arr[j] = ans_arr[j];
    }

    min = ans_arr[n - 1];
    printf("%d\n", min);
    return 0;
}


#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(v) ((v) > 0 ? (v) : -(v))

long long row_arr[5002] = {0};
long long ans_arr[5002] = {0};
int input_arr[5002] = {0};
int order_arr[5002] = {0};
int compare(const void* a, const void* b)
{
    return (*(int *)a - *(int* )b);
}

int main()
{
    int n, i, j, min;
 //   freopen("a.txt", "r", stdin);
    scanf("%d", &n);
    for(i = 0; i < n; i++)
    {
        scanf("%d", &input_arr[i]);
        order_arr[i] = input_arr[i];
    }
    qsort(order_arr, n, sizeof(int), compare);
    row_arr[0] = ABS(input_arr[0] - order_arr[0]);
    for(i = 1; i < n; i++)
        row_arr[i] = (long long)MIN(row_arr[i - 1], ABS((long long)input_arr[0] - (long long)order_arr[i]));
    for(i = 1; i < n; i++)
    {
        ans_arr[0] = row_arr[0] + ABS(input_arr[i] - order_arr[0]);
        for(j = 1; j < n; j++)
            ans_arr[j] = (long long)MIN(ans_arr[j - 1], row_arr[j] + ABS((long long)input_arr[i] - (long long)order_arr[j]));
        for(j = 0; j < n; j++)
            row_arr[j] = ans_arr[j];
    }

    min = ans_arr[n - 1];
    printf("%d\n", min);
    return 0;
}


#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(v) ((v) > 0 ? (v) : -(v))

long long row_arr[5002] = {0};
long long ans_arr[5002] = {0};
int input_arr[5002] = {0};
int order_arr[5002] = {0};
int compare(const void* a, const void* b)
{
    return (*(int *)a - *(int* )b);
}

int main()
{
    int n, i, j, min;
    //freopen("a.txt", "r", stdin);
    scanf("%d", &n);
    for(i = 0; i < n; i++)
    {
        scanf("%d", &input_arr[i]);
        order_arr[i] = input_arr[i];
    }
    qsort(order_arr, n, sizeof(int), compare);
    row_arr[0] = ABS(input_arr[0] - order_arr[0]);
    for(i = 1; i < n; i++)
        row_arr[i] = MIN(row_arr[i - 1], ABS(input_arr[0] - order_arr[i]));
    for(i = 1; i < n; i++)
    {
        ans_arr[0] = row_arr[0] + ABS(input_arr[i] - order_arr[j]);
        for(j = 1; j < n; j++)
            ans_arr[j] = MIN(ans_arr[j - 1], row_arr[j] + ABS(input_arr[i] - order_arr[j]));
        for(j = 0; j < n; j++)
            row_arr[j] = ans_arr[j];
    }

    min = ans_arr[n - 1];
    printf("%d\n", min);
    return 0;
}


#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(v) ((v) > 0 ? (v) : -(v))

long long row_arr[5002] = {0};
long long ans_arr[5002] = {0};
int input_arr[5002] = {0};
int order_arr[5002] = {0};
int compare(const void* a, const void* b)
{
    return (*(int *)a - *(int* )b);
}

int main()
{
    int n, i, j, min;
 //   freopen("a.txt", "r", stdin);
    scanf("%d", &n);
    for(i = 0; i < n; i++)
    {
        scanf("%d", &input_arr[i]);
        order_arr[i] = input_arr[i];
    }
    qsort(order_arr, n, sizeof(int), compare);
    row_arr[0] = ABS(input_arr[0] - order_arr[0]);
    for(i = 1; i < n; i++)
        row_arr[i] = MIN(row_arr[i - 1], ABS((long long)input_arr[0] - (long long)order_arr[i]));
    for(i = 1; i < n; i++)
    {
        ans_arr[0] = row_arr[0] + ABS(input_arr[i] - order_arr[0]);
        for(j = 1; j < n; j++)
            ans_arr[j] = MIN(ans_arr[j - 1], row_arr[j] + ABS((long long)input_arr[i] - (long long)order_arr[j]));
        for(j = 0; j < n; j++)
            row_arr[j] = ans_arr[j];
    }

    min = ans_arr[n - 1];
    printf("%d\n", min);
    return 0;
}


#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define MIN(a, b) ((a) < (b) ? (a) : (b))
//#define abs(v) ((v) > 0 ? (v) : -(v))

long long row_arr[5002] = {0};
long long ans_arr[5002] = {0};
int input_arr[5002] = {0};
int order_arr[5002] = {0};
int compare(const void* a, const void* b)
{
    return (*(int *)a - *(int* )b);
}

int main()
{
    int n, i, j, min;
    //freopen("a.txt", "r", stdin);
    scanf("%d", &n);
    for(i = 0; i < n; i++)
    {
        scanf("%d", &input_arr[i]);
        order_arr[i] = input_arr[i];
    }
    qsort(order_arr, n, sizeof(int), compare);
    row_arr[0] = abs(input_arr[0] - order_arr[0]);
    for(i = 1; i < n; i++)
        row_arr[i] = MIN(row_arr[i - 1], abs(input_arr[0] - order_arr[i]));
    for(i = 1; i < n; i++)
    {
        ans_arr[0] = row_arr[0] + abs(input_arr[i] - order_arr[0]);
        for(j = 1; j < n; j++)
            ans_arr[j] = MIN(ans_arr[j - 1], row_arr[j] + abs(input_arr[i] - order_arr[j]));
        for(j = 0; j < n; j++)
            row_arr[j] = ans_arr[j];
    }

    min = ans_arr[n - 1];
    printf("%d\n", min);
    return 0;
}


#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(v) ((v) > 0 ? (v) : -(v))

long long row_arr[1][5002] = {0};
long long col_arr[5002][1] = {0};
long long ans_arr[5002];
int input_arr[5002] = {0};
int order_arr[5002] = {0};
int compare(const void* a, const void* b)
{
    return (*(int *)a - *(int* )b);
}

int main()
{
    int n, i, j, min;
    //freopen("a.txt", "r", stdin);
    scanf("%d", &n);
    for(i = 0; i < n; i++)
    {
        scanf("%d", &input_arr[i]);
        order_arr[i] = input_arr[i];
    }
    qsort(order_arr, n, sizeof(int), compare);
    row_arr[0][0] = ABS(input_arr[0] - order_arr[0]);
    col_arr[0][0] = ABS(input_arr[0] - order_arr[0]);
    for(i = 1; i < n; i++)
    {
        //init first line
        row_arr[0][i] = MIN(row_arr[0][i - 1], ABS(input_arr[0] - order_arr[i]));
        col_arr[i][0] = ABS(input_arr[i] - order_arr[0]) + col_arr[i - 1][0];
    }
    for(i = 1; i < n; i++)
    {
        ans_arr[0] = col_arr[i][0];
        for(j = 1; j < n; j++)
            ans_arr[j] = MIN(ans_arr[j - 1], row_arr[0][j] + ABS(input_arr[i] - order_arr[j]));
        for(j = 0; j < n; j++)
            row_arr[0][j] = ans_arr[j];
    }

    min = ans_arr[n - 1];
    printf("%d\n", min);
    return 0;
}


#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(v) ((v) > 0 ? (v) : -(v))

int row_arr[1][5002] = {0};
int col_arr[5002][1] = {0};
int ans_arr[5002];
int input_arr[5002] = {0};
int order_arr[5002] = {0};
int compare(const void* a, const void* b)
{
    return (*(int *)a - *(int* )b);
}

int main()
{
    int n, i, j, min;
    //freopen("a.txt", "r", stdin);
    scanf("%d", &n);
    for(i = 0; i < n; i++)
    {
        scanf("%d", &input_arr[i]);
        order_arr[i] = input_arr[i];
    }
    qsort(order_arr, n, sizeof(int), compare);
    row_arr[0][0] = ABS(input_arr[0] - order_arr[0]);
    col_arr[0][0] = ABS(input_arr[0] - order_arr[0]);
    for(i = 1; i < n; i++)
    {
        //init first line
        row_arr[0][i] = MIN(row_arr[0][i - 1], ABS(input_arr[0] - order_arr[i]));
        col_arr[i][0] = ABS(input_arr[i] - order_arr[0]) + col_arr[i - 1][0];
    }
    for(i = 1; i < n; i++)
    {
        ans_arr[0] = col_arr[i][0];
        for(j = 1; j < n; j++)
            ans_arr[j] = MIN(ans_arr[j - 1], row_arr[0][j] + ABS(input_arr[i] - order_arr[j]));
        for(j = 0; j < n; j++)
            row_arr[0][j] = ans_arr[j];
    }

    min = ans_arr[n - 1];
    printf("%d\n", min);
    return 0;
}


#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(v) ((v) > 0 ? (v) : -(v))

int arr[5002][5002] = {0};
int input_arr[5002] = {0};
int order_arr[5002] = {0};
int compare(const void* a, const void* b)
{
    return (*(int *)a - *(int* )b);
}

int main()
{
    int n, i, j, min;
    //freopen("a.txt", "r", stdin);
    scanf("%d", &n);
    for(i = 0; i < n; i++)
    {
        scanf("%d", &input_arr[i]);
        order_arr[i] = input_arr[i];
    }
    qsort(order_arr, n, sizeof(int), compare);
    arr[0][0] = ABS(input_arr[0] - order_arr[0]);
    for(i = 1; i < n; i++)
    {
        //init first line
        arr[0][i] = MIN(arr[0][i - 1], ABS(input_arr[0] - order_arr[i]));
        arr[i][0] = ABS(input_arr[i] - order_arr[0]) + arr[i - 1][0];
    }
    for(i = 1; i < n; i++)
    {
        for(j = 1; j < n; j++)
            arr[i][j] = MIN(arr[i][j - 1], arr[i - 1][j] + ABS(input_arr[i] - order_arr[j]));
    }

    min = arr[n - 1][0];
    for(i = 1; i < n; i++)
    {
        if(min > arr[n - 1][i])
            min = arr[n - 1][i];
    }
    printf("%d\n", min);
    return 0;
}


#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(v) ((v) > 0 ? (v) : -(v))

unsigned short arr[5002][5002] = {0};
int input_arr[5002] = {0};
int order_arr[5002] = {0};
int compare(const void* a, const void* b)
{
    return (*(int *)a - *(int* )b);
}

int main()
{
    int n, i, j, min;
    //freopen("a.txt", "r", stdin);
    scanf("%d", &n);
    for(i = 0; i < n; i++)
    {
        scanf("%d", &input_arr[i]);
        order_arr[i] = input_arr[i];
    }
    qsort(order_arr, n, sizeof(int), compare);
    arr[0][0] = ABS(input_arr[0] - order_arr[0]);
    for(i = 1; i < n; i++)
    {
        //init first line
        arr[0][i] = MIN(arr[0][i - 1], ABS(input_arr[0] - order_arr[i]));
        arr[i][0] = ABS(input_arr[i] - order_arr[0]) + arr[i - 1][0];
    }
    for(i = 1; i < n; i++)
    {
        for(j = 1; j < n; j++)
            arr[i][j] = MIN(arr[i][j - 1], arr[i - 1][j] + ABS(input_arr[i] - order_arr[j]));
    }

    min = arr[n - 1][0];
    for(i = 1; i < n; i++)
    {
        if(min > arr[n - 1][i])
            min = arr[n - 1][i];
    }
    printf("%d\n", min);
    return 0;
}


#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define MIN(a, b) ((a) < (b) ? (a) : (b))
//#define abs(v) ((v) > 0 ? (v) : -(v))

long long row_arr[5002] = {0};
long long ans_arr[5002] = {0};
int input_arr[5002] = {0};
int order_arr[5002] = {0};
int compare(const void* a, const void* b)
{
    return (*(int *)a - *(int* )b);
}

int main()
{
    int n, i, j;
    long long min;
    //freopen("a.txt", "r", stdin);
    scanf("%d", &n);
    for(i = 0; i < n; i++)
    {
        scanf("%d", &input_arr[i]);
        order_arr[i] = input_arr[i];
    }
    qsort(order_arr, n, sizeof(int), compare);
    row_arr[0] = abs(input_arr[0] - order_arr[0]);
    for(i = 1; i < n; i++)
        row_arr[i] = MIN(row_arr[i - 1], abs(input_arr[0] - order_arr[i]));
    for(i = 1; i < n; i++)
    {
        ans_arr[0] = row_arr[0] + abs(input_arr[i] - order_arr[0]);
        for(j = 1; j < n; j++)
            ans_arr[j] = MIN(ans_arr[j - 1], row_arr[j] + abs(input_arr[i] - order_arr[j]));
        for(j = 0; j < n; j++)
            row_arr[j] = ans_arr[j];
    }

    min = ans_arr[n - 1];
    printf("%I64d\n", min);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long i64;
i64 dp[5001][5001];
map<int,int>mp;
vector<int>b;
int a[5001];
i64 solve(int n,int k)
{
  if(n==0 || k==0)return 0;
   i64 &ret=dp[n][k];
   if(ret==-1)
   {
    if(k==1)
      {
       ret=0;
          ret+=abs(a[n]-b[k-1])+solve(n-1,k);
      }
      else
      {
           i64 A=solve(n,k-1);
            i64 B=abs(a[n]-b[k-1])+min(solve(n-1,k),solve(n-1,k-1));
        ret=min(A,B);
      }

   }
   return ret;

}
int main()
{
    int n;
  scanf("%d",&n);
   for(int i=1;i<=n;i++)
   {

     scanf("%d",&a[i]);
     if(mp[a[i]]==0){mp[a[i]]=1;b.push_back(a[i]);
     }
   }
    memset(dp,-1,sizeof dp);
    sort(b.begin(),b.end());
     cout<<solve(n,b.size());
   return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long i64;
bool visit[5001][5001];
map<int,int>mp;
vector<int>b;
map<pair<int,int>,i64>dp;
int a[5001];
i64 solve(int n,int k)
{
  if(n==0 || k==0)return 0;
     i64 ret;

   if(visit[n][k]==0)
   {
       visit[n][k]=true;
    if(k==1)
      {
       ret=0;
          ret+=abs(a[n]-b[k-1])+solve(n-1,k);
      }
      else
      {
           i64 A=solve(n,k-1);
            i64 B=abs(a[n]-b[k-1])+min(solve(n-1,k),solve(n-1,k-1));
        ret=min(A,B);
      }
     dp[make_pair(n,k)]=ret;
     return ret;
   }
   return dp[make_pair(n,k)];

}
int main()
{
    int n;
  scanf("%d",&n);
   for(int i=1;i<=n;i++)
   {

     scanf("%d",&a[i]);
     if(mp[a[i]]==0){mp[a[i]]=1;b.push_back(a[i]);
     }
   }
//    memset(dp,-1,sizeof dp);
    sort(b.begin(),b.end());
     cout<<solve(n,b.size());
   return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long i64;
bool visit[5001][5001];
map<int,int>mp;
vector<int>b;
map<pair<int,int>,int>dp;
int a[5001];
int solve(int n,int k)
{
  if(n==0 || k==0)return 0;
     int ret;

   if(visit[n][k]==0)
   {
       visit[n][k]=true;
    if(k==1)
      {
       ret=0;
          ret+=abs(a[n]-b[k-1])+solve(n-1,k);
      }
      else
      {
           int A=solve(n,k-1);
            int B=abs(a[n]-b[k-1])+min(solve(n-1,k),solve(n-1,k-1));
        ret=min(A,B);
      }
     dp[make_pair(n,k)]=ret;
     return ret;
   }
   return dp[make_pair(n,k)];

}
int main()
{
    int n;
  scanf("%d",&n);
   for(int i=1;i<=n;i++)
   {

     scanf("%d",&a[i]);
     if(mp[a[i]]==0){mp[a[i]]=1;b.push_back(a[i]);
     }
   }
//    memset(dp,-1,sizeof dp);
    sort(b.begin(),b.end());
     cout<<solve(n,b.size());
   return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long i64;
bool visit[5001][5001];
map<int,int>mp;
vector<int>b;
map<pair<int,int>,i64>dp;
int a[5001];
i64 solve(int n,int k)
{
  if(n==0 || k==0)return 0;
     i64 ret;

    if(k==1)
      {
       ret=0;
          ret+=abs(a[n]-b[k-1])+solve(n-1,k);
          return ret;
      }
      else
      {
            i64 B=min(solve(n,k-1),solve(n-1,k)+abs(a[n]-b[k-1]));
            return B;
      }
}
int main()
{
    int n;
  scanf("%d",&n);
   for(int i=1;i<=n;i++)
   {

     scanf("%d",&a[i]);
     if(mp[a[i]]==0){mp[a[i]]=1;b.push_back(a[i]);
     }
   }
//    memset(dp,-1,sizeof dp);
    sort(b.begin(),b.end());
     cout<<solve(n,b.size());
   return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long i64;
i64 dp[3][5005];
int main()
{
    int n;
    scanf("%d",&n);
    i64 data[n+50];
    vector<i64>lis;
    for(int i=0;i<n;i++)
    {
       cin>>data[i];
       lis.push_back(data[i]);
    }
    sort(lis.begin(),lis.end());
     for(int i=0;i<n;i++)
        dp[0][i]=1LL<<60;
    for(int i=0;i<n;i++)
    {
        //cout<<lis[i]<<"...::: ";
         for(int j=0;j<n;j++)
         {
             if(j)dp[0][j]=min((abs(lis[i]-data[j])+dp[0][j-1]),dp[0][j]);
             else dp[0][j]=min(dp[0][j],abs(data[j]-lis[i]));
            //cout<<dp[0][j]<<" ";
         }
         //cout<<endl<<endl;
    }
    cout<<dp[0][n-1]<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long i64;
int dp[5001][5001];
map<int,int>mp;
vector<int>b;
int a[5001];
int solve(int n,int k)
{
  if(n==0 || k==0)return 0;
   int &ret=dp[n][k];
   if(ret==-1)
   {
    if(k==1)
      {
       ret=0;
          ret+=abs(a[n]-b[k-1])+solve(n-1,k);
      }
      else
      {
           int A=solve(n,k-1);
            int B=abs(a[n]-b[k-1])+min(solve(n-1,k),solve(n-1,k-1));
        ret=min(A,B);
      }

   }
   return ret;

}
int main()
{
    int n;
  scanf("%d",&n);
   for(int i=1;i<=n;i++)
   {

     scanf("%d",&a[i]);
     if(mp[a[i]]==0){mp[a[i]]=1;b.push_back(a[i]);
     }
   }
    memset(dp,-1,sizeof dp);
    sort(b.begin(),b.end());
     cout<<solve(n,b.size());
   return 0;
}


#include<cstdio>
#include<queue>
using namespace std;

priority_queue<int> q;

int main(void)
{
    int n;
    long long ans=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        int a;
        scanf("%d",&a);
        q.push(a);
        if(a<q.top())
        {
            ans+=q.top()-a;
            q.pop();
            q.push(a);
        }
    }
    printf("%lld",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
priority_queue<long long>q;
inline long long read(){
   long long s=0,w=1;
   char ch=getchar();
   while(ch<='0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}
int main()
{
	long long n,x,ans=0;
	n=read();
	for(int i=1;i<=n;i++){
		x=read();
		q.push(x);
		if(x<q.top()){
			ans+=q.top()-x;
			q.pop(); q.push(x);
		}
	}
	printf("%lld",ans);
}


#include <iostream>
#include <queue>
using namespace std;
long long n,a,ans;
priority_queue <int> q;
int main(){
	cin>>n;
	cin>>a;
	q.push(a);
	for (int i=1;i<n;i++) {
		cin>>a;
		q.push(a);
		if (q.top()>a) {
			ans+=q.top()-a;
			q.pop();
			q.push(a);
		}
	}
	cout<<ans<<endl;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;

priority_queue <int> Q;
int n;
long long ans;

int main() {
	scanf("%d",&n);
	int a;
	scanf("%d",&a);
	Q.push(a);
	for (int i=1;i<n;i++) {
		scanf("%d",&a);
		Q.push(a);
		int opt=Q.top();
		if (opt>a) {
			ans+=opt-a;
			Q.pop();
			Q.push(a);
		}
	}
	printf("%lld",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const long long inf=1000000000;
long long n,m,ans=inf;
long long a[5010],f[3][5010],w[5010];
void solve( ){

	for(register long long i=1;i<=n;++i){
		long long k=i%2;
		for(register long long j=1;j<=n;++j){
			if(j==1)f[k][j]=f[1-k][j]+abs(w[j]-a[i]);
			else f[k][j]=min(f[k][j-1],f[1-k][j]+abs(w[j]-a[i]));
		}
	}
	ans=min(ans,f[n%2][n]);
}

int main( ){
	long long k,x,y;
	scanf("%lld",&n);
	for(register long long i=1;i<=n;++i){
		scanf("%lld",&a[i]);
		w[i]=a[i];
	}
	sort(w+1,w+n+1);
	solve( );
	printf("%lld\n",ans);
	return 0;
}


#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>

using namespace std;

#define MAXN 5005
#define INF 0x3f3f3f3f

typedef long long LL;

LL dp[MAXN],a[MAXN],b[MAXN];

int main()
{
    int n;
    scanf("%d",&n);
    for (int i=1;i<=n;i++) scanf("%I64d",&a[i]),b[i]=a[i];
    sort(b+1,b+n+1);
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
        {
            if (j==1) dp[j]+=abs(a[i]-b[j]);
            else
                dp[j]=min(dp[j-1],dp[j]+abs(a[i]-b[j]));
        }
    printf("%I64d\n",dp[n]);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<vector>
#include<queue>
#include<algorithm>
#include<string>
#include<sstream>
#include<cctype>
#include<cmath>
#include<iomanip>
#include<map>
#include<stack>
#include<set>
#include<functional>
#define in(x) x=read()
#define qr read()
#define inf 1000000005
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline int read()
{
	char ch=' ';
	int f=1,x=0;
	while(!isdigit(ch)){ch=getchar();if(ch=='-')f*=-1;}
	while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
	return f*x;
}
int a[100010],b[100010],dp[3][100010];
int main()
{
	int n=qr;
	for(int i=1;i<=n;i++)in(a[i]),b[i]=a[i];
	sort(b+1,b+1+n);
	int now=unique(b+1,b+1+n)-b-1;
//	cout<<now<<endl;
	for(int i=1;i<=now;i++)dp[1][i]=abs(b[i]-a[1]),dp[2][i]=inf;
	int noww=0;
	for(int i=2;i<=n;i++)
	{
//		cout<<i<<endl;
		int nowmin=inf;
		for(int j=1;j<=now;j++)
		{
			nowmin=min(nowmin,dp[noww+1][j]);
			dp[(noww^1)+1][j]=min(dp[(noww^1)+1][j],nowmin+abs(b[j]-a[i]));
		}
		noww^=1;
		for(int j=1;j<=now;j++)dp[(noww^1)+1][j]=inf;
//		cout<<i<<endl;
	}
//	cout<<noww<<endl;
//	for(int i=1;i<=n;i++)
//	{
//		for(int j=1;j<=2;j++)
//		{
//			cout<<dp[i][j]<<" ";
//		}
//		cout<<endl;
//	}
	int ans=inf;
	for(int i=1;i<=now;i++)ans=min(ans,dp[noww+1][i]);//,cout<<ans<<endl;;
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[500001];
int f[500001]={0};
int main()
{
	int n,minn=1e18;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]),minn=min(minn,a[i]);
	a[0]=minn;
	for(int i=1;i<=n;i++)
	{
		if(a[i]>=a[i-1])f[i]=f[i-1];
		else f[i]=f[i-1]+a[i-1]-a[i];
		for(int j=i-1;j>=0;j--)
			if(a[i]<a[j])f[j]=f[j]+a[j]-a[i];
			else f[j]=f[j]+a[i]-a[j];
	}
	int ans=1e18;
	for(int i=1;i<=n;i++)
		ans=min(ans,f[i]);
	printf("%d",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,a[5010],b[5010],f[5010];
int main()
{
    scanf("%lld",&n);
    for(int i=1;i<=n;i++) scanf("%lld",&a[i]),b[i]=a[i];
    sort(b+1,b+n+1);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
        	if(j==1) f[j]+=abs(b[j]-a[i]);
            else f[j]=min(f[j-1],f[j]+abs(b[j]-a[i]));
        }
    printf("%lld",f[n]);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long n,a[5001],b[5001],f[5001];
int main()
{
    scanf("%lld",&n);
    for(int i=1;i<=n;i++) scanf("%lld",&a[i]),b[i]=a[i];
    sort(b+1,b+n+1);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if(j==1)f[j]+=abs(b[j]-a[i]);
            else f[j]=min(f[j-1],f[j]+abs(b[j]-a[i]));
    printf("%lld",f[n]);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll inf=1ll<<62;
ll dp[2][5007],a[5007],b[5007];
ll min(ll x,ll y)
{
    return x<y?x:y;
}
int main()
{
    ll n;
    while(scanf("%I64d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            b[i]=a[i];
        }
        sort(b+1,b+n+1);
        int tot=unique(b+1,b+n+1)-b-1;
        dp[1][1]=inf;
        for(int i=1;i<=tot;i++)
        {
            dp[1&1][i]=fabs(b[i]-a[1]);
            dp[2&1][i]=inf;
        }
        for(int i=2;i<=n;i++)
        {
             ll minn=inf;
             for(int j=1;j<=tot;j++)
             {
                 minn=min(minn,dp[(i-1)&1][j]);
                 dp[i&1][j]=min(dp[i&1][j],minn+fabs(b[j]-a[i]));
             }
             for(int j=1;j<=tot;j++)dp[(i+1)&1][j]=inf;
        }
        ll ans=inf;
        for(int i=1;i<=tot;i++)ans=min(ans,dp[n&1][i]);
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<iomanip>
#include<cstdio>
#include<vector>
///#include<cmath>
#include<bitset>
#include<set>
#include<map>
#include<string>
#define inf 2000000000
#define mod 1000000007
using namespace std;
int n;
int a[5003];
int b[5003];
int c[5003];
void mergesort(int *a,int *b,int n)
{
    int i,i0,i1,iRight,iEnd,width,j;
    for(width=1;width<n;width*=2)
        for(i=0;i+width<n;i+=width*2)
        {
            i0=i;
            i1=iRight=i+width;
            iEnd=min(i+width*2,n);
            for(j=i;i0<iRight;j++)
                if(i1==iEnd || a[i0]<a[i1])
                    b[j]=a[i0++];
                else
                    b[j]=a[i1++];
            for(j=i;j<i1;j++)
                a[j]=b[j];
        }
}
long long old_dp[5003];
long long dp[5003];
int modu(int x)
{
    if(x<0)
        return -x;
    return x;
}
int main()
{
    scanf("%d",&n);
    int i,j;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        b[i]=a[i];
    }
    mergesort(b,c,n);
    old_dp[0]=modu(a[0]-b[0]);
    for(i=1;i<n;i++)
        old_dp[i]=min(old_dp[i-1],modu(a[0]-b[i]));
    for(i=1;i<n-1;i++)
    {
        dp[0]=old_dp[0]+modu(a[i]-b[0]);
        for(j=1;j<n;j++)
            dp[j]=min(dp[j-1],old_dp[j]+modu(a[i]-b[j]));
        for(j=0;j<n;j++)
            old_dp[j]=dp[j];
    }
    printf("%I64d\n",old_dp[n-1]);
    return 0;
}
/*

*/


#include<iostream>
#include<algorithm>
#include<iomanip>
#include<cstdio>
#include<vector>
///#include<cmath>
#include<bitset>
#include<set>
#include<map>
#include<string>
#define inf 2000000000
#define mod 1000000007
using namespace std;
int n;
int a[5003];
int b[5003];
int c[5003];
void mergesort(int *a,int *b,int n)
{
    int i,i0,i1,iRight,iEnd,width,j;
    for(width=1;width<n;width*=2)
        for(i=0;i+width<n;i+=width*2)
        {
            i0=i;
            i1=iRight=i+width;
            iEnd=min(i+width*2,n);
            for(j=i;i0<iRight;j++)
                if(i1==iEnd || a[i0]<a[i1])
                    b[j]=a[i0++];
                else
                    b[j]=a[i1++];
            for(j=i;j<i1;j++)
                a[j]=b[j];
        }
}
long long old_dp[5003];
long long dp[5003];
int modu(int x)
{
    if(x<0)
        return -x;
    return x;
}
int main()
{
    scanf("%d",&n);
    int i,j;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        b[i]=a[i];
    }
    mergesort(b,c,n);
    old_dp[0]=modu(a[0]-b[0]);
    for(i=1;i<n;i++)
        old_dp[i]=min(old_dp[i-1],(long long)modu(a[0]-b[i]));
    for(i=1;i<n-1;i++)
    {
        dp[0]=old_dp[0]+modu(a[i]-b[0]);
        for(j=1;j<n;j++)
            dp[j]=min(dp[j-1],old_dp[j]+modu(a[i]-b[j]));
        for(j=0;j<n;j++)
            old_dp[j]=dp[j];
    }
    printf("%I64d\n",old_dp[n-1]);
    return 0;
}
/*

*/


#include<iostream>
#include<algorithm>
#include<iomanip>
#include<cstdio>
#include<vector>
///#include<cmath>
#include<bitset>
#include<set>
#include<map>
#include<string>
#define inf 2000000000
#define mod 1000000007
using namespace std;
int n;
int a[5003];
int b[5003];
int c[5003];
void mergesort(int *a,int *b,int n)
{
    int i,i0,i1,iRight,iEnd,width,j;
    for(width=1;width<n;width*=2)
        for(i=0;i+width<n;i+=width*2)
        {
            i0=i;
            i1=iRight=i+width;
            iEnd=min(i+width*2,n);
            for(j=i;i0<iRight;j++)
                if(i1==iEnd || a[i0]<a[i1])
                    b[j]=a[i0++];
                else
                    b[j]=a[i1++];
            for(j=i;j<i1;j++)
                a[j]=b[j];
        }
}
long long old_dp[5003];
long long dp[5003];
int modu(int x)
{
    if(x<0)
        return 0;
    return x;
}
int main()
{
    scanf("%d",&n);
    int i,j;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        b[i]=a[i];
    }
    mergesort(b,c,n);
    for(i=0;i<n;i++)
        old_dp[i]=modu(a[0]-b[i]);
    for(i=1;i<n;i++)
    {
        dp[0]=old_dp[0]+modu(a[i]-b[0]);
        for(j=1;j<n;j++)
            dp[j]=min(old_dp[j-1]+modu(b[j-1]-a[i]),old_dp[j]+modu(b[j]-a[i]))+modu(a[i]-b[j]);
        for(j=0;j<n;j++)
            old_dp[j]=dp[j];
    }
    printf("%I64d\n",old_dp[n-1]);
    return 0;
}
/*

*/


#include<iostream>
#include<algorithm>
#include<iomanip>
#include<cstdio>
#include<vector>
///#include<cmath>
#include<bitset>
#include<set>
#include<map>
#include<string>
#define inf 2000000000
#define mod 1000000007
using namespace std;
int n;
int a[5003];
int b[5003];
int c[5003];
void mergesort(int *a,int *b,int n)
{
    int i,i0,i1,iRight,iEnd,width,j;
    for(width=1;width<n;width*=2)
        for(i=0;i+width<n;i+=width*2)
        {
            i0=i;
            i1=iRight=i+width;
            iEnd=min(i+width*2,n);
            for(j=i;i0<iRight;j++)
                if(i1==iEnd || a[i0]<a[i1])
                    b[j]=a[i0++];
                else
                    b[j]=a[i1++];
            for(j=i;j<i1;j++)
                a[j]=b[j];
        }
}
long long old_dp[5003];
long long dp[5003];
int modu(int x)
{
    if(x<0)
        return -x;
    return x;
}
int main()
{
    scanf("%d",&n);
    int i,j;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        b[i]=a[i];
    }
    mergesort(b,c,n);
    for(i=0;i<n;i++)
        old_dp[i]=modu(a[0]-b[i]);
    for(i=1;i<n;i++)
    {
        dp[0]=old_dp[0]+modu(a[i]-b[0]);
        for(j=1;j<n;j++)
            dp[j]=min(dp[j-1],old_dp[j]+modu(a[i]-b[j]));
        for(j=0;j<n;j++)
            old_dp[j]=dp[j];
    }
    printf("%I64d\n",old_dp[n-1]);
    return 0;
}
/*

*/


#include<iostream>
#include<algorithm>
#include<iomanip>
#include<cstdio>
#include<vector>
///#include<cmath>
#include<bitset>
#include<set>
#include<map>
#include<string>
#define inf 2000000000
#define mod 1000000007
using namespace std;
int n;
int a[5003];
int b[5003];
int c[5003];
void mergesort(int *a,int *b,int n)
{
    int i,i0,i1,iRight,iEnd,width,j;
    for(width=1;width<n;width*=2)
        for(i=0;i+width<n;i+=width*2)
        {
            i0=i;
            i1=iRight=i+width;
            iEnd=min(i+width*2,n);
            for(j=i;i0<iRight;j++)
                if(i1==iEnd || a[i0]<a[i1])
                    b[j]=a[i0++];
                else
                    b[j]=a[i1++];
            for(j=i;j<i1;j++)
                a[j]=b[j];
        }
}
long long old_dp[5003];
long long dp[5003];
int modu(int x)
{
    if(x<0)
        return -x;
    return x;
}
int main()
{
    scanf("%d",&n);
    int i,j;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        b[i]=a[i];
    }
    mergesort(b,c,n);
    old_dp[0]=modu(a[0]-b[0]);
    for(i=1;i<n;i++)
        old_dp[i]=min(old_dp[i-1],(long long)modu(a[0]-b[i]));
    for(i=1;i<n;i++)
    {
        dp[0]=old_dp[0]+modu(a[i]-b[0]);
        for(j=1;j<n;j++)
            dp[j]=min(dp[j-1],old_dp[j]+modu(a[i]-b[j]));
        for(j=0;j<n;j++)
            old_dp[j]=dp[j];
    }
    printf("%I64d\n",old_dp[n-1]);
    return 0;
}
/*

*/


#include<iostream>
#include<algorithm>
#include<iomanip>
#include<cstdio>
#include<vector>
///#include<cmath>
#include<bitset>
#include<set>
#include<map>
#include<string>
#define inf 2000000000
#define mod 1000000007
using namespace std;
int n;
int a[5003];
int b[5003];
int c[5003];
void mergesort(int *a,int *b,int n)
{
    int i,i0,i1,iRight,iEnd,width,j;
    for(width=1;width<n;width*=2)
        for(i=0;i+width<n;i+=width*2)
        {
            i0=i;
            i1=iRight=i+width;
            iEnd=min(i+width*2,n);
            for(j=i;i0<iRight;j++)
                if(i1==iEnd || a[i0]<a[i1])
                    b[j]=a[i0++];
                else
                    b[j]=a[i1++];
            for(j=i;j<i1;j++)
                a[j]=b[j];
        }
}
long long old_dp[5003];
long long dp[5003];
int modu(int x)
{
    if(x<0)
        return -x;
    return x;
}
int modu2(int x)
{
    if(x<0)
        return 0;
    return x;
}
int main()
{
    scanf("%d",&n);
    int i,j;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        b[i]=a[i];
    }
    mergesort(b,c,n);
    for(i=0;i<n;i++)
        old_dp[i]=modu(a[0]-b[i]);
    for(i=1;i<n-1;i++)
    {
        dp[0]=old_dp[0]+modu(a[i]-b[0]);
        for(j=1;j<n;j++)
            dp[j]=min(dp[j-1],old_dp[j]+modu(a[i]-b[j]));
        for(j=0;j<n;j++)
            old_dp[j]=dp[j];
    }
    dp[0]=old_dp[0]+modu2(b[0]-a[n-1]);
    for(j=1;j<n;j++)
        dp[j]=min(dp[j-1],old_dp[j]+modu2(b[j]-a[n-1]));
    printf("%I64d\n",dp[n-1]);
    return 0;
}
/*

*/


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
using namespace std;

long long d[2][5444];
long long x[5555];
long long s[5555];

inline long long mabs(long long a) {
	return (a < 0) ? -a : a;
}

int main() {
	//freopen("input.txt", "r", stdin);

	long long n; cin >> n;
	for (long long i=0; i<n; i++) {
		cin >> s[i];
	}

	for (long long i=0; i<n; i++) {
		x[i] = s[i];
	}

	sort(x, x+n);

	for (long long i=0; i<n; i++) {
		d[0][i] = mabs(s[0] - x[i]);
	}

	int cur;
	int prv;
	for (long long i=1; i<n; i++) {
		cur = i & 1;
		prv = 1 - cur;
		long long mn = d[prv][0];
		for (long long u=0; u<n; u++) {
			if (mn > d[prv][u]) {
				mn = d[prv][u];
			}
			d[cur][u] = mn + mabs(x[u] - s[i]);
		}
	}

	long long mn = 2123456789;

	for (long long i=0; i<n; i++) {
		if (d[cur][i] < mn) {
			mn = d[cur][i];
		}
	}

	cout << mn << endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
using namespace std;

long long d[2][5444];
long long x[5555];
long long s[5555];

inline long long mabs(long long a) {
	return (a < 0) ? -a : a;
}

int main() {
//	freopen("input.txt", "r", stdin);

	long long n; cin >> n;
	for (long long i=0; i<n; i++) {
		cin >> s[i];
	}

	for (long long i=0; i<n; i++) {
		x[i] = s[i];
	}

	sort(x, x+n);

	for (long long i=0; i<n; i++) {
		d[0][i] = mabs(s[0] - x[i]);
	}

	int cur;
	int prv;
	for (long long i=1; i<n; i++) {
		cur = i & 1;
		prv = 2 - cur;
		long long mn = d[prv][0];
		for (long long u=0; u<n; u++) {
			if (mn > d[prv][u]) {
				mn = d[prv][u];
			}
			d[cur][u] = mn + mabs(x[u] - s[i]);
		}
	}

	long long mn = 2123456789;

	for (long long i=0; i<n; i++) {
		if (d[cur][i] < mn) {
			mn = d[cur][i];
		}
	}

	cout << mn << endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
using namespace std;

long long d[5555][5444];
long long x[5555];
long long s[5555];

inline long long mabs(long long a) {
	return (a < 0) ? -a : a;
}

int main() {
//	freopen("input.txt", "r", stdin);

	long long n; cin >> n;
	for (long long i=0; i<n; i++) {
		cin >> s[i];
	}

	for (long long i=0; i<n; i++) {
		x[i] = s[i];
	}

	sort(x, x+n);

	for (long long i=0; i<n; i++) {
		d[0][i] = mabs(s[0] - x[i]);
	}

	for (long long i=1; i<n; i++) {
		long long mn = d[i-1][0];
		for (long long u=0; u<n; u++) {
			if (mn > d[i-1][u]) {
				mn = d[i-1][u];
			}
			d[i][u] = mn + mabs(x[u] - s[i]);
		}
	}

	long long mn = 2123456789;

	for (long long i=0; i<n; i++) {
		if (d[n-1][i] < mn) {
			mn = d[n-1][i];
		}
	}

	cout << mn << endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
using namespace std;

long long d[2][5444];
long long x[5555];
long long s[5555];

inline long long mabs(long long a) {
	return (a < 0) ? -a : a;
}

int main() {
	//freopen("input.txt", "r", stdin);

	long long n; cin >> n;
	for (long long i=0; i<n; i++) {
		cin >> s[i];
	}

	for (long long i=0; i<n; i++) {
		x[i] = s[i];
	}

	sort(x, x+n);

	for (long long i=0; i<n; i++) {
		d[0][i] = mabs(s[0] - x[i]);
	}

	int cur = 0;
	int prv;
	for (long long i=1; i<n; i++) {
		cur = i & 1;
		prv = 1 - cur;
		long long mn = d[prv][0];
		for (long long u=0; u<n; u++) {
			if (mn > d[prv][u]) {
				mn = d[prv][u];
			}
			d[cur][u] = mn + mabs(x[u] - s[i]);
		}
	}

	long long mn = 2123456789;

	for (long long i=0; i<n; i++) {
		if (d[cur][i] < mn) {
			mn = d[cur][i];
		}
	}

	cout << mn << endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
using namespace std;

long long d[2][5444];
long long x[5555];
long long s[5555];

inline long long mabs(long long a) {
	return (a < 0) ? -a : a;
}

int main() {
	//freopen("input.txt", "r", stdin);

	long long n; cin >> n;
	for (long long i=0; i<n; i++) {
		cin >> s[i];
	}

	for (long long i=0; i<n; i++) {
		x[i] = s[i];
	}

	sort(x, x+n);

	for (long long i=0; i<n; i++) {
		d[0][i] = mabs(s[0] - x[i]);
	}

	int cur = 0;
	int prv;
	for (long long i=1; i<n; i++) {
		cur = i & 1;
		prv = 1 - cur;
		long long mn = d[prv][0];
		for (long long u=0; u<n; u++) {
			if (mn > d[prv][u]) {
				mn = d[prv][u];
			}
			d[cur][u] = mn + mabs(x[u] - s[i]);
		}
	}

	long long mn = 21234567832423429LL;

	for (long long i=0; i<n; i++) {
		if (d[cur][i] < mn) {
			mn = d[cur][i];
		}
	}

	cout << mn << endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
using namespace std;

long long d[2][5444];
long long x[5555];
long long s[5555];

inline long long mabs(long long a) {
	return (a < 0) ? -a : a;
}

int main() {
	freopen("input.txt", "r", stdin);

	long long n; cin >> n;
	for (long long i=0; i<n; i++) {
		cin >> s[i];
	}

	for (long long i=0; i<n; i++) {
		x[i] = s[i];
	}

	sort(x, x+n);

	for (long long i=0; i<n; i++) {
		d[0][i] = mabs(s[0] - x[i]);
	}

	int cur = 0;
	int prv;
	for (long long i=1; i<n; i++) {
		cur = i & 1;
		prv = 1 - cur;
		long long mn = d[prv][0];
		for (long long u=0; u<n; u++) {
			if (mn > d[prv][u]) {
				mn = d[prv][u];
			}
			d[cur][u] = mn + mabs(x[u] - s[i]);
		}
	}

	long long mn = 21234567832423429LL;

	for (long long i=0; i<n; i++) {
		if (d[cur][i] < mn) {
			mn = d[cur][i];
		}
	}

	cout << mn << endl;

	return 0;
}


#include "iostream"
#include "cstring"
#include "cstdio"
#include "algorithm"
#include "cmath"
using namespace std;
#define LLD long long
const int N = 9000;
const LLD inf =(LLD) (1)<<50;
LLD dp[2][N],p[N],pm[N];
int cur,n;
LLD iabs(LLD t){
 if(t>0) return t;
 return -t;
}
void dyna(){
    int i,j,k,cur=1;
    LLD tmp;
     sort(pm+1,pm+n+1);
     dp[0][1]=0;
     for(i=1;i<=n;i++){
         tmp=inf;
         for(j=1;j<=n;j++){
            tmp=min(tmp,dp[1-cur][j]);
            dp[cur][j]=tmp+iabs(p[i]-pm[j]);
         }
         cur^=1;
     }
     LLD res=inf;
     for(i=1;i<=n;i++){
       if(dp[1-cur][i]<res) res=dp[1-cur][i];
     }
     cout<<res<<endl;
}
int main(){
   // freopen("R:\in2.txt","r",stdin);
  int i;
  int cases=1;
  while(scanf("%d",&n)!=EOF){
   //  if(n==-1) break;
     for(i=1;i<=n;i++){
      cin>>p[i];
      pm[i]=p[i];
     }
     dyna();
  }
}


#include "iostream"
#include "cstring"
#include "cstdio"
using namespace std;
const int N = 9000;
#define LLD long long
const LLD inf = ((LLD)1)<<60;
LLD dp[2][N],p[N],st[N];
int n,cur;
LLD iabs(LLD a){
    if(a>0) return a;
    return -a;
}
LLD min(LLD a,LLD b){
  if(a>b) return b;
  return a;
}
int main(){
    //freopen("R:\in.txt","r",stdin);
    int i,j,k;
    while(scanf("%d",&n)!=EOF){

        for(i=1;i<=n;i++){
            cin>>p[i];
            st[i]=p[i];
            //cout<<p[i]<<" ";
        }
        //cout<<endl;
        sort(st+1,st+n+1);
        memset(dp[0],0,sizeof(dp[0]));
        cur=1;
        for(i=1;i<=n;i++){
            cur=1-cur;
            LLD tmp=inf;
            for(j=1;j<=n;j++){
                tmp=min(tmp,dp[1-cur][j]);
                dp[cur][j]=tmp+iabs(p[i]-st[j]);
                //cout<<dp[cur][j]<<endl;
            }
        }
        LLD cnt=inf;
        for(i=1;i<=n;i++){
          //cout<<i<<" "<<dp[cur][i]<<endl;
          if(dp[cur][i]<cnt)
               cnt=dp[cur][i];
        }
        cout<<cnt<<endl;

    }
}


#include "iostream"
#include "cstring"
#include "cstdio"
#include "cmath"
#include "algorithm"
using namespace std;
#define LLD long long
const int N = 5005;
const long long inf = (LLD)(1)<<50;
int p[N];
LLD dp[2][N];
int n, m,len;
void dyna()
{
    int i, ii,t,j, k,cnt=0,tmp,a,b;
    LLD res=inf;
    int cur=0;
    for (ii=1;ii<=n;ii++)
    {
        t=p[ii];
        if (p[1]-t<0) continue;
        cur=1;
        for (i=0;i<=n;i++)
               dp[0][i]=inf;
        dp[0][ii]=p[1]-t;
      //  cout<<"dp[0][t]" <<dp[0][t]<<endl;
        int last=t;
        int lastid=ii;
        for (i=2;i<=n;i++) {
            /*for (j=0;j<=n;j++)
               dp[cur][j]=inf;
               */
            if (p[i]>last){
                j=p[i];
                k=i;
                dp[cur][k]=dp[1-cur][lastid]+0;
                last=j;
                lastid=k;
            }
            else{
                j=last;
                k=lastid;
                dp[cur][k]=dp[1-cur][k]+j-p[i];
            }
            //cout<<i<<" "<<j<<" "<<ii<<endl;
           /* if(i==n)
            printf("then r ii= %d  i= %d j= %d  %d\n",ii,i,j,dp[cur][k]);
            else
             printf("ii= %d  i= %d j= %d  %d\n",ii,i,j,dp[cur][k]);
            **/
            if(i==n) {
                if(dp[cur][k]<res)
                 res=dp[cur][k];
            }
            cur=1-cur;
        }
    }
    cout<<res<<endl;

}

int main()
{
  //  freopen("R:\in.txt","r",stdin);
    int i,T;
    while (scanf("%d",&n)!=EOF)
    {
       // if(n==-1) break;
        for (i=1;i<=n;i++){
            scanf("%d",&p[i]);
          //cout<<p[i]<<endl;
        }
        if(n==1){
            puts("0");
            continue;
        }
        dyna();
    }
}


#include "iostream"
#include "cstring"
#include "cstdio"
#include "algorithm"
#include "cmath"
using namespace std;
#define LLD long long
const int N = 9000;
const LLD inf =(LLD) (1)<<50;
LLD dp[2][N],p[N];
int cur,n;
LLD iabs(LLD t){
 if(t>0) return t;
 return -t;
}
void dyna(){
 cur=1;
 LLD res=inf;
 int i,j;
 memset(dp,0,sizeof(dp));
 for(i=1;i<=n;i++){
  for(j=1;j<=n;j++)  dp[cur][j]=inf;
  for(j=1;j<=n;j++){
   dp[cur][j]=min(dp[cur][j],dp[1-cur][j]+iabs(p[i]-p[j]));

  // printf("i= %d j = %d  dp[cur][j] = %lld\n",i,j,dp[cur][j]);
   if(p[i]>=p[j]){
   dp[cur][i]=min(dp[1-cur][j]+0,dp[cur][i]);
  //  printf("i= %d j = %d  dp[cur][i] = %lld\n",i,i,dp[cur][i]);
   }
  }
  cur=cur^1;
 }

 for(i=1;i<=n;i++){
   if(dp[1-cur][i]<res)
      res=dp[1-cur][i];
 }

 cout<<res<<endl;
}
int main(){
  //freopen("R:\in.txt","r",stdin);
  int i;
  while(scanf("%d",&n)!=EOF){
     if(n==-1) break;
     for(i=1;i<=n;i++){
      cin>>p[i];
     }
     dyna();
  }
}


#include "iostream"
#include "cstring"
#include "cstdio"
#include "cmath"
#include "algorithm"
using namespace std;
#define LLD long long
const int N = 5005;
const long long inf = (LLD)(1)<<50;
LLD p[N];
LLD dp[2][N];
int n, m,len;
void dyna()
{
   LLD i, ii,k,j;
    LLD t;
    LLD res=inf;
    int cur=0;
    for (ii=1;ii<=n;ii++)
    {
        t=p[ii];
        if (p[1]-t<0) continue;
        cur=1;
        for (i=0;i<=n;i++)
               dp[0][i]=inf;
        dp[0][ii]=p[1]-t;
       LLD last=t;
        LLD lastid=ii;
        for (i=2;i<=n;i++) {
            //memset(dp[cur],-1,sizeof(dp[cur]));
            if (p[i]>last&&i>=ii){
                j=p[i];
                k=i;
                dp[cur][k]=dp[1-cur][lastid]+0;
                last=j;
                lastid=k;
            }
            else{
                j=last;
                k=lastid;
                LLD tm=j-p[i];
                if(tm<0) tm=-tm;
                dp[cur][k]=dp[1-cur][k]+tm;
            }
            //cout<<i<<" "<<j<<" "<<ii<<endl;
          /*  if(i==n)
            printf("then r ii= %d  i= %d j= %d  %d\n",ii,i,j,dp[cur][k]);
            else
             printf("ii= %d  i= %d j= %d  %d\n",ii,i,j,dp[cur][k]);
             **/
            if(i==n) {
                if(dp[cur][k]>=0&&dp[cur][k]<res)
                 res=dp[cur][k];
            }
            cur=1-cur;
        }
    }
    cout<<res<<endl;

}

int main()
{
  //  freopen("R:\in.txt","r",stdin);
    int i,T;
    while (scanf("%d",&n)!=EOF)
    {
        for (i=1;i<=n;i++){
            cin>>p[i];
        }
        if(n==1){
            puts("0");
            continue;
        }
        dyna();
    }
}


#include "iostream"
#include "cstring"
#include "cstdio"
#include "cmath"
#include "algorithm"
using namespace std;
#define LLD long long
const int N = 5005;
const long long inf = (LLD)(1)<<50;
LLD p[N];
LLD dp[2][N];
int n, m,len;
void dyna()
{
   LLD i, ii,k,j;
    LLD t;
    LLD res=inf;
    int cur=0;
    for (ii=1;ii<=n;ii++)
    {
        t=p[ii];
        if (p[1]-t<0) continue;
        cur=1;
        for (i=0;i<=n;i++)
               dp[0][i]=inf;
        dp[0][ii]=p[1]-t;
        int last=t;
        int lastid=ii;
        for (i=2;i<=n;i++) {
            //memset(dp[cur],-1,sizeof(dp[cur]));
            if (p[i]>last&&i>=ii){
                j=p[i];
                k=i;
                dp[cur][k]=dp[1-cur][lastid]+0;
                last=j;
                lastid=k;
            }
            else{
                j=last;
                k=lastid;
                LLD tm=j-p[i];
                if(tm<0) tm=-tm;
                dp[cur][k]=dp[1-cur][k]+tm;
            }
            //cout<<i<<" "<<j<<" "<<ii<<endl;
          /*  if(i==n)
            printf("then r ii= %d  i= %d j= %d  %d\n",ii,i,j,dp[cur][k]);
            else
             printf("ii= %d  i= %d j= %d  %d\n",ii,i,j,dp[cur][k]);
             **/
            if(i==n) {
                if(dp[cur][k]>=0&&dp[cur][k]<res)
                 res=dp[cur][k];
            }
            cur=1-cur;
        }
    }
    cout<<res<<endl;

}

int main()
{
  //  freopen("R:\in.txt","r",stdin);
    int i,T;
    while (scanf("%d",&n)!=EOF)
    {
        for (i=1;i<=n;i++){
            scanf("%lld",&p[i]);
        }
        if(n==1){
            puts("0");
            continue;
        }
        dyna();
    }
}


/*
codeforece 13 sequence
给出一串数字n个 可以调整每个数字 代价为调整的差值
问用最小的代价 调整这个序列使得其实一个非下降序列的代价是多少
dp[i][j] 前i个数字 最后一个是pm[j]时的最小代价
一个注意点是：每个数字调整后肯定是该序列中的某个数
sample:
4
2 4 3 1
5
3 2 -1 2 11
5
2 1 1 1 1
**/
#include "iostream"
#include "cstring"
#include "cstdio"
#include "algorithm"
#include "cmath"
using namespace std;
#define LLD long long
const int N = 9000;
const LLD inf =(LLD) (1)<<50;
LLD dp[2][N],p[N],pm[N];
int cur,n;
LLD iabs(LLD t){
 if(t>0) return t;
 return -t;
}
void dyna(){
    int i,j,k,cur=1;
    LLD tmp;
    /*dp[i][j] 前i个数字 最后一个是pm[j]时的最小代价
    **/
     sort(pm+1,pm+n+1);
     //dp[0][1]=0;
     memset(dp[0],0,sizeof(dp[0]));
     for(i=1;i<=n;i++){
         tmp=inf;
         for(j=1;j<=n;j++){
             /*计算出之前的在1到j之间最小的那个数
                因为pm是有序的在1到j之前任何一个pm[j]状态都是能够转移到当前状态的
             **/
            tmp=min(tmp,dp[1-cur][j]);
            dp[cur][j]=tmp+iabs(p[i]-pm[j]);
         }
         cur^=1;
     }
     LLD res=inf;
     for(i=1;i<=n;i++){
       if(dp[1-cur][i]<res) res=dp[1-cur][i];
     }
     cout<<res<<endl;
}
int main(){
  int i;
  int cases=1;
  while(scanf("%d",&n)!=EOF){
      if(n==-1) break;
     for(i=1;i<=n;i++){
      cin>>p[i];
      pm[i]=p[i];
     }
     dyna();
  }
}


#include "iostream"
#include "cstring"
#include "cstdio"
#include "algorithm"
using namespace std;
const int N = 9000;
#define LLD long long
const LLD inf = ((LLD)1)<<60;
LLD dp[2][N],p[N],st[N];
int n,cur;
LLD iabs(LLD a){
    if(a>0) return a;
    return -a;
}
LLD min(LLD a,LLD b){
  if(a>b) return b;
  return a;
}
int main(){
    //freopen("R:\in.txt","r",stdin);
    int i,j,k;
    while(scanf("%d",&n)!=EOF){

        for(i=1;i<=n;i++){
            cin>>p[i];
            st[i]=p[i];
            //cout<<p[i]<<" ";
        }
        //cout<<endl;
        sort(st+1,st+n+1);
        memset(dp[0],0,sizeof(dp[0]));
        cur=1;
        for(i=1;i<=n;i++){
            cur=1-cur;
            LLD tmp=inf;
            for(j=1;j<=n;j++){
                tmp=min(tmp,dp[1-cur][j]);
                dp[cur][j]=tmp+iabs(p[i]-st[j]);
                //cout<<dp[cur][j]<<endl;
            }
        }
        LLD cnt=inf;
        for(i=1;i<=n;i++){
          //cout<<i<<" "<<dp[cur][i]<<endl;
          if(dp[cur][i]<cnt)
               cnt=dp[cur][i];
        }
        cout<<cnt<<endl;

    }
}


#include "iostream"
#include "cstring"
#include "cstdio"
#include "cmath"
#include "algorithm"
using namespace std;
#define LLD long long
const int N = 5005;
const long long inf = (LLD)(1)<<50;
int p[N];
LLD dp[2][N];
int n, m,len;
void dyna()
{
    int i, ii,t,j, k,cnt=0,tmp,a,b;
    LLD res=inf;
    int cur=0;
    for (ii=1;ii<=n;ii++)
    {
        t=p[ii];
        if (p[1]-t<0) continue;
        cur=1;
        for (i=0;i<=n;i++)
               dp[0][i]=inf;
        dp[0][ii]=p[1]-t;
      //  cout<<"dp[0][t]" <<dp[0][t]<<endl;
        int last=t;
        int lastid=ii;
        for (i=2;i<=n;i++) {
           /* for (j=0;j<=n;j++)
               dp[cur][j]=inf;**/

            if (p[i]>last&&i>=ii){
                j=p[i];
                k=i;
                dp[cur][k]=dp[1-cur][lastid]+0;
                last=j;
                lastid=k;
            }
            else{
                j=last;
                k=lastid;
                int tm=j-p[i];
                if(tm<0) tm=-tm;
                dp[cur][k]=dp[1-cur][k]+tm;
            }
            //cout<<i<<" "<<j<<" "<<ii<<endl;
          /*  if(i==n)
            printf("then r ii= %d  i= %d j= %d  %d\n",ii,i,j,dp[cur][k]);
            else
             printf("ii= %d  i= %d j= %d  %d\n",ii,i,j,dp[cur][k]);
             **/
            if(i==n) {
                if(dp[cur][k]>=0&&dp[cur][k]<res)
                 res=dp[cur][k];
            }
            cur=1-cur;
        }
    }
    cout<<res<<endl;

}

int main()
{
   // freopen("R:\in.txt","r",stdin);
    int i,T;
    while (scanf("%d",&n)!=EOF)
    {
      //  if(n==-1) break;
        for (i=1;i<=n;i++){
            scanf("%d",&p[i]);
          //cout<<p[i]<<endl;
        }
        if(n==1){
            puts("0");
            continue;
        }
        dyna();
    }
}


#include "iostream"
#include "cstring"
#include "cstdio"
#include "cmath"
#include "algorithm"
using namespace std;
const int N = 5005;
const int M = 105;
const int inf = 1 << 29;
int p[N];
int dp[2][N];
int n, m,len;
int min(int a, int b)
{
    return a < b ? a : b;
}
int max(int a,int b)
{
    return a>b?a:b;
}
void dyna()
{
    int i, ii,t,j, k,cnt=0,res=inf,tmp,a,b;
    int cur=0;
    for (ii=1;ii<=n;ii++)
    {
        if (p[1]-t<0) continue;
        cur=1;
        for (i=1;i<=n;i++)
               dp[0][i]=inf;
        t=p[ii];
        dp[0][ii]=abs(t-p[1]);
        int last=t;
        int lastid=ii;
        for (i=2;i<=n;i++) {
            if (p[i]>last){
                j=p[i];
                k=i;
                dp[cur][k]=dp[1-cur][lastid]+abs(p[i]-j);
                last=j;
                lastid=k;
            }
            else{
                j=last;
                k=lastid;
                dp[cur][k]=dp[1-cur][k]+abs(p[i]-j);
            }
           //printf("ii= %d  i= %d j= %d  %d \n",ii,i,j,dp[i][j]);
            if(i==n) res=min(res,dp[cur][k]);
            cur=1-cur;
        }
    }
    cout<<res<<endl;

}

int main()
{
    //freopen("R:\in.txt","r",stdin);
    int i,T;
    while (scanf("%d",&n)!=EOF)
    {
        for (i=1;i<=n;i++)
            scanf("%d",&p[i]);
        dyna();
    }
}


#include "iostream"
#include "cstring"
#include "cstdio"
#include "cmath"
#include "algorithm"
using namespace std;
#define LLD long long
const int N = 5005;
const long long inf = (LLD)(1)<<50;
int p[N];
LLD dp[2][N];
int n, m,len;
/*
int min(int a, int b){
    return a < b ? a : b;
}
int max(int a,int b){
    return a>b?a:b;
}**/
void dyna()
{
    int i, ii,t,j, k,cnt=0,tmp,a,b;
    LLD res=inf;
    //cout<<res<<endl;
    int cur=0;
    for (ii=1;ii<=n;ii++)
    {
        t=p[ii];
        if (p[1]-t<0) continue;
        cur=1;
        for (i=1;i<=n;i++)
               dp[0][i]=inf;
        dp[0][ii]=p[1]-t;
      //  cout<<"dp[0][t]" <<dp[0][t]<<endl;
        int last=t;
        int lastid=ii;
        for (i=2;i<=n;i++) {
            if (p[i]>last){
                j=p[i];
                k=i;
                dp[cur][k]=dp[1-cur][lastid]+0;
                last=j;
                lastid=k;
            }
            else{
                j=last;
                k=lastid;
                dp[cur][k]=dp[1-cur][k]+j-p[i];
            }
            //cout<<i<<" "<<j<<" "<<ii<<endl;
            //printf("ii= %d  i= %d j= %d  %d\n",ii,i,j,dp[cur][j]);
            if(i==n) {
                if(dp[cur][k]<res)
                 res=dp[cur][k];
            }
            cur=1-cur;
        }
    }
    cout<<res<<endl;

}

int main()
{
  //  freopen("R:\in.txt","r",stdin);
    int i,T;
    while (scanf("%d",&n)!=EOF)
    {
        for (i=1;i<=n;i++)
            scanf("%d",&p[i]);
        dyna();
    }
}


#include "iostream"
#include "cstring"
#include "cstdio"
#include "cmath"
#include "algorithm"
using namespace std;
#define LLD long long
const int N = 5005;
const long long inf = (LLD)(1)<<50;
int p[N];
LLD dp[2][N];
int n, m,len;
void dyna()
{
    int i, ii,t,j, k,cnt=0,tmp,a,b;
    LLD res=inf;
    int cur=0;
    for (ii=1;ii<=n;ii++)
    {
        t=p[ii];
        if (p[1]-t<0) continue;
        cur=1;
        for (i=0;i<=n;i++)
               dp[0][i]=inf;
        dp[0][ii]=p[1]-t;
      //  cout<<"dp[0][t]" <<dp[0][t]<<endl;
        int last=t;
        int lastid=ii;
        for (i=2;i<=n;i++) {
            for (j=0;j<=n;j++)
               dp[cur][i]=inf;
            if (p[i]>last){
                j=p[i];
                k=i;
                dp[cur][k]=dp[1-cur][lastid]+0;
                last=j;
                lastid=k;
            }
            else{
                j=last;
                k=lastid;
                dp[cur][k]=dp[1-cur][k]+j-p[i];
            }
            //cout<<i<<" "<<j<<" "<<ii<<endl;
            //printf("ii= %d  i= %d j= %d  %d\n",ii,i,j,dp[cur][j]);
            if(i==n) {
                if(dp[cur][k]<res)
                 res=dp[cur][k];
            }
            cur=1-cur;
        }
    }
    cout<<res<<endl;

}

int main()
{
  //  freopen("R:\in.txt","r",stdin);
    int i,T;
    while (scanf("%d",&n)!=EOF)
    {
        for (i=1;i<=n;i++)
            scanf("%d",&p[i]);
        if(n==1){
            puts("0");
            continue;
        }
        dyna();
    }
}


#include "iostream"
#include "cstring"
#include "cstdio"
#include "cmath"
#include "algorithm"
using namespace std;
#define LLD long long
const int N = 5005;
const long long inf = (LLD)(1)<<50;
int p[N];
LLD dp[2][N];
int n, m,len;
void dyna()
{
    int i, ii,t,j, k,cnt=0,tmp,a,b;
    LLD res=inf;
    int cur=0;
    for (ii=1;ii<=n;ii++)
    {
        t=p[ii];
        if (p[1]-t<0) continue;
        cur=1;
        for (i=0;i<=n;i++)
               dp[0][i]=inf;
        dp[0][ii]=p[1]-t;
      //  cout<<"dp[0][t]" <<dp[0][t]<<endl;
        int last=t;
        int lastid=ii;
        for (i=2;i<=n;i++) {
            for (j=0;j<=n;j++)
               dp[cur][j]=inf;
            if (p[i]>last){
                j=p[i];
                k=i;
                dp[cur][k]=dp[1-cur][lastid]+0;
                last=j;
                lastid=k;
            }
            else{
                j=last;
                k=lastid;
                dp[cur][k]=dp[1-cur][k]+j-p[i];
            }
            //cout<<i<<" "<<j<<" "<<ii<<endl;
           /* if(i==n)
            printf("then r ii= %d  i= %d j= %d  %d\n",ii,i,j,dp[cur][k]);
            else
             printf("ii= %d  i= %d j= %d  %d\n",ii,i,j,dp[cur][k]);
            **/
            if(i==n) {
                if(dp[cur][k]<res)
                 res=dp[cur][k];
            }
            cur=1-cur;
        }
    }
    cout<<res<<endl;

}

int main()
{
   // freopen("R:\in.txt","r",stdin);
    int i,T;
    while (scanf("%d",&n)!=EOF)
    {
       // if(n==-1) break;
        for (i=1;i<=n;i++){
            scanf("%d",&p[i]);
          //cout<<p[i]<<endl;
        }
        if(n==1){
            puts("0");
            continue;
        }
        dyna();
    }
}


#include "iostream"
#include "cstring"
#include "cstdio"
#include "algorithm"
using namespace std;
const int N = 9000;
#define LLD long long
const LLD inf = ((LLD)1)<<55;
LLD p[N],st[N],n,cnt,dp[2][N];
LLD iabs(LLD t){
  if(t>0) return t;
  return -t;
}
LLD min(LLD a,LLD b){
  if(a>b) return b;
  return a;
}
int main(){
  //freopen("R:\in.txt","r",stdin);
  int i,j;
  while(cin>>n){
     for(i=1;i<=n;i++){
       cin>>p[i];
       st[i]=p[i];
     }
     sort(st+1,st+n+1);
     int cur=0;
     memset(dp[cur],0,sizeof(dp[cur]));
     for(i=1;i<=n;i++){
        cur=1-cur;
        LLD tmp=inf;
        for(j=1;j<=n;j++){
          tmp=min(tmp,dp[1-cur][j]);
          dp[cur][j]=tmp+iabs(p[i]-st[j]);
        }
     }
     cnt=inf;
     for(i=1;i<=n;i++){
       if(dp[cur][i]<cnt)
          cnt=dp[cur][i];
     }
     cout<<cnt<<endl;

  }
}


#include "iostream"
#include "cstring"
#include "cstdio"
#include "algorithm"
#include "cmath"
using namespace std;
#define LLD long long
const int N = 9000;
const LLD inf =(LLD) (1)<<50;
LLD dp[2][N],p[N];
int cur,n;
LLD iabs(LLD t){
 if(t>0) return t;
 return -t;
}
void dyna(){
 cur=1;
 int id=0;
 LLD res=inf;
 int i,j;
 memset(dp,0,sizeof(dp));
 for(i=1;i<=n;i++){
  dp[0][i]=iabs(p[1]-p[i]);
 }
 for(i=2;i<=n;i++){
  for(j=1;j<=n;j++)  dp[cur][j]=inf;
  for(j=1;j<=n;j++){
   dp[cur][j]=min(dp[cur][j],dp[1-cur][j]+iabs(p[i]-p[j]));

 //  printf("id%d i= %d j = %lld  dp[cur][j] = %lld  < 1   >",id++,i,p[j],dp[cur][j]);
   //printf("i= %d j = %lld  dp[1-cur][j] = %lld\n",i,p[j],dp[1-cur][j]);

   if(p[j]>p[i-1]){
  // if(p[i-1]>=p[j]){
    dp[cur][j]=min(dp[cur][j],dp[1-cur][i-1]+iabs(p[j]-p[i]));

   }
   //printf("id%d i= %d j = %lld  dp[cur][j] = %lld  < 1   >",id++,i,p[j],dp[cur][j]);
   //printf("i= %d j = %lld  dp[1-cur][j] = %lld\n",i,p[j],dp[1-cur][i-1]);

   if(p[i]>=p[j]){
   dp[cur][i]=min(dp[1-cur][j]+0,dp[cur][i]);
   // printf("id%d i= %d j = %lld  dp[cur][i] = %lld  < 2 >",id++,i,p[i],dp[cur][i]);
   // printf("i= %d j = %lld  dp[1-cur][i] = %lld\n",i,p[i],dp[1-cur][j]);
   }
  }
  cur=cur^1;
 }

 for(i=1;i<=n;i++){
   if(dp[1-cur][i]<res)
      res=dp[1-cur][i];
 }

 cout<<res<<endl;
}
int main(){
  //freopen("R:\in.txt","r",stdin);
  int i;
  int cases=1;
  while(scanf("%d",&n)!=EOF){
     if(n==-1) break;
     for(i=1;i<=n;i++){
      cin>>p[i];
     }
   //  printf("case : %d \n",cases++);
     dyna();
  }
}


#include "iostream"
#include "cstring"
#include "cstdio"
#include "algorithm"
#include "cmath"
using namespace std;
#define LLD long long
const int N = 9000;
const LLD inf =(LLD) (1)<<50;
LLD dp[2][N],p[N];
int cur,n;
LLD iabs(LLD t){
 if(t>0) return t;
 return -t;
}
void dyna(){
 cur=1;
 int id=0;
 LLD res=inf;
 int i,j;
 memset(dp,0,sizeof(dp));
 for(i=1;i<=n;i++){
  dp[0][i]=iabs(p[1]-p[i]);
 }
 for(i=2;i<=n;i++){
  for(j=1;j<=n;j++)  dp[cur][j]=inf;
  for(j=1;j<=n;j++){
   dp[cur][j]=min(dp[cur][j],dp[1-cur][j]+iabs(p[i]-p[j]));

 //  printf("id%d i= %d j = %lld  dp[cur][j] = %lld  < 1   >",id++,i,p[j],dp[cur][j]);
   //printf("i= %d j = %lld  dp[1-cur][j] = %lld\n",i,p[j],dp[1-cur][j]);

   if(p[j]>=p[i-1]){
  // if(p[i-1]>=p[j]){
    dp[cur][j]=min(dp[cur][j],dp[1-cur][i-1]+iabs(p[j]-p[i]));

   }
   //printf("id%d i= %d j = %lld  dp[cur][j] = %lld  < 1   >",id++,i,p[j],dp[cur][j]);
   //printf("i= %d j = %lld  dp[1-cur][j] = %lld\n",i,p[j],dp[1-cur][i-1]);

   if(p[i]>=p[j]){
   dp[cur][i]=min(dp[1-cur][j]+0,dp[cur][i]);
   // printf("id%d i= %d j = %lld  dp[cur][i] = %lld  < 2 >",id++,i,p[i],dp[cur][i]);
   // printf("i= %d j = %lld  dp[1-cur][i] = %lld\n",i,p[i],dp[1-cur][j]);
   }
  }
  cur=cur^1;
 }

 for(i=1;i<=n;i++){
   if(dp[1-cur][i]<res)
      res=dp[1-cur][i];
 }

 cout<<res<<endl;
}
int main(){
 // freopen("R:\in.txt","r",stdin);
  int i;
  int cases=1;
  while(scanf("%d",&n)!=EOF){
     if(n==-1) break;
     for(i=1;i<=n;i++){
      cin>>p[i];
     }
   //  printf("case : %d \n",cases++);
     dyna();
  }
}


#include "iostream"
#include "cstring"
#include "cstdio"
#include "cmath"
#include "algorithm"
using namespace std;
#define LLD long long
const int N = 5005;
const long long inf = (LLD)(1)<<50;
int p[N];
LLD dp[2][N];
int n, m,len;
void dyna()
{
    int i, ii,t,j, k,cnt=0,tmp,a,b;
    LLD res=inf;
    int cur=0;
    for (ii=1;ii<=n;ii++)
   // ii=3;
    {
        t=p[ii];
    //  if (p[1]-t<0) continue;
        cur=1;
        for (i=0;i<=n;i++)
               dp[0][i]=inf;
        dp[0][ii]=p[1]-t;
      //  cout<<"dp[0][t]" <<dp[0][t]<<endl;
        int last=t;
        int lastid=ii;
        for (i=2;i<=n;i++) {
           /* for (j=0;j<=n;j++)
               dp[cur][j]=inf;**/

            if (p[i]>last&&i>=ii){
                j=p[i];
                k=i;
                dp[cur][k]=dp[1-cur][lastid]+0;
                last=j;
                lastid=k;
            }
            else{
                j=last;
                k=lastid;
                int tm=j-p[i];
                if(tm<0) tm=-tm;
                dp[cur][k]=dp[1-cur][k]+tm;
            }
            //cout<<i<<" "<<j<<" "<<ii<<endl;
          /*  if(i==n)
            printf("then r ii= %d  i= %d j= %d  %d\n",ii,i,j,dp[cur][k]);
            else
             printf("ii= %d  i= %d j= %d  %d\n",ii,i,j,dp[cur][k]);
             **/
            if(i==n) {
                if(dp[cur][k]>=0&&dp[cur][k]<res)
                 res=dp[cur][k];
            }
            cur=1-cur;
        }
    }
    cout<<res<<endl;

}

int main()
{
   // freopen("R:\in.txt","r",stdin);
    int i,T;
    while (scanf("%d",&n)!=EOF)
    {
      //  if(n==-1) break;
        for (i=1;i<=n;i++){
            scanf("%d",&p[i]);
          //cout<<p[i]<<endl;
        }
        if(n==1){
            puts("0");
            continue;
        }
        dyna();
    }
}


#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <cstring>
#include <map>
#include <string>
#include <queue>
#include <set>
#include <stack>
using namespace std;
typedef pair<int, int> ii;
typedef long long ll;
typedef pair<ii, int> iii;
#define rep(i, a, b) for(int i = a; i < b; i++)
#define red(i, a, b) for(int i = a; i > b; i--)
#define RED true
#define BLACK false
#define pb push_back
#define mk make_pair
#define fi first
#define se second
const int N = 5001;
const int M = 1000 + 1;
const int inf = 1e9 + 7;
const ll base = 1e18;
const double ep = 1e-9;

int dp[N][N];
int a[N];
int b[N];
int n;

int main(){
     //freopen("out.txt", "r", stdin);
    cin >> n;
    rep(i, 1, n + 1){
        cin >> a[i];
        b[i] = a[i];
    }
    sort(b + 1, b + n + 1);
    rep(i, 1, n + 1){
        dp[i][0] = base;
        rep(j, 1, n + 1){
            dp[i][j] = dp[i][j-1];
            int tmp = abs(a[i] - b[j]);
            dp[i][j] = min(dp[i-1][j] + tmp, dp[i][j]);
        }
    }
    cout<<dp[n][n];
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <cstring>
#include <map>
#include <string>
#include <queue>
#include <set>
#include <stack>
using namespace std;
typedef pair<int, int> ii;
typedef long long ll;
typedef pair<ii, int> iii;
#define rep(i, a, b) for(int i = a; i < b; i++)
#define red(i, a, b) for(int i = a; i > b; i--)
#define RED true
#define BLACK false
#define pb push_back
#define mk make_pair
#define fi first
#define se second
const int N = 1e3 + 7;
const int M = 1000 + 1;
const int inf = 1e9 + 7;
const ll base = 1e18;
const double ep = 1e-9;

ll dp[N][N];
int a[N];
int b[N];
int n;

int main(){
     //freopen("out.txt", "r", stdin);
    cin >> n;
    rep(i, 1, n + 1){
        cin >> a[i];
        b[i] = a[i];
    }
    sort(b + 1, b + n + 1);
    rep(i, 1, n + 1){
        dp[i][0] = base;
        rep(j, 1, n + 1){
            dp[i][j] = dp[i][j-1];
            int tmp = abs(a[i] - b[j]);
            dp[i][j] = min(dp[i-1][j] + tmp, dp[i][j]);
        }
    }
    cout<<dp[n][n];
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <cstring>
#include <map>
#include <string>
#include <queue>
#include <set>
#include <stack>
using namespace std;
typedef pair<int, int> ii;
typedef long long ll;
typedef pair<ii, int> iii;
#define rep(i, a, b) for(int i = a; i < b; i++)
#define red(i, a, b) for(int i = a; i > b; i--)
#define RED true
#define BLACK false
#define pb push_back
#define mk make_pair
#define fi first
#define se second
const int N = 5e3 + 7;
const int M = 1000 + 1;
const int inf = 1e9 + 7;
const ll base = 1e18;
const double ep = 1e-9;

ll dp[N][N];
int a[N];
int b[N];
int n;

int main(){
     //freopen("out.txt", "r", stdin);
    cin >> n;
    rep(i, 1, n + 1){
        cin >> a[i];
        b[i] = a[i];
    }
    sort(b + 1, b + n + 1);
    rep(i, 1, n + 1){
        dp[i][0] = base;
        rep(j, 1, n + 1){
            dp[i][j] = dp[i][j-1];
            int tmp = abs(a[i] - b[j]);
            dp[i][j] = min(dp[i-1][j] + tmp, dp[i][j]);
        }
    }
    cout<<dp[n][n];
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <cstring>
#include <map>
#include <string>
#include <queue>
#include <set>
#include <stack>
using namespace std;
typedef pair<int, int> ii;
typedef long long ll;
typedef pair<ii, int> iii;
#define rep(i, a, b) for(int i = a; i < b; i++)
#define red(i, a, b) for(int i = a; i > b; i--)
#define RED true
#define BLACK false
#define pb push_back
#define mk make_pair
#define fi first
#define se second
const int N = 5001;
const int M = 1000 + 1;
const int inf = 1e9 + 7;
const ll base = 1e18;
const double ep = 1e-9;

ll dp[N][N];
int a[N];
int b[N];
int n;

int main(){
     //freopen("out.txt", "r", stdin);
    cin >> n;
    rep(i, 1, n + 1){
        cin >> a[i];
        b[i] = a[i];
    }
    sort(b + 1, b + n + 1);
    rep(i, 1, n + 1){
        dp[i][0] = base;
        rep(j, 1, n + 1){
            dp[i][j] = dp[i][j-1];
            int tmp = abs(a[i] - b[j]);
            dp[i][j] = min(dp[i-1][j] + tmp, dp[i][j]);
        }
    }
    cout<<dp[n][n];
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <cstring>
#include <map>
#include <string>
#include <queue>
#include <set>
#include <stack>
using namespace std;
typedef pair<int, int> ii;
typedef long long ll;
typedef pair<ii, int> iii;
#define rep(i, a, b) for(int i = a; i < b; i++)
#define red(i, a, b) for(int i = a; i > b; i--)
#define RED true
#define BLACK false
#define pb push_back
#define mk make_pair
#define fi first
#define se second
const int N = 5001;
const int M = 1000 + 1;
const int inf = 1e9 + 7;
const ll base = 1e18;
const double ep = 1e-9;

ll dp[N];
int a[N];
int b[N];
int n;

int main(){
     //freopen("out.txt", "r", stdin);
    cin >> n;
    rep(i, 1, n + 1){
        cin >> a[i];
        b[i] = a[i];
    }
    sort(b + 1, b + n + 1);
    rep(i, 1, n + 1){
        dp[0] = base;
        rep(j, 1, n + 1){
            int tmp = abs(a[i] - b[j]);
            dp[j] = min(dp[j] + tmp, dp[j-1]);
        }
    }
    cout<<dp[n];
    return 0;
}


#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
const double eps=1e-8;
int sgn(double a){return a<-eps?-1:a<eps?0:1;}
int a[5001],b[5001];
ll ans[5001];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        b[i]=a[i];
    }
    sort(b+1,b+n+1);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            ans[j]+=abs(a[i]-b[j]);
            if(j>1) ans[j]=min(ans[j],ans[j-1]);
        }
    printf("%lld\n",ans[n]);
}


#include <iostream>
#include <queue>
using namespace std;
int n,a,ans;
priority_queue <int> q;
int main(){
	cin>>n;
	cin>>a;
	q.push(a);
	for (int i=1;i<n;i++) {
		cin>>a;
		q.push(a);
		if (q.top()>a) {
			ans+=q.top()-a;
			q.pop();
			q.push(a);
		}
	}
	cout<<ans<<endl;
}


#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
typedef long long ll;
ll n,a[5010],b[5010],f[5010];
inline int read()
{
    int f=1,x=0;
    char s=getchar();
    while(s<'0'||s>'9')
    {
        if(s=='-') f=-1;
        s=getchar();
    }
    while(s>='0'&&s<='9')
        x=x*10+s-'0',s=getchar();
    return x*f;
}
inline void write(ll x)
{
    if(x<0) putchar('-'),x=-x;
    if(x>9) write(x/10);
    putchar(x%10+'0');
}
int main()
{
    n=read();
    for(int i=1;i<=n;i++)
		a[i]=read(),b[i]=a[i];
    sort(b+1,b+n+1);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            if(j==1) f[j]+=abs(b[j]-a[i]);
            else f[j]=min(f[j-1],f[j]+abs(b[j]-a[i]));
        }
    write(f[n]);
    return 0;
}


#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
template<class AC>void r(AC &x){
    x=0;
    int f=0;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        f|=(ch=='-');
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    x=f?-x:x;
    return;
}
priority_queue<int,vector<int> >q;
ll n,ans;
int main(){
    r(n);
    while(n--){
    	int x;
    	r(x);
    	q.push(x);
    	if(x<q.top())ans+=(q.top()-x),q.pop(),q.push(x);
	}
	cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[500001];
int f[500001]={0};
int main()
{
	int n,minn=1e9;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]),minn=min(minn,a[i]);
	a[0]=minn;
	for(int i=1;i<=n;i++)
	{
		if(a[i]>=a[i-1])f[i]=f[i-1];
		else f[i]=f[i-1]+a[i-1]-a[i];
		for(int j=i-1;j>=0;j--)
			if(a[i]<a[j])f[j]=f[j]+a[j]-a[i];
			else f[j]=f[j]+a[i]-a[j];
	}
	int ans=500001;
	for(int i=1;i<=n;i++)
		ans=min(ans,f[i]);
	printf("%d",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const long long inf=1000000000;
long long n,m,ans;
long long a[5010],f[3][5010],w[5010];
void solve( ){

	for(register long long i=1;i<=n;++i){
		long long k=i%2;
		for(register long long j=1;j<=n;++j){
			if(j==1)f[k][j]=f[1-k][j]+abs(w[j]-a[i]);
			else f[k][j]=min(f[k][j-1],f[1-k][j]+abs(w[j]-a[i]));
		}
	}
	ans=f[n%2][n];
}

int main( ){
	long long k,x,y;
	scanf("%lld",&n);
	for(register long long i=1;i<=n;++i){
		scanf("%lld",&a[i]);
		w[i]=a[i];
	}
	sort(w+1,w+n+1);
	solve( );
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long n,a[5005];
priority_queue<long long> q;
int main()
{
	scanf("%lld",&n);
	int p0=1000000005;
	for (int i=1;i<=n;i++) {
	    scanf("%lld",&a[i]);
	    p0=min(p0,a[i]);
	}
	q.push(a[1]);
	for (int i=2;i<=n;i++){
		int opt=q.top();
		if (opt<=a[i]) q.push(a[i]);
		else {
			q.pop();
			q.push(a[i]);
			q.push(a[i]);
		}
	}
	long long ans=0;
	for (int i=1;i<=n;i++) ans+=a[i]-p0;
	int k=1;
	q.push(p0);
	while (!q.empty()){
		long long p2=q.top();
		q.pop();
		if (q.empty()){
			break;
		}
		long long p1=q.top();
		ans-=(p2-p1)*k;
		k++;
	}
	printf("%lld\n",ans);
}


#include<bits/stdc++.h>
using namespace std;
long long n,a[5005];
priority_queue<long long> q;
int main()
{
	cin>>n;
	long long p0=1000000005;
	for (int i=1;i<=n;i++) {
	    cin>>a[i];
	    p0=min(p0,a[i]);
	}
	q.push(a[1]);
	for (int i=2;i<=n;i++){
		int opt=q.top();
		if (opt<=a[i]) q.push(a[i]);
		else {
			q.pop();
			q.push(a[i]);
			q.push(a[i]);
		}
	}
	long long ans=0;
	for (int i=1;i<=n;i++) ans+=a[i]-p0;
	int k=1;
	q.push(p0);
	while (!q.empty()){
		int p2=q.top();
		q.pop();
		if (q.empty()){
			break;
		}
		int p1=q.top();
		ans-=(p2-p1)*k;
		k++;
	}
	cout<<ans<<endl;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int n;

    cin >> n;

    vector<long long int> v(n);
    for(int i = 0; i < n; i++) {
        cin >> v[i];
    }
    vector<long long int> vv(v);
    sort(vv.begin(), vv.end());
    int op = 0;

    vector<long long int> f(n);

    f[0] = abs(v[0]-vv[0]);

    for(int i = 1; i < n; i++) {
        f[i] = min(f[i-1], abs(v[0]-vv[i]));
    }

    for(int i = 1; i < n; i++) {
        vector<long long int> f2(n);
        f2[0] = f[0]+abs(v[i]-vv[0]);
        for(int j = 1; j < n; j++) {
            f2[j] = min(f[j] + abs(v[i]-vv[j]), f2[j-1]);
        }
        f = f2;
    }

    cout << f[n-1] << endl;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int f[5001][5001];
int main()
{
    int n;

    cin >> n;

    vector<int> v(n);
    for(int i = 0; i < n; i++) {
        cin >> v[i];
    }
    vector<int> vv(v);
    sort(vv.begin(), vv.end());
    int op = 0;

    vector<int> f(n);

    f[0] = abs(v[0]-vv[0]);

    for(int i = 1; i < n; i++) {
        f[i] = min(f[i-1], abs(v[0]-vv[i]));
    }

    for(int i = 1; i < n; i++) {
        vector<int> f2(n);
        f2[0] = f[0]+abs(v[i]-vv[0]);
        for(int j = 1; j < n; j++) {
            f2[j] = min(f[j] + abs(v[i]-vv[j]), f2[j-1]);
        }
        f = f2;
    }

    cout << f[n-1] << endl;
}


#include<stdio.h>
#include<math.h>
int main() {
        long long n, a;
        scanf("%lli", &n);
        long long nums[n];
        for (int i = 0; i < n; i++) {
                scanf("%lli", &a);
                nums[i] = a;
        }
        long long numSet[64];
        for (int i = 0; i < n; i++) {
                long long cur = nums[i];
                int bit = 0;
                while (cur) {
                        numSet[bit] += cur & 1;
                        cur >>= 1;
                        bit++;
                }
        }
        int largestUniqueBit = -1;
        for (int i = 63; i >= 0; i--) {
                if (numSet[i] == 1) {
                        largestUniqueBit = i;
                }
        }
        if (largestUniqueBit == -1) {
                for (int i = 0; i < n; i++) {
                        printf("%lli ", nums[i]);
                }
        } else {
                int index = -1;
                for (int i = 0; i < n; i++) {
                        if (nums[i] >= (long long)pow(2, largestUniqueBit) && ((nums[i] >> largestUniqueBit) & 1)) {
                                printf("%lli ", nums[i]);
                                index = i;
                        }
                }
                for (int i = 0; i < n; i++) {
                        if (i != index) {
                                printf("%lli ", nums[i]);
                        }
                }
        }
}
// AnuHasAFunction.c


#include<stdio.h>
#include<math.h>
int main() {
        long long n, a;
        scanf("%lli", &n);
        long long nums[n];
        for (int i = 0; i < n; i++) {
                scanf("%lli", &a);
                nums[i] = a;
        }
        long long numSet[64];
        for (int i = 0; i < 64; i++) {
                numSet[i] = 0;
        }
        for (int i = 0; i < n; i++) {
                long long cur = nums[i];
                int bit = 0;
                while (cur) {
                        numSet[bit] += cur & 1;
                        cur >>= 1;
                        bit++;
                }
        }
        int largestUniqueBit = -1;
        for (int i = 63; i >= 0; i--) {
                if (numSet[i] == 1) {
                        largestUniqueBit = i;
                }
        }
        if (largestUniqueBit == -1) {
                for (int i = 0; i < n; i++) {
                        printf("%lli ", nums[i]);
                }
        } else {
                int index = -1;
                for (int i = 0; i < n; i++) {
                        if (nums[i] >= (long long)pow(2, largestUniqueBit) && ((nums[i] >> largestUniqueBit) & 1)) {
                                printf("%lli ", nums[i]);
                                index = i;
                        }
                }
                for (int i = 0; i < n; i++) {
                        if (i != index) {
                                printf("%lli ", nums[i]);
                        }
                }
        }
}
// AnuHasAFunction.c