Module Module1

    Sub Main()

        Dim N As Integer = Console.ReadLine()
        Dim A(N) As Integer
        Dim B As String() = Split(Console.ReadLine())
        Dim i As Integer = 0
        Dim s As Long

        A(0) = B(0)
        For i = 1 To N - 1
            A(i) = B(i - 1)
        Next

        For i = N - 2 To 1 Step -1
            If A(i) < A(i - 1) Then
                A(i - 1) = A(i)
            End If
        Next

        For i = 0 To N - 1
            s += A(i)
        Next

        Console.WriteLine(s)
        'Console.ReadLine()

    End Sub

End Module
