#include<stdio.h>
 
#define MAX 1000

struct MCS {
	int cost;
	int order;
};

void exchange( struct MCS *pa, struct MCS *pb )
{
   	struct MCS t;
     
	t=*pa;
	*pa=*pb;
	*pb=t;
	 
	 return;
}
 
int Partition( struct MCS A[], int p, int r )
{
    struct MCS x;
    int i,j;
 
    x=A[r];
    i=p-1;
    for( j=p; j<r; j++ ) {
        if( A[j].cost < x.cost ) {
            i++;
            exchange( &A[i], &A[j] );
         }
    }
    exchange( &A[i+1], &A[r] );

    return i+1;
}
void QuickSort( struct MCS A[], int p, int r )
{
    int q;
     
    if( p < r ) {
        q=Partition( A, p, r );
        QuickSort( A, p, q-1 );
        QuickSort( A, q+1, r );
    }
    return;
}

int main( void ) 
{   
    int n,i;
    struct MCS W[MAX+1];
    int cnt;
        
    scanf( "%d", &n );
    for( i=0; i<n; i++ ) {
        scanf( "%d", &W[i].cost );
        W[i].order=i;
    }
    QuickSort( W, 0, n-1 );
    
    cnt=0;    
    for( i=0; i<n; i++ ) {
    	if( i != W[i].order ) {
    		cnt+=W[i].cost;
        }
    }
    printf( "%d\n", cnt );
    return 0;
}