#P1732. Minimum Absolute Difference Sum

    ID: 15017 Type: Default 1000ms 256MiB

Minimum Absolute Difference Sum

Minimum Absolute Difference Sum

Given a sequence \( A = {a_1, a_2, \ldots, a_n} \), define a new sequence \( B = {b_1, b_2, \ldots, b_n} \) as follows:

\[ b_i = \begin{cases} a_1, & \text{if } i = 1, \\ \min_{1 \le j 1. \end{cases} \]

The task is to compute and output the sum:

\[ \sum_{i=1}^{n} b_i \]

inputFormat

The first line contains an integer \( n \) that denotes the number of elements in the sequence \( A \).

The second line contains \( n \) space-separated integers: \( a_1, a_2, \ldots, a_n \).

outputFormat

Output a single integer representing \( \sum_{i=1}^{n} b_i \), where \( b_i \) is defined as above.

sample

5
1 5 3 2 4
9