import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

	static class Task {
        public void solve(InputReader in, PrintWriter out) {
        	int n = in.nextInt();
        	if(n==1) {
        		out.println(in.nextInt());
        		return;
        	}
        	int ans = 0;
        	int prev = in.nextInt();
        	for(int i=1; i<n; i++) {
        		int curr = in.nextInt();
        		if(prev > curr)
        			ans+=prev-curr;
        		if(i==n-1)
        			ans+=curr;
        		prev = curr;
        	}
        	out.println(ans);
        }
        
        public void solve_again(InputReader in, PrintWriter out) {
        	int n = in.nextInt();
        	int curr = in.nextInt();
        	int ans = curr;
        	for(int i=1; i<n; i++) {
        		int next = in.nextInt();
        		if(next > curr)
        			ans+=next-curr;
        		curr = next;
        	}
        	out.println(ans);
        }
        
    }
	
    public static void main(String argds[]) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        solver.solve(in, out);
        out.close();
    }

	static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;
        
        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }
        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
 
        public int nextInt() {
            return Integer.parseInt(next());
        }
 
        public long nextLong() {
            return Long.parseLong(next());
        }
    }	
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStream; InputStream java.io import java.io.InputStreamReader; InputStreamReader java.io import java.io.OutputStream; OutputStream java.io import java.io.PrintWriter; PrintWriter java.io import java.util.StringTokenizer; StringTokenizer java.util public class Main {

	static class Task {
        public void solve(InputReader in, PrintWriter out) {
        	int n = in.nextInt();
        	if(n==1) {
        		out.println(in.nextInt());
        		return;
        	}
        	int ans = 0;
        	int prev = in.nextInt();
        	for(int i=1; i<n; i++) {
        		int curr = in.nextInt();
        		if(prev > curr)
        			ans+=prev-curr;
        		if(i==n-1)
        			ans+=curr;
        		prev = curr;
        	}
        	out.println(ans);
        }
        
        public void solve_again(InputReader in, PrintWriter out) {
        	int n = in.nextInt();
        	int curr = in.nextInt();
        	int ans = curr;
        	for(int i=1; i<n; i++) {
        		int next = in.nextInt();
        		if(next > curr)
        			ans+=next-curr;
        		curr = next;
        	}
        	out.println(ans);
        }
        
    }
	
    public static void main(String argds[]) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        solver.solve(in, out);
        out.close();
    }

	static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;
        
        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }
        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
 
        public int nextInt() {
            return Integer.parseInt(next());
        }
 
        public long nextLong() {
            return Long.parseLong(next());
        }
    }	
} Main public static void main(String argds[]) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        solver.solve(in, out);
        out.close();
    } main {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        solver.solve(in, out);
        out.close();
    } InputStream inputStream = System.in; inputStream System.in System System.in OutputStream outputStream = System.out; outputStream System.out System System.out InputReader in = new InputReader(inputStream); in new InputReader(inputStream) PrintWriter out = new PrintWriter(outputStream); out new PrintWriter(outputStream) Task solver = new Task(); solver new Task() solver.solve(in, out) solver.solve solver in out out.close() out.close out String argds[] argds static class Task {
        public void solve(InputReader in, PrintWriter out) {
        	int n = in.nextInt();
        	if(n==1) {
        		out.println(in.nextInt());
        		return;
        	}
        	int ans = 0;
        	int prev = in.nextInt();
        	for(int i=1; i<n; i++) {
        		int curr = in.nextInt();
        		if(prev > curr)
        			ans+=prev-curr;
        		if(i==n-1)
        			ans+=curr;
        		prev = curr;
        	}
        	out.println(ans);
        }
        
        public void solve_again(InputReader in, PrintWriter out) {
        	int n = in.nextInt();
        	int curr = in.nextInt();
        	int ans = curr;
        	for(int i=1; i<n; i++) {
        		int next = in.nextInt();
        		if(next > curr)
        			ans+=next-curr;
        		curr = next;
        	}
        	out.println(ans);
        }
        
    } Task public void solve(InputReader in, PrintWriter out) {
        	int n = in.nextInt();
        	if(n==1) {
        		out.println(in.nextInt());
        		return;
        	}
        	int ans = 0;
        	int prev = in.nextInt();
        	for(int i=1; i<n; i++) {
        		int curr = in.nextInt();
        		if(prev > curr)
        			ans+=prev-curr;
        		if(i==n-1)
        			ans+=curr;
        		prev = curr;
        	}
        	out.println(ans);
        } solve {
        	int n = in.nextInt();
        	if(n==1) {
        		out.println(in.nextInt());
        		return;
        	}
        	int ans = 0;
        	int prev = in.nextInt();
        	for(int i=1; i<n; i++) {
        		int curr = in.nextInt();
        		if(prev > curr)
        			ans+=prev-curr;
        		if(i==n-1)
        			ans+=curr;
        		prev = curr;
        	}
        	out.println(ans);
        } int n = in.nextInt(); n in.nextInt() in.nextInt in if(n==1) {
        		out.println(in.nextInt());
        		return;
        	} n==1 n 1 {
        		out.println(in.nextInt());
        		return;
        	} out.println(in.nextInt()) out.println out in.nextInt() in.nextInt in return; int ans = 0; ans 0 int prev = in.nextInt(); prev in.nextInt() in.nextInt in for(int i=1; i<n; i++) {
        		int curr = in.nextInt();
        		if(prev > curr)
        			ans+=prev-curr;
        		if(i==n-1)
        			ans+=curr;
        		prev = curr;
        	} int i=1; int i=1; i 1 i<n i n i++ i++ i {
        		int curr = in.nextInt();
        		if(prev > curr)
        			ans+=prev-curr;
        		if(i==n-1)
        			ans+=curr;
        		prev = curr;
        	} {
        		int curr = in.nextInt();
        		if(prev > curr)
        			ans+=prev-curr;
        		if(i==n-1)
        			ans+=curr;
        		prev = curr;
        	} int curr = in.nextInt(); curr in.nextInt() in.nextInt in if(prev > curr)
        			ans+=prev-curr; prev > curr prev curr ans+=prev-curr ans prev-curr prev curr if(i==n-1)
        			ans+=curr; i==n-1 i n-1 n 1 ans+=curr ans curr prev = curr prev curr out.println(ans) out.println out ans InputReader in in PrintWriter out out public void solve_again(InputReader in, PrintWriter out) {
        	int n = in.nextInt();
        	int curr = in.nextInt();
        	int ans = curr;
        	for(int i=1; i<n; i++) {
        		int next = in.nextInt();
        		if(next > curr)
        			ans+=next-curr;
        		curr = next;
        	}
        	out.println(ans);
        } solve_again {
        	int n = in.nextInt();
        	int curr = in.nextInt();
        	int ans = curr;
        	for(int i=1; i<n; i++) {
        		int next = in.nextInt();
        		if(next > curr)
        			ans+=next-curr;
        		curr = next;
        	}
        	out.println(ans);
        } int n = in.nextInt(); n in.nextInt() in.nextInt in int curr = in.nextInt(); curr in.nextInt() in.nextInt in int ans = curr; ans curr for(int i=1; i<n; i++) {
        		int next = in.nextInt();
        		if(next > curr)
        			ans+=next-curr;
        		curr = next;
        	} int i=1; int i=1; i 1 i<n i n i++ i++ i {
        		int next = in.nextInt();
        		if(next > curr)
        			ans+=next-curr;
        		curr = next;
        	} {
        		int next = in.nextInt();
        		if(next > curr)
        			ans+=next-curr;
        		curr = next;
        	} int next = in.nextInt(); next in.nextInt() in.nextInt in if(next > curr)
        			ans+=next-curr; next > curr next curr ans+=next-curr ans next-curr next curr curr = next curr next out.println(ans) out.println out ans InputReader in in PrintWriter out out static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;
        
        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }
        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
 
        public int nextInt() {
            return Integer.parseInt(next());
        }
 
        public long nextLong() {
            return Long.parseLong(next());
        }
    } InputReader public BufferedReader reader; reader public StringTokenizer tokenizer; tokenizer public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        } InputReader {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        } reader = new BufferedReader(new InputStreamReader(stream), 32768) reader new BufferedReader(new InputStreamReader(stream), 32768) tokenizer = null tokenizer null InputStream stream stream public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        } next {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        } while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } tokenizer == null || !tokenizer.hasMoreTokens() tokenizer == null tokenizer null !tokenizer.hasMoreTokens() tokenizer.hasMoreTokens() tokenizer.hasMoreTokens tokenizer {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                } IOException e {
                    throw new RuntimeException(e);
                } throw new RuntimeException(e); new RuntimeException(e) {
                    tokenizer = new StringTokenizer(reader.readLine());
                } tokenizer = new StringTokenizer(reader.readLine()) tokenizer new StringTokenizer(reader.readLine()) return tokenizer.nextToken(); tokenizer.nextToken() tokenizer.nextToken tokenizer public int nextInt() {
            return Integer.parseInt(next());
        } nextInt {
            return Integer.parseInt(next());
        } return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next public long nextLong() {
            return Long.parseLong(next());
        } nextLong {
            return Long.parseLong(next());
        } return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next public class Main {

	static class Task {
        public void solve(InputReader in, PrintWriter out) {
        	int n = in.nextInt();
        	if(n==1) {
        		out.println(in.nextInt());
        		return;
        	}
        	int ans = 0;
        	int prev = in.nextInt();
        	for(int i=1; i<n; i++) {
        		int curr = in.nextInt();
        		if(prev > curr)
        			ans+=prev-curr;
        		if(i==n-1)
        			ans+=curr;
        		prev = curr;
        	}
        	out.println(ans);
        }
        
        public void solve_again(InputReader in, PrintWriter out) {
        	int n = in.nextInt();
        	int curr = in.nextInt();
        	int ans = curr;
        	for(int i=1; i<n; i++) {
        		int next = in.nextInt();
        		if(next > curr)
        			ans+=next-curr;
        		curr = next;
        	}
        	out.println(ans);
        }
        
    }
	
    public static void main(String argds[]) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        solver.solve(in, out);
        out.close();
    }

	static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;
        
        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }
        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
 
        public int nextInt() {
            return Integer.parseInt(next());
        }
 
        public long nextLong() {
            return Long.parseLong(next());
        }
    }	
} public class Main {

	static class Task {
        public void solve(InputReader in, PrintWriter out) {
        	int n = in.nextInt();
        	if(n==1) {
        		out.println(in.nextInt());
        		return;
        	}
        	int ans = 0;
        	int prev = in.nextInt();
        	for(int i=1; i<n; i++) {
        		int curr = in.nextInt();
        		if(prev > curr)
        			ans+=prev-curr;
        		if(i==n-1)
        			ans+=curr;
        		prev = curr;
        	}
        	out.println(ans);
        }
        
        public void solve_again(InputReader in, PrintWriter out) {
        	int n = in.nextInt();
        	int curr = in.nextInt();
        	int ans = curr;
        	for(int i=1; i<n; i++) {
        		int next = in.nextInt();
        		if(next > curr)
        			ans+=next-curr;
        		curr = next;
        	}
        	out.println(ans);
        }
        
    }
	
    public static void main(String argds[]) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        solver.solve(in, out);
        out.close();
    }

	static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;
        
        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }
        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
 
        public int nextInt() {
            return Integer.parseInt(next());
        }
 
        public long nextLong() {
            return Long.parseLong(next());
        }
    }	
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

	static class Task {
        public void solve(InputReader in, PrintWriter out) {
        	int n = in.nextInt();
        	if(n==1) {
        		out.println(in.nextInt());
        		return;
        	}
        	int ans = 0;
        	int prev = in.nextInt();
        	for(int i=1; i<n; i++) {
        		int curr = in.nextInt();
        		if(prev > curr)
        			ans+=prev-curr;
        		if(i==n-1)
        			ans+=curr;
        		prev = curr;
        	}
        	out.println(ans);
        }
        
        public void solve_again(InputReader in, PrintWriter out) {
        	int n = in.nextInt();
        	int curr = in.nextInt();
        	int ans = curr;
        	for(int i=1; i<n; i++) {
        		int next = in.nextInt();
        		if(next > curr)
        			ans+=next-curr;
        		curr = next;
        	}
        	out.println(ans);
        }
        
    }
	
    public static void main(String argds[]) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        solver.solve(in, out);
        out.close();
    }

	static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;
        
        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }
        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
 
        public int nextInt() {
            return Integer.parseInt(next());
        }
 
        public long nextLong() {
            return Long.parseLong(next());
        }
    }	
}