#K34212. Final Position on a 1-Dimensional Grid
Final Position on a 1-Dimensional Grid
Final Position on a 1-Dimensional Grid
You are given a person standing on a 1-dimensional grid at position 0. The person takes a series of steps, where each step is either 1
(a step to the right) or -1
(a step to the left).
Your task is to compute the final position of the person after all the steps have been taken.
Note: The input is provided via standard input (stdin) and the result should be output via standard output (stdout).
The final position can be mathematically represented by the formula:
\(P = \sum_{i=1}^{n} s_i\)
where \(s_i\) is the value of the i-th step, and \(n\) is the number of steps.
inputFormat
The input consists of two lines. The first line contains an integer \(n\) representing the number of steps. The second line contains \(n\) space-separated integers, each of which is either 1
or -1
, indicating the direction of each step.
If \(n = 0\), the second line may be empty and the output should be \(0\).
outputFormat
Output a single integer representing the final position after taking all the steps.
## sample5
1 1 1 1 1
5