

import java.util.*;
import java.lang.*;
import java.io.*;

public class Main
{
    public static void main (String[] args)
    {
        // your code goes here
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int[] h = new int[n];
        long sum = 0;
        for(int j=0; j<n; j++)
            h[j] = s.nextInt();

        for(int j=1; j<n; j++)
        {
            if(h[j-1]>h[j])
            {
                sum+=h[j-1]-h[j];
                h[j] = h[j-1];
            }
        }
        System.out.println(sum);
    }
} import java.util.*; util.* java import java.lang.*; lang.* java import java.io.*; io.* java public class Main
{
    public static void main (String[] args)
    {
        // your code goes here
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int[] h = new int[n];
        long sum = 0;
        for(int j=0; j<n; j++)
            h[j] = s.nextInt();

        for(int j=1; j<n; j++)
        {
            if(h[j-1]>h[j])
            {
                sum+=h[j-1]-h[j];
                h[j] = h[j-1];
            }
        }
        System.out.println(sum);
    }
} Main public static void main (String[] args)
    {
        // your code goes here
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int[] h = new int[n];
        long sum = 0;
        for(int j=0; j<n; j++)
            h[j] = s.nextInt();

        for(int j=1; j<n; j++)
        {
            if(h[j-1]>h[j])
            {
                sum+=h[j-1]-h[j];
                h[j] = h[j-1];
            }
        }
        System.out.println(sum);
    } main {
        // your code goes here
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int[] h = new int[n];
        long sum = 0;
        for(int j=0; j<n; j++)
            h[j] = s.nextInt();

        for(int j=1; j<n; j++)
        {
            if(h[j-1]>h[j])
            {
                sum+=h[j-1]-h[j];
                h[j] = h[j-1];
            }
        }
        System.out.println(sum);
    } Scanner s = new Scanner(System.in); s new Scanner(System.in) int n = s.nextInt(); n s.nextInt() s.nextInt s int[] h = new int[n]; h new int[n] n long sum = 0; sum 0 for(int j=0; j<n; j++)
            h[j] = s.nextInt(); int j=0; int j=0; j 0 j<n j n j++ j++ j h[j] = s.nextInt(); h[j] = s.nextInt() h[j] h j s.nextInt() s.nextInt s for(int j=1; j<n; j++)
        {
            if(h[j-1]>h[j])
            {
                sum+=h[j-1]-h[j];
                h[j] = h[j-1];
            }
        } int j=1; int j=1; j 1 j<n j n j++ j++ j {
            if(h[j-1]>h[j])
            {
                sum+=h[j-1]-h[j];
                h[j] = h[j-1];
            }
        } {
            if(h[j-1]>h[j])
            {
                sum+=h[j-1]-h[j];
                h[j] = h[j-1];
            }
        } if(h[j-1]>h[j])
            {
                sum+=h[j-1]-h[j];
                h[j] = h[j-1];
            } h[j-1]>h[j] h[j-1] h j-1 j 1 h[j] h j {
                sum+=h[j-1]-h[j];
                h[j] = h[j-1];
            } sum+=h[j-1]-h[j] sum h[j-1]-h[j] h[j-1] h j-1 j 1 h[j] h j h[j] = h[j-1] h[j] h j h[j-1] h j-1 j 1 System.out.println(sum) System.out.println System.out System System.out sum String[] args args public class Main
{
    public static void main (String[] args)
    {
        // your code goes here
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int[] h = new int[n];
        long sum = 0;
        for(int j=0; j<n; j++)
            h[j] = s.nextInt();

        for(int j=1; j<n; j++)
        {
            if(h[j-1]>h[j])
            {
                sum+=h[j-1]-h[j];
                h[j] = h[j-1];
            }
        }
        System.out.println(sum);
    }
} public class Main
{
    public static void main (String[] args)
    {
        // your code goes here
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int[] h = new int[n];
        long sum = 0;
        for(int j=0; j<n; j++)
            h[j] = s.nextInt();

        for(int j=1; j<n; j++)
        {
            if(h[j-1]>h[j])
            {
                sum+=h[j-1]-h[j];
                h[j] = h[j-1];
            }
        }
        System.out.println(sum);
    }
} Main


import java.util.*;
import java.lang.*;
import java.io.*;

public class Main
{
    public static void main (String[] args)
    {
        // your code goes here
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int[] h = new int[n];
        long sum = 0;
        for(int j=0; j<n; j++)
            h[j] = s.nextInt();

        for(int j=1; j<n; j++)
        {
            if(h[j-1]>h[j])
            {
                sum+=h[j-1]-h[j];
                h[j] = h[j-1];
            }
        }
        System.out.println(sum);
    }
}