#K45842. Silhouette Perimeter
Silhouette Perimeter
Silhouette Perimeter
You are given a list of non-negative integers representing the heights of a sequence of buildings aligned side by side. The task is to compute the total perimeter of the silhouette formed when viewing the buildings from a distance.
The perimeter is calculated by summing:
- The left side of the first building and the right side of the last building,
- The top side of each building (which has length equal to the building's height),
- The absolute differences between the heights of adjacent buildings (which represent the partially exposed vertical sides).
In mathematical form, if the building heights are given by \(h_1, h_2, \dots, h_n\), then for \(n \ge 1\), the perimeter \(P\) is computed as:
[ P = h_1 + h_n + \sum_{i=1}^{n} h_i + \sum_{i=2}^{n} |h_i - h_{i-1}|. ]
If there are no buildings (i.e. the list is empty), the perimeter is defined to be 0.
inputFormat
The input is provided via standard input (stdin) and consists of two lines.
- The first line contains a single integer \(n\) \( (n \ge 0)\) indicating the number of buildings.
- If \(n > 0\), the second line contains \(n\) space-separated non-negative integers representing the heights of the buildings.
outputFormat
Output a single integer to standard output (stdout) representing the total perimeter of the silhouette.
## sample0
0