#C5789. Shortest Color Subsequence

    ID: 49476 Type: Default 1000ms 256MiB

Shortest Color Subsequence

Shortest Color Subsequence

You are given a sequence of colors represented by integers and a set of target colors. Your task is to determine the length of the shortest contiguous subsequence of the given sequence that contains all the target colors at least once.

Formally, given a sequence \( S = [s_1, s_2, \dots, s_n] \) and a set of target colors \( T \) with \(|T|=m\), find the minimum length \( L = j - i + 1 \) such that the subsequence \( [s_i, s_{i+1}, \dots, s_j] \) contains every color in \( T \) at least once. If no such subsequence exists, output -1.

This problem can be solved using the sliding window technique.

inputFormat

The first line contains two integers \( n \) and \( m \), representing the number of elements in the color sequence and the number of target colors respectively.

The second line contains \( n \) space-separated integers representing the color sequence.

The third line contains \( m \) space-separated integers representing the target color set.

outputFormat

Output one integer on a single line: the length of the shortest contiguous subsequence that contains all target colors at least once, or -1 if no such subsequence exists.

## sample
10 3
1 2 3 4 2 3 1 2 2 3
1 2 3
3