(Problem C ) Error Sequence

0

0 votes
Algorithms, Medium, Suffix Automata, String
Problem

Consider a strictly-increasing sequence of integers a1,a2,,ak. Alice picked a continuous sequence p1,p2,,pn from it.

Bob then removed exactly nm numbers from this sequence, while maintaining the relative order of the remaining elements, thus obtaining a sequence q1,q2,,qm. He then subtracted q1 from all the elements to form the final sequence b1,b2,,bm.

For example, let {ai}={2,3,5,7,11,13,17,19,23,29,31},p1=11,n=6,m=4. This yields the sequence of primes p={11,13,17,19,23,29}. Assume that Bob has removed the numbers 17 and 23 to obtain q={11,17,19,29}. After that, he subtracted 11 from every element to get b={0,6,8,18}.

Alternatively, he could have removed 11 and 29 to obtain the sequence q={13,17,19,23} and b={0,4,6,10}. Note that the same sequence may also be generated by, for instance, picking p={13,17,19,23,29,31} and removing 29 and 31.

Eve found the number n and the sequences {ai}ki=1{bi}mi=1. She now wonders, what is the smallest possible p1, that could yield the sequence {bi}mi=1 through the process described above?

Input format:

  • First line: t (number of test cases)
  • For each test case
    • First line: Three space-separated integers knand m satisfying 1mnk
    • Second line: k space-separated integers {ai}ki=1 satisfying 0ai109 and ai<ai+1
    • Third line: m space-separated integers {bi}mi=1. It is guaranteed that b1=0 .

It is guaranteed that there always exists a solution.

Output:

For each test case, output a single integer p1 - the smallest element in the sequence that Alice chose.

Scoring:

In all test sets, 1t10.

In the first test set, worth 12 points, 1m=n102 and k104.

In the second test set, worth 21 points, 1m<n102 and k104.

In the third test set, worth 24 points, 1m=n105 and k2105.

In the fourth test set, worth 25 points, 1mn105nm50 and k2105.

In the fifth test set, worth 18 points,t=31mn5105nm100 and k106.

Sample Input
1
11 6 4
2 3 5 7 11 13 17 19 23 29 31
0 6 8 18
Sample Output
11
Time Limit: 5
Memory Limit: 256
Source Limit:
Explanation

This is the example from the statement.

Editor Image

?