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


public class Main {

	final static long MOD = 1000000007;
	public static String[] Ws = null;
	public static int wsIndx = 0;
	public static BufferedReader in = null;
	public static long cnt=0;
	public static PrintWriter pw = new PrintWriter(System.out);
	public static void main(String[] args) throws Exception {
		solutionA(args);
		if(in!=null){
			in.close();
		}
		pw.flush();
		pw.close();
	}

	public static void solutionA(String[] args) throws Exception{

		int N = nextInt();
		int[] h = nextInts();
		int ans = 0;
		int idx=0;
		for(int i=idx;i<N;i++) {
			if(i==0) {
				if(h[i]==0) {
					idx++;
				}else {
					break;
				}
			}else {
				if(h[i]==0||h[i-1]==0&h[i]==0) {
					idx++;
				}else {
					break;
				}
			}
		}

		while(idx!=N) {
			int min=Integer.MAX_VALUE;
			int end=N;
			for(int i=idx;i<N;i++) {
				if(h[i]!=0) {
					min=Math.min(min, h[i]);
				}else {
					end=i;
					break;
				}
			}
			for(int i=idx;i<end;i++) {
				h[i]-=min;
			}
			ans+=min;
			for(int i=idx;i<N;i++) {
				if(i==0) {
					if(h[i]==0) {
						idx++;
					}else {
						break;
					}
				}else {
					if(h[i]==0||h[i-1]==0&h[i]==0) {
						idx++;
					}else {
						break;
					}
				}
			}
			if(idx==N) {
				break;
			}
		}
		pw.println(ans);
	}

	public static void check() throws Exception{
		if(in == null){
			in = new BufferedReader(new InputStreamReader(System.in));
		}
		if(Ws==null || Ws.length<=wsIndx){
			Ws = in.readLine().split(" ");
			wsIndx=0;
		}
	}
	public static int nextInt()throws Exception{
		check();
		return Integer.parseInt(Ws[wsIndx++]);
	}

	public static long nextLong()throws Exception{
		check();
		return Long.parseLong(Ws[wsIndx++]);
	}

	public static String nextString()throws Exception{
		check();
		return Ws[wsIndx++];
	}

	public static int[] nextInts()throws Exception{
		check();
		int[] tmp = new int[Ws.length];
		for(int i=0;i<tmp.length;i++){
			tmp[i]=Integer.parseInt(Ws[i]);
		}
		wsIndx=Ws.length;
		return tmp;
	}

	public static long[] nextLongs()throws Exception{
		check();
		long[] tmp = new long[Ws.length];
		for(int i=0;i<tmp.length;i++){
			tmp[i]=Long.parseLong(Ws[i]);
		}
		wsIndx=Ws.length;
		return tmp;
	}

	public static String[] nextStrings()throws Exception{
		check();
		wsIndx=Ws.length;
		return Ws;
	}

}
 import java.io.*; io.* java import java.util.*; util.* java import java.util.regex.*; regex.* java.util public class Main {

	final static long MOD = 1000000007;
	public static String[] Ws = null;
	public static int wsIndx = 0;
	public static BufferedReader in = null;
	public static long cnt=0;
	public static PrintWriter pw = new PrintWriter(System.out);
	public static void main(String[] args) throws Exception {
		solutionA(args);
		if(in!=null){
			in.close();
		}
		pw.flush();
		pw.close();
	}

	public static void solutionA(String[] args) throws Exception{

		int N = nextInt();
		int[] h = nextInts();
		int ans = 0;
		int idx=0;
		for(int i=idx;i<N;i++) {
			if(i==0) {
				if(h[i]==0) {
					idx++;
				}else {
					break;
				}
			}else {
				if(h[i]==0||h[i-1]==0&h[i]==0) {
					idx++;
				}else {
					break;
				}
			}
		}

		while(idx!=N) {
			int min=Integer.MAX_VALUE;
			int end=N;
			for(int i=idx;i<N;i++) {
				if(h[i]!=0) {
					min=Math.min(min, h[i]);
				}else {
					end=i;
					break;
				}
			}
			for(int i=idx;i<end;i++) {
				h[i]-=min;
			}
			ans+=min;
			for(int i=idx;i<N;i++) {
				if(i==0) {
					if(h[i]==0) {
						idx++;
					}else {
						break;
					}
				}else {
					if(h[i]==0||h[i-1]==0&h[i]==0) {
						idx++;
					}else {
						break;
					}
				}
			}
			if(idx==N) {
				break;
			}
		}
		pw.println(ans);
	}

	public static void check() throws Exception{
		if(in == null){
			in = new BufferedReader(new InputStreamReader(System.in));
		}
		if(Ws==null || Ws.length<=wsIndx){
			Ws = in.readLine().split(" ");
			wsIndx=0;
		}
	}
	public static int nextInt()throws Exception{
		check();
		return Integer.parseInt(Ws[wsIndx++]);
	}

	public static long nextLong()throws Exception{
		check();
		return Long.parseLong(Ws[wsIndx++]);
	}

	public static String nextString()throws Exception{
		check();
		return Ws[wsIndx++];
	}

	public static int[] nextInts()throws Exception{
		check();
		int[] tmp = new int[Ws.length];
		for(int i=0;i<tmp.length;i++){
			tmp[i]=Integer.parseInt(Ws[i]);
		}
		wsIndx=Ws.length;
		return tmp;
	}

	public static long[] nextLongs()throws Exception{
		check();
		long[] tmp = new long[Ws.length];
		for(int i=0;i<tmp.length;i++){
			tmp[i]=Long.parseLong(Ws[i]);
		}
		wsIndx=Ws.length;
		return tmp;
	}

	public static String[] nextStrings()throws Exception{
		check();
		wsIndx=Ws.length;
		return Ws;
	}

} Main final static long MOD = 1000000007; MOD 1000000007 public static String[] Ws = null; Ws null public static int wsIndx = 0; wsIndx 0 public static BufferedReader in = null; in null public static long cnt=0; cnt 0 public static PrintWriter pw = new PrintWriter(System.out); pw new PrintWriter(System.out) public static void main(String[] args) throws Exception {
		solutionA(args);
		if(in!=null){
			in.close();
		}
		pw.flush();
		pw.close();
	} main {
		solutionA(args);
		if(in!=null){
			in.close();
		}
		pw.flush();
		pw.close();
	} solutionA(args) solutionA args if(in!=null){
			in.close();
		} in!=null in null {
			in.close();
		} in.close() in.close in pw.flush() pw.flush pw pw.close() pw.close pw String[] args args public static void solutionA(String[] args) throws Exception{

		int N = nextInt();
		int[] h = nextInts();
		int ans = 0;
		int idx=0;
		for(int i=idx;i<N;i++) {
			if(i==0) {
				if(h[i]==0) {
					idx++;
				}else {
					break;
				}
			}else {
				if(h[i]==0||h[i-1]==0&h[i]==0) {
					idx++;
				}else {
					break;
				}
			}
		}

		while(idx!=N) {
			int min=Integer.MAX_VALUE;
			int end=N;
			for(int i=idx;i<N;i++) {
				if(h[i]!=0) {
					min=Math.min(min, h[i]);
				}else {
					end=i;
					break;
				}
			}
			for(int i=idx;i<end;i++) {
				h[i]-=min;
			}
			ans+=min;
			for(int i=idx;i<N;i++) {
				if(i==0) {
					if(h[i]==0) {
						idx++;
					}else {
						break;
					}
				}else {
					if(h[i]==0||h[i-1]==0&h[i]==0) {
						idx++;
					}else {
						break;
					}
				}
			}
			if(idx==N) {
				break;
			}
		}
		pw.println(ans);
	} solutionA {

		int N = nextInt();
		int[] h = nextInts();
		int ans = 0;
		int idx=0;
		for(int i=idx;i<N;i++) {
			if(i==0) {
				if(h[i]==0) {
					idx++;
				}else {
					break;
				}
			}else {
				if(h[i]==0||h[i-1]==0&h[i]==0) {
					idx++;
				}else {
					break;
				}
			}
		}

		while(idx!=N) {
			int min=Integer.MAX_VALUE;
			int end=N;
			for(int i=idx;i<N;i++) {
				if(h[i]!=0) {
					min=Math.min(min, h[i]);
				}else {
					end=i;
					break;
				}
			}
			for(int i=idx;i<end;i++) {
				h[i]-=min;
			}
			ans+=min;
			for(int i=idx;i<N;i++) {
				if(i==0) {
					if(h[i]==0) {
						idx++;
					}else {
						break;
					}
				}else {
					if(h[i]==0||h[i-1]==0&h[i]==0) {
						idx++;
					}else {
						break;
					}
				}
			}
			if(idx==N) {
				break;
			}
		}
		pw.println(ans);
	} int N = nextInt(); N nextInt() nextInt int[] h = nextInts(); h nextInts() nextInts int ans = 0; ans 0 int idx=0; idx 0 for(int i=idx;i<N;i++) {
			if(i==0) {
				if(h[i]==0) {
					idx++;
				}else {
					break;
				}
			}else {
				if(h[i]==0||h[i-1]==0&h[i]==0) {
					idx++;
				}else {
					break;
				}
			}
		} int i=idx; int i=idx; i idx i<N i N i++ i++ i {
			if(i==0) {
				if(h[i]==0) {
					idx++;
				}else {
					break;
				}
			}else {
				if(h[i]==0||h[i-1]==0&h[i]==0) {
					idx++;
				}else {
					break;
				}
			}
		} {
			if(i==0) {
				if(h[i]==0) {
					idx++;
				}else {
					break;
				}
			}else {
				if(h[i]==0||h[i-1]==0&h[i]==0) {
					idx++;
				}else {
					break;
				}
			}
		} if(i==0) {
				if(h[i]==0) {
					idx++;
				}else {
					break;
				}
			}else {
				if(h[i]==0||h[i-1]==0&h[i]==0) {
					idx++;
				}else {
					break;
				}
			} i==0 i 0 {
				if(h[i]==0) {
					idx++;
				}else {
					break;
				}
			} if(h[i]==0) {
					idx++;
				}else {
					break;
				} h[i]==0 h[i] h i 0 {
					idx++;
				} idx++ idx {
					break;
				} break; {
				if(h[i]==0||h[i-1]==0&h[i]==0) {
					idx++;
				}else {
					break;
				}
			} if(h[i]==0||h[i-1]==0&h[i]==0) {
					idx++;
				}else {
					break;
				} h[i]==0||h[i-1]==0&h[i]==0 h[i]==0 h[i] h i 0 h[i-1]==0&h[i]==0 h[i-1]==0 h[i-1] h i-1 i 1 0 h[i]==0 h[i] h i 0 {
					idx++;
				} idx++ idx {
					break;
				} break; while(idx!=N) {
			int min=Integer.MAX_VALUE;
			int end=N;
			for(int i=idx;i<N;i++) {
				if(h[i]!=0) {
					min=Math.min(min, h[i]);
				}else {
					end=i;
					break;
				}
			}
			for(int i=idx;i<end;i++) {
				h[i]-=min;
			}
			ans+=min;
			for(int i=idx;i<N;i++) {
				if(i==0) {
					if(h[i]==0) {
						idx++;
					}else {
						break;
					}
				}else {
					if(h[i]==0||h[i-1]==0&h[i]==0) {
						idx++;
					}else {
						break;
					}
				}
			}
			if(idx==N) {
				break;
			}
		} idx!=N idx N {
			int min=Integer.MAX_VALUE;
			int end=N;
			for(int i=idx;i<N;i++) {
				if(h[i]!=0) {
					min=Math.min(min, h[i]);
				}else {
					end=i;
					break;
				}
			}
			for(int i=idx;i<end;i++) {
				h[i]-=min;
			}
			ans+=min;
			for(int i=idx;i<N;i++) {
				if(i==0) {
					if(h[i]==0) {
						idx++;
					}else {
						break;
					}
				}else {
					if(h[i]==0||h[i-1]==0&h[i]==0) {
						idx++;
					}else {
						break;
					}
				}
			}
			if(idx==N) {
				break;
			}
		} int min=Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE int end=N; end N for(int i=idx;i<N;i++) {
				if(h[i]!=0) {
					min=Math.min(min, h[i]);
				}else {
					end=i;
					break;
				}
			} int i=idx; int i=idx; i idx i<N i N i++ i++ i {
				if(h[i]!=0) {
					min=Math.min(min, h[i]);
				}else {
					end=i;
					break;
				}
			} {
				if(h[i]!=0) {
					min=Math.min(min, h[i]);
				}else {
					end=i;
					break;
				}
			} if(h[i]!=0) {
					min=Math.min(min, h[i]);
				}else {
					end=i;
					break;
				} h[i]!=0 h[i] h i 0 {
					min=Math.min(min, h[i]);
				} min=Math.min(min, h[i]) min Math.min(min, h[i]) Math.min Math min h[i] h i {
					end=i;
					break;
				} end=i end i break; for(int i=idx;i<end;i++) {
				h[i]-=min;
			} int i=idx; int i=idx; i idx i<end i end i++ i++ i {
				h[i]-=min;
			} {
				h[i]-=min;
			} h[i]-=min h[i] h i min ans+=min ans min for(int i=idx;i<N;i++) {
				if(i==0) {
					if(h[i]==0) {
						idx++;
					}else {
						break;
					}
				}else {
					if(h[i]==0||h[i-1]==0&h[i]==0) {
						idx++;
					}else {
						break;
					}
				}
			} int i=idx; int i=idx; i idx i<N i N i++ i++ i {
				if(i==0) {
					if(h[i]==0) {
						idx++;
					}else {
						break;
					}
				}else {
					if(h[i]==0||h[i-1]==0&h[i]==0) {
						idx++;
					}else {
						break;
					}
				}
			} {
				if(i==0) {
					if(h[i]==0) {
						idx++;
					}else {
						break;
					}
				}else {
					if(h[i]==0||h[i-1]==0&h[i]==0) {
						idx++;
					}else {
						break;
					}
				}
			} if(i==0) {
					if(h[i]==0) {
						idx++;
					}else {
						break;
					}
				}else {
					if(h[i]==0||h[i-1]==0&h[i]==0) {
						idx++;
					}else {
						break;
					}
				} i==0 i 0 {
					if(h[i]==0) {
						idx++;
					}else {
						break;
					}
				} if(h[i]==0) {
						idx++;
					}else {
						break;
					} h[i]==0 h[i] h i 0 {
						idx++;
					} idx++ idx {
						break;
					} break; {
					if(h[i]==0||h[i-1]==0&h[i]==0) {
						idx++;
					}else {
						break;
					}
				} if(h[i]==0||h[i-1]==0&h[i]==0) {
						idx++;
					}else {
						break;
					} h[i]==0||h[i-1]==0&h[i]==0 h[i]==0 h[i] h i 0 h[i-1]==0&h[i]==0 h[i-1]==0 h[i-1] h i-1 i 1 0 h[i]==0 h[i] h i 0 {
						idx++;
					} idx++ idx {
						break;
					} break; if(idx==N) {
				break;
			} idx==N idx N {
				break;
			} break; pw.println(ans) pw.println pw ans String[] args args public static void check() throws Exception{
		if(in == null){
			in = new BufferedReader(new InputStreamReader(System.in));
		}
		if(Ws==null || Ws.length<=wsIndx){
			Ws = in.readLine().split(" ");
			wsIndx=0;
		}
	} check {
		if(in == null){
			in = new BufferedReader(new InputStreamReader(System.in));
		}
		if(Ws==null || Ws.length<=wsIndx){
			Ws = in.readLine().split(" ");
			wsIndx=0;
		}
	} if(in == null){
			in = new BufferedReader(new InputStreamReader(System.in));
		} in == null in null {
			in = new BufferedReader(new InputStreamReader(System.in));
		} in = new BufferedReader(new InputStreamReader(System.in)) in new BufferedReader(new InputStreamReader(System.in)) if(Ws==null || Ws.length<=wsIndx){
			Ws = in.readLine().split(" ");
			wsIndx=0;
		} Ws==null || Ws.length<=wsIndx Ws==null Ws null Ws.length<=wsIndx Ws.length Ws Ws.length wsIndx {
			Ws = in.readLine().split(" ");
			wsIndx=0;
		} Ws = in.readLine().split(" ") Ws in.readLine().split(" ") in.readLine().split in.readLine() in.readLine in " " wsIndx=0 wsIndx 0 public static int nextInt()throws Exception{
		check();
		return Integer.parseInt(Ws[wsIndx++]);
	} nextInt {
		check();
		return Integer.parseInt(Ws[wsIndx++]);
	} check() check return Integer.parseInt(Ws[wsIndx++]); Integer.parseInt(Ws[wsIndx++]) Integer.parseInt Integer Ws[wsIndx++] Ws wsIndx++ wsIndx public static long nextLong()throws Exception{
		check();
		return Long.parseLong(Ws[wsIndx++]);
	} nextLong {
		check();
		return Long.parseLong(Ws[wsIndx++]);
	} check() check return Long.parseLong(Ws[wsIndx++]); Long.parseLong(Ws[wsIndx++]) Long.parseLong Long Ws[wsIndx++] Ws wsIndx++ wsIndx public static String nextString()throws Exception{
		check();
		return Ws[wsIndx++];
	} nextString {
		check();
		return Ws[wsIndx++];
	} check() check return Ws[wsIndx++]; Ws[wsIndx++] Ws wsIndx++ wsIndx public static int[] nextInts()throws Exception{
		check();
		int[] tmp = new int[Ws.length];
		for(int i=0;i<tmp.length;i++){
			tmp[i]=Integer.parseInt(Ws[i]);
		}
		wsIndx=Ws.length;
		return tmp;
	} nextInts {
		check();
		int[] tmp = new int[Ws.length];
		for(int i=0;i<tmp.length;i++){
			tmp[i]=Integer.parseInt(Ws[i]);
		}
		wsIndx=Ws.length;
		return tmp;
	} check() check int[] tmp = new int[Ws.length]; tmp new int[Ws.length] Ws.length Ws Ws.length for(int i=0;i<tmp.length;i++){
			tmp[i]=Integer.parseInt(Ws[i]);
		} int i=0; int i=0; i 0 i<tmp.length i tmp.length tmp tmp.length i++ i++ i {
			tmp[i]=Integer.parseInt(Ws[i]);
		} {
			tmp[i]=Integer.parseInt(Ws[i]);
		} tmp[i]=Integer.parseInt(Ws[i]) tmp[i] tmp i Integer.parseInt(Ws[i]) Integer.parseInt Integer Ws[i] Ws i wsIndx=Ws.length wsIndx Ws.length Ws Ws.length return tmp; tmp public static long[] nextLongs()throws Exception{
		check();
		long[] tmp = new long[Ws.length];
		for(int i=0;i<tmp.length;i++){
			tmp[i]=Long.parseLong(Ws[i]);
		}
		wsIndx=Ws.length;
		return tmp;
	} nextLongs {
		check();
		long[] tmp = new long[Ws.length];
		for(int i=0;i<tmp.length;i++){
			tmp[i]=Long.parseLong(Ws[i]);
		}
		wsIndx=Ws.length;
		return tmp;
	} check() check long[] tmp = new long[Ws.length]; tmp new long[Ws.length] Ws.length Ws Ws.length for(int i=0;i<tmp.length;i++){
			tmp[i]=Long.parseLong(Ws[i]);
		} int i=0; int i=0; i 0 i<tmp.length i tmp.length tmp tmp.length i++ i++ i {
			tmp[i]=Long.parseLong(Ws[i]);
		} {
			tmp[i]=Long.parseLong(Ws[i]);
		} tmp[i]=Long.parseLong(Ws[i]) tmp[i] tmp i Long.parseLong(Ws[i]) Long.parseLong Long Ws[i] Ws i wsIndx=Ws.length wsIndx Ws.length Ws Ws.length return tmp; tmp public static String[] nextStrings()throws Exception{
		check();
		wsIndx=Ws.length;
		return Ws;
	} nextStrings {
		check();
		wsIndx=Ws.length;
		return Ws;
	} check() check wsIndx=Ws.length wsIndx Ws.length Ws Ws.length return Ws; Ws public class Main {

	final static long MOD = 1000000007;
	public static String[] Ws = null;
	public static int wsIndx = 0;
	public static BufferedReader in = null;
	public static long cnt=0;
	public static PrintWriter pw = new PrintWriter(System.out);
	public static void main(String[] args) throws Exception {
		solutionA(args);
		if(in!=null){
			in.close();
		}
		pw.flush();
		pw.close();
	}

	public static void solutionA(String[] args) throws Exception{

		int N = nextInt();
		int[] h = nextInts();
		int ans = 0;
		int idx=0;
		for(int i=idx;i<N;i++) {
			if(i==0) {
				if(h[i]==0) {
					idx++;
				}else {
					break;
				}
			}else {
				if(h[i]==0||h[i-1]==0&h[i]==0) {
					idx++;
				}else {
					break;
				}
			}
		}

		while(idx!=N) {
			int min=Integer.MAX_VALUE;
			int end=N;
			for(int i=idx;i<N;i++) {
				if(h[i]!=0) {
					min=Math.min(min, h[i]);
				}else {
					end=i;
					break;
				}
			}
			for(int i=idx;i<end;i++) {
				h[i]-=min;
			}
			ans+=min;
			for(int i=idx;i<N;i++) {
				if(i==0) {
					if(h[i]==0) {
						idx++;
					}else {
						break;
					}
				}else {
					if(h[i]==0||h[i-1]==0&h[i]==0) {
						idx++;
					}else {
						break;
					}
				}
			}
			if(idx==N) {
				break;
			}
		}
		pw.println(ans);
	}

	public static void check() throws Exception{
		if(in == null){
			in = new BufferedReader(new InputStreamReader(System.in));
		}
		if(Ws==null || Ws.length<=wsIndx){
			Ws = in.readLine().split(" ");
			wsIndx=0;
		}
	}
	public static int nextInt()throws Exception{
		check();
		return Integer.parseInt(Ws[wsIndx++]);
	}

	public static long nextLong()throws Exception{
		check();
		return Long.parseLong(Ws[wsIndx++]);
	}

	public static String nextString()throws Exception{
		check();
		return Ws[wsIndx++];
	}

	public static int[] nextInts()throws Exception{
		check();
		int[] tmp = new int[Ws.length];
		for(int i=0;i<tmp.length;i++){
			tmp[i]=Integer.parseInt(Ws[i]);
		}
		wsIndx=Ws.length;
		return tmp;
	}

	public static long[] nextLongs()throws Exception{
		check();
		long[] tmp = new long[Ws.length];
		for(int i=0;i<tmp.length;i++){
			tmp[i]=Long.parseLong(Ws[i]);
		}
		wsIndx=Ws.length;
		return tmp;
	}

	public static String[] nextStrings()throws Exception{
		check();
		wsIndx=Ws.length;
		return Ws;
	}

} public class Main {

	final static long MOD = 1000000007;
	public static String[] Ws = null;
	public static int wsIndx = 0;
	public static BufferedReader in = null;
	public static long cnt=0;
	public static PrintWriter pw = new PrintWriter(System.out);
	public static void main(String[] args) throws Exception {
		solutionA(args);
		if(in!=null){
			in.close();
		}
		pw.flush();
		pw.close();
	}

	public static void solutionA(String[] args) throws Exception{

		int N = nextInt();
		int[] h = nextInts();
		int ans = 0;
		int idx=0;
		for(int i=idx;i<N;i++) {
			if(i==0) {
				if(h[i]==0) {
					idx++;
				}else {
					break;
				}
			}else {
				if(h[i]==0||h[i-1]==0&h[i]==0) {
					idx++;
				}else {
					break;
				}
			}
		}

		while(idx!=N) {
			int min=Integer.MAX_VALUE;
			int end=N;
			for(int i=idx;i<N;i++) {
				if(h[i]!=0) {
					min=Math.min(min, h[i]);
				}else {
					end=i;
					break;
				}
			}
			for(int i=idx;i<end;i++) {
				h[i]-=min;
			}
			ans+=min;
			for(int i=idx;i<N;i++) {
				if(i==0) {
					if(h[i]==0) {
						idx++;
					}else {
						break;
					}
				}else {
					if(h[i]==0||h[i-1]==0&h[i]==0) {
						idx++;
					}else {
						break;
					}
				}
			}
			if(idx==N) {
				break;
			}
		}
		pw.println(ans);
	}

	public static void check() throws Exception{
		if(in == null){
			in = new BufferedReader(new InputStreamReader(System.in));
		}
		if(Ws==null || Ws.length<=wsIndx){
			Ws = in.readLine().split(" ");
			wsIndx=0;
		}
	}
	public static int nextInt()throws Exception{
		check();
		return Integer.parseInt(Ws[wsIndx++]);
	}

	public static long nextLong()throws Exception{
		check();
		return Long.parseLong(Ws[wsIndx++]);
	}

	public static String nextString()throws Exception{
		check();
		return Ws[wsIndx++];
	}

	public static int[] nextInts()throws Exception{
		check();
		int[] tmp = new int[Ws.length];
		for(int i=0;i<tmp.length;i++){
			tmp[i]=Integer.parseInt(Ws[i]);
		}
		wsIndx=Ws.length;
		return tmp;
	}

	public static long[] nextLongs()throws Exception{
		check();
		long[] tmp = new long[Ws.length];
		for(int i=0;i<tmp.length;i++){
			tmp[i]=Long.parseLong(Ws[i]);
		}
		wsIndx=Ws.length;
		return tmp;
	}

	public static String[] nextStrings()throws Exception{
		check();
		wsIndx=Ws.length;
		return Ws;
	}

} Main
import java.io.*;
import java.util.*;
import java.util.regex.*;


public class Main {

	final static long MOD = 1000000007;
	public static String[] Ws = null;
	public static int wsIndx = 0;
	public static BufferedReader in = null;
	public static long cnt=0;
	public static PrintWriter pw = new PrintWriter(System.out);
	public static void main(String[] args) throws Exception {
		solutionA(args);
		if(in!=null){
			in.close();
		}
		pw.flush();
		pw.close();
	}

	public static void solutionA(String[] args) throws Exception{

		int N = nextInt();
		int[] h = nextInts();
		int ans = 0;
		int idx=0;
		for(int i=idx;i<N;i++) {
			if(i==0) {
				if(h[i]==0) {
					idx++;
				}else {
					break;
				}
			}else {
				if(h[i]==0||h[i-1]==0&h[i]==0) {
					idx++;
				}else {
					break;
				}
			}
		}

		while(idx!=N) {
			int min=Integer.MAX_VALUE;
			int end=N;
			for(int i=idx;i<N;i++) {
				if(h[i]!=0) {
					min=Math.min(min, h[i]);
				}else {
					end=i;
					break;
				}
			}
			for(int i=idx;i<end;i++) {
				h[i]-=min;
			}
			ans+=min;
			for(int i=idx;i<N;i++) {
				if(i==0) {
					if(h[i]==0) {
						idx++;
					}else {
						break;
					}
				}else {
					if(h[i]==0||h[i-1]==0&h[i]==0) {
						idx++;
					}else {
						break;
					}
				}
			}
			if(idx==N) {
				break;
			}
		}
		pw.println(ans);
	}

	public static void check() throws Exception{
		if(in == null){
			in = new BufferedReader(new InputStreamReader(System.in));
		}
		if(Ws==null || Ws.length<=wsIndx){
			Ws = in.readLine().split(" ");
			wsIndx=0;
		}
	}
	public static int nextInt()throws Exception{
		check();
		return Integer.parseInt(Ws[wsIndx++]);
	}

	public static long nextLong()throws Exception{
		check();
		return Long.parseLong(Ws[wsIndx++]);
	}

	public static String nextString()throws Exception{
		check();
		return Ws[wsIndx++];
	}

	public static int[] nextInts()throws Exception{
		check();
		int[] tmp = new int[Ws.length];
		for(int i=0;i<tmp.length;i++){
			tmp[i]=Integer.parseInt(Ws[i]);
		}
		wsIndx=Ws.length;
		return tmp;
	}

	public static long[] nextLongs()throws Exception{
		check();
		long[] tmp = new long[Ws.length];
		for(int i=0;i<tmp.length;i++){
			tmp[i]=Long.parseLong(Ws[i]);
		}
		wsIndx=Ws.length;
		return tmp;
	}

	public static String[] nextStrings()throws Exception{
		check();
		wsIndx=Ws.length;
		return Ws;
	}

}

