#region Header #!/usr/bin/env python3 # from typing import * import sys import io import math import collections import decimal import itertools from queue import PriorityQueue import bisect import heapq def input(): return sys.stdin.readline()[:-1] sys.setrecursionlimit(1000000) #endregion # _INPUT = """6 # 1 2 0 8 6 9 # """ # sys.stdin = io.StringIO(_INPUT) def main(): N = int(input()) + 1 Y = [0] + list(map(int, input().split())) T = [Y[0]] for i in range(1, N): T.append(max(T[-1], Y[i])) dp = [[0 for _ in range(T[-1]+1)] for _ in range(N)] for i in range(1, N): for k in range(T[i]+1): if Y[i] <= k: dp[i][k] = min(dp[i-1][v] + (v - Y[i]) for v in range(Y[i], k+1)) else: dp[i][k] = dp[i-1][k] + (Y[i] - k) for k in range(T[i]+1, T[-1]+1): dp[i][k] = dp[i][T[i]] v = min(dp[N-1]) print(v) if __name__ == '__main__': main()