import java.util.Arrays;
import java.util.*;

public class Main{

         public static void main(String[] args) {

                Scanner scan = new Scanner(System.in);
                int N = scan.nextInt();
                int array[] = new int[N+1];
                int result=0;

                for(int i=0 ; i < N ; i++){
                        array[i]=scan.nextInt();
                }
                array[N]=0;

                for(int i=N ; i > 0 ; i--){
                        if(array[i]<array[i-1]){
                                result+=array[i-1]-array[i];
                        }
                }

                System.out.println(result);
         }
} import java.util.Arrays; Arrays java.util import java.util.*; util.* java public class Main{

         public static void main(String[] args) {

                Scanner scan = new Scanner(System.in);
                int N = scan.nextInt();
                int array[] = new int[N+1];
                int result=0;

                for(int i=0 ; i < N ; i++){
                        array[i]=scan.nextInt();
                }
                array[N]=0;

                for(int i=N ; i > 0 ; i--){
                        if(array[i]<array[i-1]){
                                result+=array[i-1]-array[i];
                        }
                }

                System.out.println(result);
         }
} Main public static void main(String[] args) {

                Scanner scan = new Scanner(System.in);
                int N = scan.nextInt();
                int array[] = new int[N+1];
                int result=0;

                for(int i=0 ; i < N ; i++){
                        array[i]=scan.nextInt();
                }
                array[N]=0;

                for(int i=N ; i > 0 ; i--){
                        if(array[i]<array[i-1]){
                                result+=array[i-1]-array[i];
                        }
                }

                System.out.println(result);
         } main {

                Scanner scan = new Scanner(System.in);
                int N = scan.nextInt();
                int array[] = new int[N+1];
                int result=0;

                for(int i=0 ; i < N ; i++){
                        array[i]=scan.nextInt();
                }
                array[N]=0;

                for(int i=N ; i > 0 ; i--){
                        if(array[i]<array[i-1]){
                                result+=array[i-1]-array[i];
                        }
                }

                System.out.println(result);
         } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int N = scan.nextInt(); N scan.nextInt() scan.nextInt scan int array[] = new int[N+1]; array new int[N+1] N+1 N 1 int result=0; result 0 for(int i=0 ; i < N ; i++){
                        array[i]=scan.nextInt();
                } int i=0 ; int i=0 ; i 0 i < N i N i++ i++ i {
                        array[i]=scan.nextInt();
                } {
                        array[i]=scan.nextInt();
                } array[i]=scan.nextInt() array[i] array i scan.nextInt() scan.nextInt scan array[N]=0 array[N] array N 0 for(int i=N ; i > 0 ; i--){
                        if(array[i]<array[i-1]){
                                result+=array[i-1]-array[i];
                        }
                } int i=N ; int i=N ; i N i > 0 i 0 i-- i-- i {
                        if(array[i]<array[i-1]){
                                result+=array[i-1]-array[i];
                        }
                } {
                        if(array[i]<array[i-1]){
                                result+=array[i-1]-array[i];
                        }
                } if(array[i]<array[i-1]){
                                result+=array[i-1]-array[i];
                        } array[i]<array[i-1] array[i] array i array[i-1] array i-1 i 1 {
                                result+=array[i-1]-array[i];
                        } result+=array[i-1]-array[i] result array[i-1]-array[i] array[i-1] array i-1 i 1 array[i] array i System.out.println(result) System.out.println System.out System System.out result String[] args args public class Main{

         public static void main(String[] args) {

                Scanner scan = new Scanner(System.in);
                int N = scan.nextInt();
                int array[] = new int[N+1];
                int result=0;

                for(int i=0 ; i < N ; i++){
                        array[i]=scan.nextInt();
                }
                array[N]=0;

                for(int i=N ; i > 0 ; i--){
                        if(array[i]<array[i-1]){
                                result+=array[i-1]-array[i];
                        }
                }

                System.out.println(result);
         }
} public class Main{

         public static void main(String[] args) {

                Scanner scan = new Scanner(System.in);
                int N = scan.nextInt();
                int array[] = new int[N+1];
                int result=0;

                for(int i=0 ; i < N ; i++){
                        array[i]=scan.nextInt();
                }
                array[N]=0;

                for(int i=N ; i > 0 ; i--){
                        if(array[i]<array[i-1]){
                                result+=array[i-1]-array[i];
                        }
                }

                System.out.println(result);
         }
} Main
import java.util.Arrays;
import java.util.*;

public class Main{

         public static void main(String[] args) {

                Scanner scan = new Scanner(System.in);
                int N = scan.nextInt();
                int array[] = new int[N+1];
                int result=0;

                for(int i=0 ; i < N ; i++){
                        array[i]=scan.nextInt();
                }
                array[N]=0;

                for(int i=N ; i > 0 ; i--){
                        if(array[i]<array[i-1]){
                                result+=array[i-1]-array[i];
                        }
                }

                System.out.println(result);
         }
}