def input_data():
    _ = input()
    m = [int(d) for d in input().split()]
    return m



def do_task(m):
    res = 0
    while len(m)>0:
        elem = m[len(m)-1]
        if elem == 1:
            res+=len(m)
            return res
        m.pop(len(m)-1)
        res += elem
        for l in m:
            if l>1:
                break
            else:
                m.pop(0)
        for i in range(len(m)):
            if m[i] == elem:
                break
            m[i]-=1

    return res


def output_data(m):
    print(m)


def my_code():
    m = input_data()
    m = do_task(m)
    output_data(m)
    pass


if __name__ == '__main__':
    my_code()
