The 14th Huazhong University of Science and Technology Programming Contest Neat Tree (Monotone Stack)

Link: https://www.nowcoder.com/acm/contest/106/I
Source: Niuke.com

It’s universally acknowledged that there’re innumerable trees in the campus of HUST.

There is a row of trees along the East-9 Road which consists of N trees. Now that we know the height of each tree, the gardeners of HUST want to know the super-neatness of this row of trees. The neatness of a sequence of trees is defined as the difference between the maximum height and minimum height in this sequence. The super-neatness of this sequence of trees is defined as the sum of neatness of all continous subsequences of the trees.

Multiple cases please process until the end of input. There are at most 100 test cases.

Enter description:

For each case:
The first line contains an integer N represents the number of
trees.
The next line n positive integers followed, in which h i represent the
height of the tree i. 

Output description:

For each test case, output a integer in a single line which represents the super-neatness of the row
of trees.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325902918&siteId=291194637
Recommended