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

class Main{
  static boolean found = false;
  public static void main(String[] args) throws IOException {
    PrintWriter out = new PrintWriter(System.out);
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st  = new StringTokenizer(br.readLine());

    int N = Integer.parseInt(st.nextToken());
    
    st  = new StringTokenizer(br.readLine());

    int[] arr = new int[N];

    for (int i=0; i<N; i++) {
      arr[i] = Integer.parseInt(st.nextToken());
    }

    long ans = 0;

    for (int i=1; i<N; i++) {
      if(arr[i]<arr[i-1]){
        ans+=arr[i-1]-arr[i];
        arr[i] = arr[i-1];
      }
    }

    out.println(ans);

    out.close();
  }

  
}







 import java.util.*; util.* java import java.io.*; io.* java class Main{
  static boolean found = false;
  public static void main(String[] args) throws IOException {
    PrintWriter out = new PrintWriter(System.out);
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st  = new StringTokenizer(br.readLine());

    int N = Integer.parseInt(st.nextToken());
    
    st  = new StringTokenizer(br.readLine());

    int[] arr = new int[N];

    for (int i=0; i<N; i++) {
      arr[i] = Integer.parseInt(st.nextToken());
    }

    long ans = 0;

    for (int i=1; i<N; i++) {
      if(arr[i]<arr[i-1]){
        ans+=arr[i-1]-arr[i];
        arr[i] = arr[i-1];
      }
    }

    out.println(ans);

    out.close();
  }

  
} Main static boolean found = false; found false public static void main(String[] args) throws IOException {
    PrintWriter out = new PrintWriter(System.out);
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st  = new StringTokenizer(br.readLine());

    int N = Integer.parseInt(st.nextToken());
    
    st  = new StringTokenizer(br.readLine());

    int[] arr = new int[N];

    for (int i=0; i<N; i++) {
      arr[i] = Integer.parseInt(st.nextToken());
    }

    long ans = 0;

    for (int i=1; i<N; i++) {
      if(arr[i]<arr[i-1]){
        ans+=arr[i-1]-arr[i];
        arr[i] = arr[i-1];
      }
    }

    out.println(ans);

    out.close();
  } main {
    PrintWriter out = new PrintWriter(System.out);
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st  = new StringTokenizer(br.readLine());

    int N = Integer.parseInt(st.nextToken());
    
    st  = new StringTokenizer(br.readLine());

    int[] arr = new int[N];

    for (int i=0; i<N; i++) {
      arr[i] = Integer.parseInt(st.nextToken());
    }

    long ans = 0;

    for (int i=1; i<N; i++) {
      if(arr[i]<arr[i-1]){
        ans+=arr[i-1]-arr[i];
        arr[i] = arr[i-1];
      }
    }

    out.println(ans);

    out.close();
  } PrintWriter out = new PrintWriter(System.out); out new PrintWriter(System.out) BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) StringTokenizer st  = new StringTokenizer(br.readLine()); st new StringTokenizer(br.readLine()) int N = Integer.parseInt(st.nextToken()); N Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st st  = new StringTokenizer(br.readLine()) st new StringTokenizer(br.readLine()) int[] arr = new int[N]; arr new int[N] N for (int i=0; i<N; i++) {
      arr[i] = Integer.parseInt(st.nextToken());
    } int i=0; int i=0; i 0 i<N i N i++ i++ i {
      arr[i] = Integer.parseInt(st.nextToken());
    } {
      arr[i] = Integer.parseInt(st.nextToken());
    } arr[i] = Integer.parseInt(st.nextToken()) arr[i] arr i Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st long ans = 0; ans 0 for (int i=1; i<N; i++) {
      if(arr[i]<arr[i-1]){
        ans+=arr[i-1]-arr[i];
        arr[i] = arr[i-1];
      }
    } int i=1; int i=1; i 1 i<N i N i++ i++ i {
      if(arr[i]<arr[i-1]){
        ans+=arr[i-1]-arr[i];
        arr[i] = arr[i-1];
      }
    } {
      if(arr[i]<arr[i-1]){
        ans+=arr[i-1]-arr[i];
        arr[i] = arr[i-1];
      }
    } if(arr[i]<arr[i-1]){
        ans+=arr[i-1]-arr[i];
        arr[i] = arr[i-1];
      } arr[i]<arr[i-1] arr[i] arr i arr[i-1] arr i-1 i 1 {
        ans+=arr[i-1]-arr[i];
        arr[i] = arr[i-1];
      } ans+=arr[i-1]-arr[i] ans arr[i-1]-arr[i] arr[i-1] arr i-1 i 1 arr[i] arr i arr[i] = arr[i-1] arr[i] arr i arr[i-1] arr i-1 i 1 out.println(ans) out.println out ans out.close() out.close out String[] args args
import java.util.*;
import java.io.*;

class Main{
  static boolean found = false;
  public static void main(String[] args) throws IOException {
    PrintWriter out = new PrintWriter(System.out);
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st  = new StringTokenizer(br.readLine());

    int N = Integer.parseInt(st.nextToken());
    
    st  = new StringTokenizer(br.readLine());

    int[] arr = new int[N];

    for (int i=0; i<N; i++) {
      arr[i] = Integer.parseInt(st.nextToken());
    }

    long ans = 0;

    for (int i=1; i<N; i++) {
      if(arr[i]<arr[i-1]){
        ans+=arr[i-1]-arr[i];
        arr[i] = arr[i-1];
      }
    }

    out.println(ans);

    out.close();
  }

  
}








