#include <bits/stdc++.h>
using namespace std;

const int MAXN = 2e5 + 10;
int n, ans, ar[MAXN];

int main() {
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> ar[i];
	if (ar[0])
		return cout << -1, 0;
	for (int i = 0; i < n - 1; i++) {
		if (ar[i + 1] > ar[i]) {
			ans += ar[i + 1] - ar[i];
		}
		else {
			ans += ar[i + 1];
		}
	}
	cout << ans;
}