import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;

void main() {
	auto N = readln().split()[0].to!int();
	int[] h = readln().split().map!(x => x.to!int())().array;
	writeln(solve(h));
}

int solve(int[] h) {
	if (h.length == 0) return 0;
	if (h.length == 1) return h[0];
	
	auto lowest_i = h.length - h.minPos().length, lowest = h[lowest_i];
	h = h.map!(x => x-lowest).array;
	return lowest + solve(h[0..lowest_i]) + solve(h[lowest_i+1 .. $]);
	
}