Hide

Problem G
Lamppost Race

A group of friends has created a simple racing game to pass the time while waiting for the solar eclipse. The game is played on a long, straight street which has lampposts occurring at various intervals up and down the street. The runners begin the race at one end of the street and then must touch certain lampposts in a predetermined order, running back and forth between them. Whoever is the first to touch the last lamppost wins the race.

The group wants you to figure out how far they are actually running in the race. You will be given the position of each lamppost in the order they must be touched. Lamppost positions will be given as a distance in meters from the starting point of the race.

For example, suppose the first lamppost the runners must touch is $5$ meters from the start, the second one is $14$ meters from the start, the third is $8$ meters from the start, and the fourth is $20$ meters. The runners must run $5$ meters to reach the first lamppost, then run $9$ (from $5$ to $14$) meters to reach the second lamppost, then $6$ meters (from $14$ to $8$) to reach the third lamppost, and finally $12$ meters (from $8$ to $20$) to reach the final lamppost. Thus, the total distance traveled is $5 + 9 + 6 + 12 = 32$ meters.

Input

The input consists of information for a single race. The first line consists of an integer $N$ ($1 \le N \le 1\, 000$) indicating the number of lampposts the runners must touch. The second line contains $N$ space-separated integers $d_1$, $d_2$, ..., $d_ N$, with each $d_ i$ in the range $1 \le d_ i \le 1\, 000$. Each $d_ i$ specifies how far the $i$th lamppost is from the start of the race.

Output

You should output a single integer, the total distance the racers must cover.

Sample Input 1 Sample Output 1
4
5 14 8 20
32

Please log in to submit a solution to this problem

Log in