Under construction

Still being built. Things may change

Reversing

Interview

Time limit2sMemory limit512 MB

Summary
Given prefix or suffix reversals applied to an array, track where the element originally at position K ends up after all operations.
Level

Medium6 of 10

Topics
Array, Implementation, Simulation, Math
Solved
No attempts yet

Problem

An array AA holds NN numbers, and its indices start at 1. You apply MM operations to the array. Write a program that finds the position of the number that started at index KK once every operation is done.

One operation is a single nonzero integer ii. When ii is positive, the order of the first ii numbers of AA is reversed. When ii is negative, the order of the last −i-i numbers is reversed.

For example, take N=5N = 5 and A=[1,3,2,4,5]A = [1, 3, 2, 4, 5]. Applying operation 33 turns AA into [2,3,1,4,5][2, 3, 1, 4, 5]. Applying operation −4-4 to that reverses the last four numbers and gives [2,5,4,1,3][2, 5, 4, 1, 3]. The number that started at position 1 moves to position 4, and the number that started at position 3 moves to position 1.

Input

The first line contains the size of the array NN (1≤N≤100,0001 \le N \le 100{,}000), the position KK (1≤K≤N1 \le K \le N), and the number of operations MM (1≤M≤100,0001 \le M \le 100{,}000).

The second line contains the numbers stored in AA, from the first number to the last. Each of those numbers is a natural number no greater than 100,000. The same number may appear in the array several times.

Each of the next MM lines contains one integer ii describing an operation. ii is a nonzero integer whose absolute value is at most NN.

Output

On the first line, print the position that the KKth number has moved to after all MM operations are done.

Examples5

  1. Example 1

    Input
    5 1 2
    1 3 2 4 5
    3
    -4
    
    Expected output
    4
    
  2. Example 2

    Input
    5 3 2
    5 5 5 5 5
    3
    -4
    
    Expected output
    1
    
  3. Example 3

    Input
    4 4 8
    10 20 15 25
    1
    -2
    3
    -4
    -1
    2
    -3
    4
    
    Expected output
    3
    
  4. Example 4

    Input
    10 1 6
    9 8 1 2 8 7 3 4 6 6
    9
    -9
    8
    -8
    7
    -7
    
    Expected output
    1
    
  5. Example 5

    Input
    5 2 5
    7 6 5 4 3
    3
    -3
    3
    -3
    3
    
    Expected output
    2