#P9815. Simulating Directional Movement on a Number Line

    ID: 22961 Type: Default 1000ms 256MiB

Simulating Directional Movement on a Number Line

Simulating Directional Movement on a Number Line

We are given (n+2) points arranged in a line and numbered from (0) to (n+1). For each point (i) (where (0 \le i \le n+1)), there are two integers (a_i) and (b_i). It is specified that (a_0 = b_0 = a_{n+1} = b_{n+1} = 0).

Initially, you are at point (x=0) with a moving direction (y=1) (meaning you are moving to the right). The process continues until after some move the state ((x, y)) becomes either ((0, -1)) or ((n+1, 1)). The movement rules are as follows:

  • If \(y = 1\): Increase \(x\) by \(1\). Then, if \(a_x > 0\), set \(y = -1\) (reverse direction); otherwise, keep \(y\) unchanged. Finally, decrease \(a_x\) by \(1\).
  • If \(y = -1\): Decrease \(x\) by \(1\). Then, if \(b_x > 0\), set \(y = 1\); otherwise, keep \(y\) unchanged. Finally, decrease \(b_x\) by \(1\).
The task is to simulate the movements and determine the final position \(x\). Note that the final \(x\) will always be either \(0\) or \(n+1\).

inputFormat

The input consists of three lines:

  1. The first line contains an integer (n) ((n \ge 1)).
  2. The second line contains (n) space-separated integers (a_1, a_2, \dots, a_{n}).
  3. The third line contains (n) space-separated integers (b_1, b_2, \dots, b_{n}).

    Note: For the endpoints, (a_0, a_{n+1}, b_0, b_{n+1}) are assumed to be (0).

outputFormat

Output a single integer representing the final position (x) after the simulation ends. This final position will be either (0) or (n+1).

sample

1
0
0
2