//package atcoder.beginnercontest.r176;

import java.util.Scanner;

/**
 * @author pribic (Priyank Doshi)
 * @since 29/08/20
 */
public class Main {

  public static void main(String[] args) {
    try (Scanner sc = new Scanner(System.in)) {
      int n = sc.nextInt();
      long ans = 0;
      int lastVal = -1;
      for(int i = 0; i < n; i++) {
        if(lastVal == -1) {
          lastVal = sc.nextInt();
        } else {
          int curVal = sc.nextInt();
          if(curVal < lastVal) {
            ans += lastVal - curVal;
          }
          lastVal = Math.max(lastVal, curVal);
        }
      }
      
      System.out.println(ans);
    }
  }
} import java.util.Scanner; Scanner java.util public class Main {

  public static void main(String[] args) {
    try (Scanner sc = new Scanner(System.in)) {
      int n = sc.nextInt();
      long ans = 0;
      int lastVal = -1;
      for(int i = 0; i < n; i++) {
        if(lastVal == -1) {
          lastVal = sc.nextInt();
        } else {
          int curVal = sc.nextInt();
          if(curVal < lastVal) {
            ans += lastVal - curVal;
          }
          lastVal = Math.max(lastVal, curVal);
        }
      }
      
      System.out.println(ans);
    }
  }
} Main public static void main(String[] args) {
    try (Scanner sc = new Scanner(System.in)) {
      int n = sc.nextInt();
      long ans = 0;
      int lastVal = -1;
      for(int i = 0; i < n; i++) {
        if(lastVal == -1) {
          lastVal = sc.nextInt();
        } else {
          int curVal = sc.nextInt();
          if(curVal < lastVal) {
            ans += lastVal - curVal;
          }
          lastVal = Math.max(lastVal, curVal);
        }
      }
      
      System.out.println(ans);
    }
  } main {
    try (Scanner sc = new Scanner(System.in)) {
      int n = sc.nextInt();
      long ans = 0;
      int lastVal = -1;
      for(int i = 0; i < n; i++) {
        if(lastVal == -1) {
          lastVal = sc.nextInt();
        } else {
          int curVal = sc.nextInt();
          if(curVal < lastVal) {
            ans += lastVal - curVal;
          }
          lastVal = Math.max(lastVal, curVal);
        }
      }
      
      System.out.println(ans);
    }
  } try (Scanner sc = new Scanner(System.in)) {
      int n = sc.nextInt();
      long ans = 0;
      int lastVal = -1;
      for(int i = 0; i < n; i++) {
        if(lastVal == -1) {
          lastVal = sc.nextInt();
        } else {
          int curVal = sc.nextInt();
          if(curVal < lastVal) {
            ans += lastVal - curVal;
          }
          lastVal = Math.max(lastVal, curVal);
        }
      }
      
      System.out.println(ans);
    } {
      int n = sc.nextInt();
      long ans = 0;
      int lastVal = -1;
      for(int i = 0; i < n; i++) {
        if(lastVal == -1) {
          lastVal = sc.nextInt();
        } else {
          int curVal = sc.nextInt();
          if(curVal < lastVal) {
            ans += lastVal - curVal;
          }
          lastVal = Math.max(lastVal, curVal);
        }
      }
      
      System.out.println(ans);
    } Scanner sc = new Scanner(System.in) Scanner sc = new Scanner(System.in) new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc long ans = 0; ans 0 int lastVal = -1; lastVal -1 1 for(int i = 0; i < n; i++) {
        if(lastVal == -1) {
          lastVal = sc.nextInt();
        } else {
          int curVal = sc.nextInt();
          if(curVal < lastVal) {
            ans += lastVal - curVal;
          }
          lastVal = Math.max(lastVal, curVal);
        }
      } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
        if(lastVal == -1) {
          lastVal = sc.nextInt();
        } else {
          int curVal = sc.nextInt();
          if(curVal < lastVal) {
            ans += lastVal - curVal;
          }
          lastVal = Math.max(lastVal, curVal);
        }
      } {
        if(lastVal == -1) {
          lastVal = sc.nextInt();
        } else {
          int curVal = sc.nextInt();
          if(curVal < lastVal) {
            ans += lastVal - curVal;
          }
          lastVal = Math.max(lastVal, curVal);
        }
      } if(lastVal == -1) {
          lastVal = sc.nextInt();
        } else {
          int curVal = sc.nextInt();
          if(curVal < lastVal) {
            ans += lastVal - curVal;
          }
          lastVal = Math.max(lastVal, curVal);
        } lastVal == -1 lastVal -1 1 {
          lastVal = sc.nextInt();
        } lastVal = sc.nextInt() lastVal sc.nextInt() sc.nextInt sc {
          int curVal = sc.nextInt();
          if(curVal < lastVal) {
            ans += lastVal - curVal;
          }
          lastVal = Math.max(lastVal, curVal);
        } int curVal = sc.nextInt(); curVal sc.nextInt() sc.nextInt sc if(curVal < lastVal) {
            ans += lastVal - curVal;
          } curVal < lastVal curVal lastVal {
            ans += lastVal - curVal;
          } ans += lastVal - curVal ans lastVal - curVal lastVal curVal lastVal = Math.max(lastVal, curVal) lastVal Math.max(lastVal, curVal) Math.max Math lastVal curVal System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {

  public static void main(String[] args) {
    try (Scanner sc = new Scanner(System.in)) {
      int n = sc.nextInt();
      long ans = 0;
      int lastVal = -1;
      for(int i = 0; i < n; i++) {
        if(lastVal == -1) {
          lastVal = sc.nextInt();
        } else {
          int curVal = sc.nextInt();
          if(curVal < lastVal) {
            ans += lastVal - curVal;
          }
          lastVal = Math.max(lastVal, curVal);
        }
      }
      
      System.out.println(ans);
    }
  }
} public class Main {

  public static void main(String[] args) {
    try (Scanner sc = new Scanner(System.in)) {
      int n = sc.nextInt();
      long ans = 0;
      int lastVal = -1;
      for(int i = 0; i < n; i++) {
        if(lastVal == -1) {
          lastVal = sc.nextInt();
        } else {
          int curVal = sc.nextInt();
          if(curVal < lastVal) {
            ans += lastVal - curVal;
          }
          lastVal = Math.max(lastVal, curVal);
        }
      }
      
      System.out.println(ans);
    }
  }
} Main
//package atcoder.beginnercontest.r176;

import java.util.Scanner;

/**
 * @author pribic (Priyank Doshi)
 * @since 29/08/20
 */
public class Main {

  public static void main(String[] args) {
    try (Scanner sc = new Scanner(System.in)) {
      int n = sc.nextInt();
      long ans = 0;
      int lastVal = -1;
      for(int i = 0; i < n; i++) {
        if(lastVal == -1) {
          lastVal = sc.nextInt();
        } else {
          int curVal = sc.nextInt();
          if(curVal < lastVal) {
            ans += lastVal - curVal;
          }
          lastVal = Math.max(lastVal, curVal);
        }
      }
      
      System.out.println(ans);
    }
  }
}