static void Main() { var ss = new StreamScanner(new StreamReader(Console.OpenStandardInput())); var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; new Program().Solve(ss, sw); sw.Flush(); }
void Solve(StreamScanner ss, StreamWriter sw) { //--------------------------------- var N = ss.Next(int.Parse); var A = ss.Next(long.Parse, N); if (A[0] != 0) { sw.WriteLine(-1); return; } var ans = 0L; for (var i = 1; i < N; i++) { if (A[i] > A[i - 1] + 1) { sw.WriteLine(-1); return; } if (A[i] == A[i - 1] + 1) { continue; } if (A[i] <= A[i - 1]) { ans += A[i - 1]; } } ans += A[N - 1]; sw.WriteLine(ans); //--------------------------------- }