//package atcoder;

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


public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = s.nextInt();
        }

        int start = 0;
        int ans = 0;
        while(true) {
            start = 0;
            int count = 0;
            while(count < n && arr[start] == 0) {
                start++;
                count++;
            }

            if(count == n) {
                break;
            }

            int i = start;
            int min = Integer.MAX_VALUE;
            while(i < n && arr[i] != 0) {

                min = Math.min(arr[i], min);
                i++;
            }

            int j = start;
            while(j < i) {
                arr[j] -= min;
                j++;
            }
            ans += min;
        }
        System.out.println(ans);
    }
} import java.io.*; io.* java import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = s.nextInt();
        }

        int start = 0;
        int ans = 0;
        while(true) {
            start = 0;
            int count = 0;
            while(count < n && arr[start] == 0) {
                start++;
                count++;
            }

            if(count == n) {
                break;
            }

            int i = start;
            int min = Integer.MAX_VALUE;
            while(i < n && arr[i] != 0) {

                min = Math.min(arr[i], min);
                i++;
            }

            int j = start;
            while(j < i) {
                arr[j] -= min;
                j++;
            }
            ans += min;
        }
        System.out.println(ans);
    }
} Main public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = s.nextInt();
        }

        int start = 0;
        int ans = 0;
        while(true) {
            start = 0;
            int count = 0;
            while(count < n && arr[start] == 0) {
                start++;
                count++;
            }

            if(count == n) {
                break;
            }

            int i = start;
            int min = Integer.MAX_VALUE;
            while(i < n && arr[i] != 0) {

                min = Math.min(arr[i], min);
                i++;
            }

            int j = start;
            while(j < i) {
                arr[j] -= min;
                j++;
            }
            ans += min;
        }
        System.out.println(ans);
    } main {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = s.nextInt();
        }

        int start = 0;
        int ans = 0;
        while(true) {
            start = 0;
            int count = 0;
            while(count < n && arr[start] == 0) {
                start++;
                count++;
            }

            if(count == n) {
                break;
            }

            int i = start;
            int min = Integer.MAX_VALUE;
            while(i < n && arr[i] != 0) {

                min = Math.min(arr[i], min);
                i++;
            }

            int j = start;
            while(j < i) {
                arr[j] -= min;
                j++;
            }
            ans += min;
        }
        System.out.println(ans);
    } Scanner s = new Scanner(System.in); s new Scanner(System.in) int n = s.nextInt(); n s.nextInt() s.nextInt s int[] arr = new int[n]; arr new int[n] n for (int i = 0; i < n; i++) {
            arr[i] = s.nextInt();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            arr[i] = s.nextInt();
        } {
            arr[i] = s.nextInt();
        } arr[i] = s.nextInt() arr[i] arr i s.nextInt() s.nextInt s int start = 0; start 0 int ans = 0; ans 0 while(true) {
            start = 0;
            int count = 0;
            while(count < n && arr[start] == 0) {
                start++;
                count++;
            }

            if(count == n) {
                break;
            }

            int i = start;
            int min = Integer.MAX_VALUE;
            while(i < n && arr[i] != 0) {

                min = Math.min(arr[i], min);
                i++;
            }

            int j = start;
            while(j < i) {
                arr[j] -= min;
                j++;
            }
            ans += min;
        } true {
            start = 0;
            int count = 0;
            while(count < n && arr[start] == 0) {
                start++;
                count++;
            }

            if(count == n) {
                break;
            }

            int i = start;
            int min = Integer.MAX_VALUE;
            while(i < n && arr[i] != 0) {

                min = Math.min(arr[i], min);
                i++;
            }

            int j = start;
            while(j < i) {
                arr[j] -= min;
                j++;
            }
            ans += min;
        } start = 0 start 0 int count = 0; count 0 while(count < n && arr[start] == 0) {
                start++;
                count++;
            } count < n && arr[start] == 0 count < n count n arr[start] == 0 arr[start] arr start 0 {
                start++;
                count++;
            } start++ start count++ count if(count == n) {
                break;
            } count == n count n {
                break;
            } break; int i = start; i start int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE while(i < n && arr[i] != 0) {

                min = Math.min(arr[i], min);
                i++;
            } i < n && arr[i] != 0 i < n i n arr[i] != 0 arr[i] arr i 0 {

                min = Math.min(arr[i], min);
                i++;
            } min = Math.min(arr[i], min) min Math.min(arr[i], min) Math.min Math arr[i] arr i min i++ i int j = start; j start while(j < i) {
                arr[j] -= min;
                j++;
            } j < i j i {
                arr[j] -= min;
                j++;
            } arr[j] -= min arr[j] arr j min j++ j ans += min ans min 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) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = s.nextInt();
        }

        int start = 0;
        int ans = 0;
        while(true) {
            start = 0;
            int count = 0;
            while(count < n && arr[start] == 0) {
                start++;
                count++;
            }

            if(count == n) {
                break;
            }

            int i = start;
            int min = Integer.MAX_VALUE;
            while(i < n && arr[i] != 0) {

                min = Math.min(arr[i], min);
                i++;
            }

            int j = start;
            while(j < i) {
                arr[j] -= min;
                j++;
            }
            ans += min;
        }
        System.out.println(ans);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = s.nextInt();
        }

        int start = 0;
        int ans = 0;
        while(true) {
            start = 0;
            int count = 0;
            while(count < n && arr[start] == 0) {
                start++;
                count++;
            }

            if(count == n) {
                break;
            }

            int i = start;
            int min = Integer.MAX_VALUE;
            while(i < n && arr[i] != 0) {

                min = Math.min(arr[i], min);
                i++;
            }

            int j = start;
            while(j < i) {
                arr[j] -= min;
                j++;
            }
            ans += min;
        }
        System.out.println(ans);
    }
} Main
//package atcoder;

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


public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = s.nextInt();
        }

        int start = 0;
        int ans = 0;
        while(true) {
            start = 0;
            int count = 0;
            while(count < n && arr[start] == 0) {
                start++;
                count++;
            }

            if(count == n) {
                break;
            }

            int i = start;
            int min = Integer.MAX_VALUE;
            while(i < n && arr[i] != 0) {

                min = Math.min(arr[i], min);
                i++;
            }

            int j = start;
            while(j < i) {
                arr[j] -= min;
                j++;
            }
            ans += min;
        }
        System.out.println(ans);
    }
}
