AtCoder Beginner Contest 116 C - Grand Garden【Python】

https://atcoder.jp/contests/abc116/tasks/abc116_c

AtCoder ProblemsのRecommendationで Difficulty: 542、Solve Probability: 37%でした。

単調増加ではなくなったところと最後のところで答えを計算していきました。
しっかりと解けたわけではなくサンプルケースに合わせてコードを調整していったらACとなりました。

N = int(input())
H = list(map(int, input().split()))

ans = 0
pre_h = 0
for i, h in enumerate(H):
    if h < pre_h:
        ans += pre_h - h
    pre_h = h
    if i == len(H) - 1:
        ans += h
print(ans)