public static void main(String[] args)
	{
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		int[] cardCounts = new int[N];
		for (int i = 0; i < N; i++)
			cardCounts[i] = in.nextInt();
		
		long straights = 0L;
		LinkedList<ArrayList<Integer>> bounds = new LinkedList<ArrayList<Integer>>();
		bounds.add(new ArrayList<Integer>());
		bounds.get(0).add(0);
		bounds.get(0).add(N);
		while (bounds.size() > 0)
		{
			ArrayList<Integer> currentBounds = bounds.removeFirst();
			int left = currentBounds.get(0);
			int right = currentBounds.get(1);
			int min = cardCounts[left];
			for (int i = left; i < right; i++)
				if (cardCounts[i] < min)
					min = cardCounts[i];
			straights += min;
			for (int i = left; i < right; i++)
				cardCounts[i] -= min;
			int a = left;
			int b = left;
			while (b < right)
			{
				if (cardCounts[b] == 0)
				{
					if (a < b)
					{
						ArrayList<Integer> newBounds = new ArrayList<Integer>();
						newBounds.add(a);
						newBounds.add(b);
						bounds.addLast(newBounds);
					}
					a = b+1;
				}
				b++;
			}
			if (a < b)
			{
				ArrayList<Integer> newBounds = new ArrayList<Integer>();
				newBounds.add(a);
				newBounds.add(b);
				bounds.addLast(newBounds);
			}
		}
		System.out.println(straights);
	}