JDCC '15 Contest 5 P2 - Elevator

View as PDF

Points: 5
Time limit: 2.0s
Memory limit: 64M

Author:
Problem type

Reyno's little brother loves riding elevators. Whenever he enters an elevator, he mashes the buttons in some order and then waits in glee as the elevator takes him up and down the building. More specifically, the elevator visits the floors in the order the buttons were pressed. However, if the elevator would pass a floor that it's supposed to stop on, it stops there first, then continues on its destination.

For example, if the elevator starts on the 5th floor, and the buttons were pressed in the order:

8, 6, 4, 7

Then the elevator would first rise to floor , stopping at floor and along the way. Reaching floor , elevator would then descend to floor because floor was already visited. In total, the elevator travels for floors.

Reyno and his brother just entered the elevator on the floor of a story building. Immediately, his brother pressed a bunch of buttons. Reyno would like to know how many floors the elevator will travel.

Input Specification

The first line of input provides the number of test cases, . test cases follow. Each test case begins with one line containing an integer . The next line contains integers between and , the floors that were pressed in the order that they were pressed.

Output Specification

For each test case, your program should output an integer: the total number of floors the elevator travels.

Sample Input

4
4
80 60 40 70
4
70 40 80 42
4
30 70 1 100
63
49 51 48 52 47 53 46 54 45 55 44 56 43 57 42 58 41 59 40 60 39 61 38 62 37 63 36 64 35 65 34 66 33 67 32 68 31 69 30 70 29 71 28 72 27 73 26 74 25 75 24 76 23 77 22 78 21 79 20 80 19 81 18

Sample Output

70
90
228
2016

Explanation for Sample

In the first case, the elevator goes to floor , stopping at and along the way. It then descends to , travelling for floors in total.

In the second case, the elevator first goes to floor , then goes to floor , stopping at floor along the way. It then goes back up to floor , for a total of floors.

In the last case, the elevator alternates going up and down, travelling a total of floors.