#P8087. Find the Shortest Valid Interval

    ID: 21270 Type: Default 1000ms 256MiB

Find the Shortest Valid Interval

Find the Shortest Valid Interval

You are given a permutation \(a\) of \(\{1,2,\dots,n\}\) and a sequence \(f\) of length \(n\). For any interval (subarray) \([l,r]\), define its MEX as the smallest positive integer that does not occur in \(\{a_l,a_{l+1},\dots,a_r\}\). For example, \(\operatorname{Mex}\{2,3\}=1\) and \(\operatorname{Mex}\{1,2,3\}=4\).

An interval \([l,r]\) is called valid if and only if \[ f_{r-l+1} < \operatorname{Mex}_{l,r}, \] where \(f_{r-l+1}\) is the \((r-l+1)\)th element of \(f\). Your task is to find the minimum length of a valid interval. If there is no valid interval, output 0.

Note: The input size can be very large, so use fast input methods.

inputFormat

The first line contains an integer (n) (the length of sequence).\nThe second line contains (n) space‐separated integers (a_1,a_2,\dots,a_n), which form a permutation of ({1,2,\dots,n}).\nThe third line contains (n) space‐separated integers (f_1,f_2,\dots,f_n).

outputFormat

Output a single integer: the minimum length of a valid interval. If no valid interval exists, output 0.

sample

3
1 2 3
0 1 2
1