output_description: Print the minimum total height of the stools needed to meet the goal.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N
      A_1 \ldots A_N
attempt: #
  # abc176 c
  #
  import sys
  from io import StringIO
  import unittest
  
  
  class TestClass(unittest.TestCase):
      def assertIO(self, input, output):
          stdout, stdin = sys.stdout, sys.stdin
          sys.stdout, sys.stdin = StringIO(), StringIO(input)
          resolve()
          sys.stdout.seek(0)
          out = sys.stdout.read()[:-1]
          sys.stdout, sys.stdin = stdout, stdin
          self.assertEqual(out, output)
  
      def test_入力例_1(self):
          input = """5
  2 1 5 4 3"""
          output = """4"""
          self.assertIO(input, output)
  
      def test_入力例_2(self):
          input = """5
  3 3 3 3 3"""
          output = """0"""
          self.assertIO(input, output)
  
  
  def resolve():
      N = int(input())
      A = list(map(int, input().split()))
  
      M = max(A)
      Mi = A.index(M)
      ans = M - A[N - 1]
      for i in range(N - 2, -1, -1):
          if i >= Mi:
              ans += M - A[i]
          else:
              if A[i] > A[i + 1]:
                  ans += A[i] - A[i + 1]
      print(ans)
  
  
  if __name__ == "__main__":
      # unittest.main()
      resolve()
  
problem_description: Statement
  
  N persons are standing in a row. The height of the i-th person from the front
  is A_i.
  
  We want to have each person stand on a stool of some heights - at least zero -
  so that the following condition is satisfied for every person:
  
  Condition: Nobody in front of the person is taller than the person. Here, the
  height of a person includes the stool.
  
  Find the minimum total height of the stools needed to meet this goal.
samples: [{"input": "5\n    2 1 5 4 3", "output": "4\n    \n\nIf the persons stand on stools of heights 0, 1, 0, 1, and 2, respectively,\ntheir heights will be 2, 2, 5, 5, and 5, satisfying the condition.\n\nWe cannot meet the goal with a smaller total height of the stools.\n\n* * *"}, {"input": "5\n    3 3 3 3 3", "output": "0\n    \n\nGiving a stool of height 0 to everyone will work."}]