import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        int[] h = new int[n];
        for(int i=0;i<n;i++)h[i] = Integer.parseInt(sc.next());
        sc.close();
        int idx1,idx2;
        int min;
        int res = 0;
        while(true){
            idx1 = -1;
            min = 1000000;
            for(int i=0;i<n;i++){
                if(h[i]>0){
                    idx1 = i;
                    min = h[i];
                    break;
                }
            }
            if(idx1==-1)break;
            idx2 = n;
            for(int i=idx1+1;i<n;i++){
                if(h[i]==0){
                    idx2 = i;
                    break;
                }else{
                    min = Math.min(h[i],min);
                }
            }
            for(int i=idx1;i<idx2;i++){
                h[i]-=min;
            }
            res+=min;
        }
        System.out.println(res);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        int[] h = new int[n];
        for(int i=0;i<n;i++)h[i] = Integer.parseInt(sc.next());
        sc.close();
        int idx1,idx2;
        int min;
        int res = 0;
        while(true){
            idx1 = -1;
            min = 1000000;
            for(int i=0;i<n;i++){
                if(h[i]>0){
                    idx1 = i;
                    min = h[i];
                    break;
                }
            }
            if(idx1==-1)break;
            idx2 = n;
            for(int i=idx1+1;i<n;i++){
                if(h[i]==0){
                    idx2 = i;
                    break;
                }else{
                    min = Math.min(h[i],min);
                }
            }
            for(int i=idx1;i<idx2;i++){
                h[i]-=min;
            }
            res+=min;
        }
        System.out.println(res);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        int[] h = new int[n];
        for(int i=0;i<n;i++)h[i] = Integer.parseInt(sc.next());
        sc.close();
        int idx1,idx2;
        int min;
        int res = 0;
        while(true){
            idx1 = -1;
            min = 1000000;
            for(int i=0;i<n;i++){
                if(h[i]>0){
                    idx1 = i;
                    min = h[i];
                    break;
                }
            }
            if(idx1==-1)break;
            idx2 = n;
            for(int i=idx1+1;i<n;i++){
                if(h[i]==0){
                    idx2 = i;
                    break;
                }else{
                    min = Math.min(h[i],min);
                }
            }
            for(int i=idx1;i<idx2;i++){
                h[i]-=min;
            }
            res+=min;
        }
        System.out.println(res);
    } main {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        int[] h = new int[n];
        for(int i=0;i<n;i++)h[i] = Integer.parseInt(sc.next());
        sc.close();
        int idx1,idx2;
        int min;
        int res = 0;
        while(true){
            idx1 = -1;
            min = 1000000;
            for(int i=0;i<n;i++){
                if(h[i]>0){
                    idx1 = i;
                    min = h[i];
                    break;
                }
            }
            if(idx1==-1)break;
            idx2 = n;
            for(int i=idx1+1;i<n;i++){
                if(h[i]==0){
                    idx2 = i;
                    break;
                }else{
                    min = Math.min(h[i],min);
                }
            }
            for(int i=idx1;i<idx2;i++){
                h[i]-=min;
            }
            res+=min;
        }
        System.out.println(res);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = Integer.parseInt(sc.next()); n Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int[] h = new int[n]; h new int[n] n for(int i=0;i<n;i++)h[i] = Integer.parseInt(sc.next()); int i=0; int i=0; i 0 i<n i n i++ i++ i h[i] = Integer.parseInt(sc.next()); h[i] = Integer.parseInt(sc.next()) h[i] h i Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc sc.close() sc.close sc int idx1 idx1 idx2; idx2 int min; min int res = 0; res 0 while(true){
            idx1 = -1;
            min = 1000000;
            for(int i=0;i<n;i++){
                if(h[i]>0){
                    idx1 = i;
                    min = h[i];
                    break;
                }
            }
            if(idx1==-1)break;
            idx2 = n;
            for(int i=idx1+1;i<n;i++){
                if(h[i]==0){
                    idx2 = i;
                    break;
                }else{
                    min = Math.min(h[i],min);
                }
            }
            for(int i=idx1;i<idx2;i++){
                h[i]-=min;
            }
            res+=min;
        } true {
            idx1 = -1;
            min = 1000000;
            for(int i=0;i<n;i++){
                if(h[i]>0){
                    idx1 = i;
                    min = h[i];
                    break;
                }
            }
            if(idx1==-1)break;
            idx2 = n;
            for(int i=idx1+1;i<n;i++){
                if(h[i]==0){
                    idx2 = i;
                    break;
                }else{
                    min = Math.min(h[i],min);
                }
            }
            for(int i=idx1;i<idx2;i++){
                h[i]-=min;
            }
            res+=min;
        } idx1 = -1 idx1 -1 1 min = 1000000 min 1000000 for(int i=0;i<n;i++){
                if(h[i]>0){
                    idx1 = i;
                    min = h[i];
                    break;
                }
            } int i=0; int i=0; i 0 i<n i n i++ i++ i {
                if(h[i]>0){
                    idx1 = i;
                    min = h[i];
                    break;
                }
            } {
                if(h[i]>0){
                    idx1 = i;
                    min = h[i];
                    break;
                }
            } if(h[i]>0){
                    idx1 = i;
                    min = h[i];
                    break;
                } h[i]>0 h[i] h i 0 {
                    idx1 = i;
                    min = h[i];
                    break;
                } idx1 = i idx1 i min = h[i] min h[i] h i break; if(idx1==-1)break; idx1==-1 idx1 -1 1 break; idx2 = n idx2 n for(int i=idx1+1;i<n;i++){
                if(h[i]==0){
                    idx2 = i;
                    break;
                }else{
                    min = Math.min(h[i],min);
                }
            } int i=idx1+1; int i=idx1+1; i idx1+1 idx1 1 i<n i n i++ i++ i {
                if(h[i]==0){
                    idx2 = i;
                    break;
                }else{
                    min = Math.min(h[i],min);
                }
            } {
                if(h[i]==0){
                    idx2 = i;
                    break;
                }else{
                    min = Math.min(h[i],min);
                }
            } if(h[i]==0){
                    idx2 = i;
                    break;
                }else{
                    min = Math.min(h[i],min);
                } h[i]==0 h[i] h i 0 {
                    idx2 = i;
                    break;
                } idx2 = i idx2 i break; {
                    min = Math.min(h[i],min);
                } min = Math.min(h[i],min) min Math.min(h[i],min) Math.min Math h[i] h i min for(int i=idx1;i<idx2;i++){
                h[i]-=min;
            } int i=idx1; int i=idx1; i idx1 i<idx2 i idx2 i++ i++ i {
                h[i]-=min;
            } {
                h[i]-=min;
            } h[i]-=min h[i] h i min res+=min res min System.out.println(res) System.out.println System.out System System.out res String[] args args public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        int[] h = new int[n];
        for(int i=0;i<n;i++)h[i] = Integer.parseInt(sc.next());
        sc.close();
        int idx1,idx2;
        int min;
        int res = 0;
        while(true){
            idx1 = -1;
            min = 1000000;
            for(int i=0;i<n;i++){
                if(h[i]>0){
                    idx1 = i;
                    min = h[i];
                    break;
                }
            }
            if(idx1==-1)break;
            idx2 = n;
            for(int i=idx1+1;i<n;i++){
                if(h[i]==0){
                    idx2 = i;
                    break;
                }else{
                    min = Math.min(h[i],min);
                }
            }
            for(int i=idx1;i<idx2;i++){
                h[i]-=min;
            }
            res+=min;
        }
        System.out.println(res);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        int[] h = new int[n];
        for(int i=0;i<n;i++)h[i] = Integer.parseInt(sc.next());
        sc.close();
        int idx1,idx2;
        int min;
        int res = 0;
        while(true){
            idx1 = -1;
            min = 1000000;
            for(int i=0;i<n;i++){
                if(h[i]>0){
                    idx1 = i;
                    min = h[i];
                    break;
                }
            }
            if(idx1==-1)break;
            idx2 = n;
            for(int i=idx1+1;i<n;i++){
                if(h[i]==0){
                    idx2 = i;
                    break;
                }else{
                    min = Math.min(h[i],min);
                }
            }
            for(int i=idx1;i<idx2;i++){
                h[i]-=min;
            }
            res+=min;
        }
        System.out.println(res);
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        int[] h = new int[n];
        for(int i=0;i<n;i++)h[i] = Integer.parseInt(sc.next());
        sc.close();
        int idx1,idx2;
        int min;
        int res = 0;
        while(true){
            idx1 = -1;
            min = 1000000;
            for(int i=0;i<n;i++){
                if(h[i]>0){
                    idx1 = i;
                    min = h[i];
                    break;
                }
            }
            if(idx1==-1)break;
            idx2 = n;
            for(int i=idx1+1;i<n;i++){
                if(h[i]==0){
                    idx2 = i;
                    break;
                }else{
                    min = Math.min(h[i],min);
                }
            }
            for(int i=idx1;i<idx2;i++){
                h[i]-=min;
            }
            res+=min;
        }
        System.out.println(res);
    }
}