TopCoder problem "ColumnDiagramPerimeter" used in SRM 419 (Division II Level One)



Problem Statement

    

A column diagram is composed of n consecutive columns arranged in a horizontal row. Each column has a width of 1, and the base of each column is on the x-axis. The heights of the columns are given in the int[] a, where a[i] is the height of the i-th column. The picture below shows an example of a column diagram:

Return the perimeter of the given column diagram.

 

Definition

    
Class:ColumnDiagramPerimeter
Method:getPerimiter
Parameters:int[]
Returns:int
Method signature:int getPerimiter(int[] a)
(be sure your method is public)
    
 

Constraints

-a will contain between 1 and 50 elements, inclusive.
-Each element of a will be between 1 and 50, inclusive.
 

Examples

0)
    
{2,3,5,6,1}
Returns: 22
This column diagram is shown on the picture in the problem statement.
1)
    
{3,2,1}
Returns: 12
2)
    
{2,2}
Returns: 8
3)
    
{1,2,1}
Returns: 10
4)
    
{1,2,3}
Returns: 12

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=10078

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=13510&pm=10078

Writer:

andrewzta

Testers:

PabloGilberto , Olexiy , marek.cygan , ivan_metelsky

Problem categories:

Simple Search, Iteration