N = int(input()) B = list(map(lambda x: int(x) - 1, input().split())) tmp = 0 res = 0 for i in range(N): if B[i] == 0: res += abs(tmp) elif B[i] == -1: tmp -= 1 res += abs(tmp) else: tmp += B[i] res += abs(tmp) print(res)