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

#BS0000347. Thời điểm sớm nhất đánh dấu mọi chỉ số I (Earliest Second to Mark Indices I)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Thời điểm sớm nhất đánh dấu mọi chỉ số I (Earliest Second to Mark Indices I)

Earliest Second to Mark Indices I

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Two integer arrays a1,a2,…,ana_1,a_2,\ldots,a_n and b1,b2,…,bmb_1,b_2,\ldots,b_m are given. No index of aa is marked initially. There are mm seconds, numbered from 11 through mm.

At second ss, choose exactly one of these actions:

  • Select an index ii (1≤i≤n1\le i\le n) and subtract 11 from aia_i.
  • If abs=0a_{b_s}=0, mark index bsb_s.
  • Leave the state unchanged.

Find the earliest second by which every index is marked. If this cannot be achieved within mm seconds, the answer is -1.

Input

The first line contains two integers n,mn,m, the array lengths.

The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n, the initial values.

The third line contains mm integers b1,b2,…,bmb_1,b_2,\ldots,b_m, where bsb_s is the index associated with second ss.

Output

Print one integer: the earliest second by which every index can be marked, or -1 if impossible.

Subtasks

  • Subtask 1 (20 points): 1≤n≤31\le n\le 3; 1≤m≤91\le m\le 9; 0≤ai≤40\le a_i\le 4 for all 1≤i≤n1\le i\le n; 1≤bs≤n1\le b_s\le n for all 1≤s≤m1\le s\le m.
  • Subtask 2 (30 points): 1≤n≤2001\le n\le 200; 1≤m≤2001\le m\le 200; 0≤ai≤1090\le a_i\le 10^9 for all 1≤i≤n1\le i\le n; 1≤bs≤n1\le b_s\le n for all 1≤s≤m1\le s\le m.
  • Subtask 3 (50 points): 1≤n≤20001\le n\le 2000; 1≤m≤20001\le m\le 2000; 0≤ai≤1090\le a_i\le 10^9 for all 1≤i≤n1\le i\le n; 1≤bs≤n1\le b_s\le n for all 1≤s≤m1\le s\le m.

Examples

Input 1

3 8
2 2 0
2 2 2 2 3 2 2 1

Output 1

8

Explanation

During the first four seconds, decrement each of the first two entries twice to obtain (0,0,0)(0,0,0). Mark index 3 at second 5, index 2 at second 6, wait at second 7 and mark index 1 at second 8. Index 1 is unavailable earlier, proving optimality.

Input 2

2 3
0 1
2 2 2

Output 2

-1

Explanation

Index 1 is zero initially but never occurs in bb, so no marking opportunity exists for it.