PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000030. Đa tập – Chèn và xóa theo thứ hạng (Multiset)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

2 lộ trình

Đa tập – Chèn và xóa theo thứ hạng (Multiset)

Multiset

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a multiset SS of integers. Unlike a set, duplicate values are allowed and each occurrence has its own position in sorted order. Initially, SS contains nn positive integers provided in nondecreasing order.

Process qq operations, represented by integers kik_i:

  • If ki>0k_i>0, insert one occurrence of value kik_i.
  • If ki<0k_i<0, sort the current multiset in nondecreasing order and remove one occurrence of the element at rank ∣ki∣|k_i|, starting from rank 11. Every removal is guaranteed to be valid.

After all operations, print 0 if the multiset is empty. Otherwise, print any value still contained in the multiset; the minimum or maximum is not required. A value no longer present is invalid.

Input

  • Line 1: integers n,qn,q, initial multiset size and operation count.
  • Line 2: exactly nn integers a1,a2,…,ana_1,a_2,\ldots,a_n in nondecreasing order.
  • Line 3: exactly qq nonzero integers k1,k2,…,kqk_1,k_2,\ldots,k_q, representing the operations in order. These are operations on the same multiset, not separate test cases.

Output

Print exactly one integer: 0 if the final multiset is empty, or any value that occurs in the final multiset otherwise. Multiple different outputs may be accepted.

Subtasks

Subtask 1 (20 points):

  • 1≤n≤201\le n\le 20.
  • 1≤q≤201\le q\le 20.
  • 1≤a1≤a2≤⋯≤an≤n1\le a_1\le a_2\le\cdots\le a_n\le n.
  • For each ki>0k_i>0: 1≤ki≤n1\le k_i\le n.
  • For each ki<0k_i<0: 1≤∣ki∣≤∣S∣1\le |k_i|\le |S| at the time of removal.
  • Every kik_i is nonzero; duplicate values retain their multiplicities.

Subtask 2 (30 points):

  • 1≤n≤30001\le n\le 3000.
  • 1≤q≤30001\le q\le 3000.
  • 1≤a1≤a2≤⋯≤an≤n1\le a_1\le a_2\le\cdots\le a_n\le n.
  • For each ki>0k_i>0: 1≤ki≤n1\le k_i\le n.
  • For each ki<0k_i<0: 1≤∣ki∣≤∣S∣1\le |k_i|\le |S| at the time of removal.
  • Every kik_i is nonzero; duplicate values retain their multiplicities.

Subtask 3 (50 points):

  • 1≤n≤10000001\le n\le 1000000.
  • 1≤q≤10000001\le q\le 1000000.
  • 1≤a1≤a2≤⋯≤an≤n1\le a_1\le a_2\le\cdots\le a_n\le n.
  • For each ki>0k_i>0: 1≤ki≤n1\le k_i\le n.
  • For each ki<0k_i<0: 1≤∣ki∣≤∣S∣1\le |k_i|\le |S| at the time of removal.
  • Every kik_i is nonzero; duplicate values retain their multiplicities.

Examples

Input

5 4
1 2 3 4 5
-5 -1 -3 -1

Output

3

Explanation

The initial sorted multiset is [1,2,3,4,5][1,2,3,4,5].

  1. -5 removes the fifth element, 55, leaving [1,2,3,4][1,2,3,4].
  2. -1 removes the first element, 11, leaving [2,3,4][2,3,4].
  3. -3 removes the third element, 44, leaving [2,3][2,3].
  4. -1 removes the first element, 22, leaving [3][3].

Only 33 remains, so this sample's output is 3. In other cases with several remaining values, any of them would be accepted.