#C11672. Magical Subsequence

    ID: 41014 Type: Default 1000ms 256MiB

Magical Subsequence

Magical Subsequence

Given a sequence of stone heights and a magical sequence, determine whether the reversed magical sequence exists as a subsequence in the stone heights.

More formally, given two sequences \( S = [s_1, s_2, \dots, s_N] \) and \( M = [m_1, m_2, \dots, m_K] \), you need to determine if there exist indices \( 1 \le i_1 < i_2 < \dots < i_K \le N \) such that: \[ s_{i_1} = m_K, \quad s_{i_2} = m_{K-1}, \quad \dots, \quad s_{i_K} = m_1 \] If such indices exist, output YES; otherwise, output NO.

inputFormat

The input is given via standard input (stdin) in the following format:

  • The first line contains an integer N, representing the number of stones.
  • The second line contains N space-separated integers, denoting the heights of the stones.
  • The third line contains an integer M, representing the length of the magical sequence.
  • The fourth line contains M space-separated integers, denoting the magical sequence.

outputFormat

The output should be printed to standard output (stdout). Print a single line containing YES if the reversed magical sequence exists as a subsequence of the stone heights, otherwise print NO.## sample

5
1 2 3 4 5
3
3 2 1
YES