A rectangular container yard is laid out as an n x n grid of stacking slots. Slot (i, j) currently holds a stack of h[i][j] unit-sized shipping containers (a value of 0 means the slot is empty). A drone photographs the yard from three vantage points: straight overhead, from the front along one side of the yard, and from the side along the perpendicular direction. Each photograph shows a silhouette: overhead, a slot contributes to the silhouette only if it holds at least one container; from the front, an entire row's silhouette height equals the tallest stack anywhere in that row; from the side, an entire column's silhouette height equals the tallest stack anywhere in that column. Compute the combined area: the overhead area, plus the sum of front-silhouette heights over all rows, plus the sum of side-silhouette heights over all columns.
n.n lines contains n space-separated integers, the j-th integer on the i-th of these lines being h[i][j].Print a single integer: the total combined silhouette area.
Example 1
Input
2 1 2 3 4
Expected
17
Explanation
All four slots hold at least one container, so the overhead area is 4. Row 0's tallest stack is 2 and row 1's is 4, so the front area is 2 + 4 = 6. Column 0's tallest stack is 3 and column 1's is 4, so the side area is 3 + 4 = 7. Total = 4 + 6 + 7 = 17.
Example 2
Input
1 2
Expected
5
Explanation
The single slot holds 2 containers, so the overhead area is 1, the front area is 2, and the side area is 2. Total = 1 + 2 + 2 = 5.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →