Input:
Table T of length N (N <= 10000) containing 32-bit integers. Each integer denotes length and direction of consecutive edge of polygon on a square grid (every vertex has integer coordinates). If integer is positive, it means edge goes up or right. If integer is negative, edge goes down or left. Edges are perpendicular to its neighbors. Edges do not intersect nor overlap with each other. Vertices do not overlap with each other.
Examplary input T:
2, -3, -1, 1, -1, 2
Represents two polygons (it doesn't matter which one we will choose):
Arrows denote first edge corresponding to the first 2 in T.
Output:
Integer denoting surface area of given polygon. For exemplary input, the output should be 5.
Desired time complexity: O(N)
Desired memory complexity (not counting input): O(1)
Can you present such algorithm and rationale behind it?