output_description: If we can make X equal to A by repeating the operation, print the minimum
  number of operations required. If we cannot, print -1.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N
      A_1
      :
      A_N
attempt: N = int(input())
  
  start = int(input())
  flag = 0
  if start != 0:
  flag = 1
  t = 0
  
  for i in range(N-1):
   k = int(input())
   if k == start:
    t += start
   elif k == start + 1:
    t += 1
    start = k
   elif k == 0:
    start = 0
   elif k > start + 1:
    t = -1
    break
   else:
    t += k
    start = k
  
  if flag ==1:
   t = -1
  print(t)
problem_description: Statement
  
  There is a sequence X of length N, where every element is initially 0. Let X_i
  denote the i-th element of X.
  
  You are given a sequence A of length N. The i-th element of A is A_i.
  Determine if we can make X equal to A by repeating the operation below. If we
  can, find the minimum number of operations required.
  
    * Choose an integer i such that 1\leq i\leq N-1. Replace the value of X_{i+1} with the value of X_i plus 1.
samples: [{"input": "4\n    0\n    1\n    1\n    2", "output": "3\n    \n\nWe can make X equal to A as follows:\n\n  * Choose i=2. X becomes (0,0,1,0).\n  * Choose i=1. X becomes (0,1,1,0).\n  * Choose i=3. X becomes (0,1,1,2).\n\n* * *"}, {"input": "3\n    1\n    2\n    1", "output": "-1\n    \n\n* * *"}, {"input": "9\n    0\n    1\n    1\n    0\n    1\n    2\n    2\n    1\n    2", "output": "8"}]