AT_scpc2026_div2_c Rearrangement

Description

#### 表示言語 / / You are given an integer array $ A=[A_1,\dots,A_N] $ of length $ N $ and an integer array $ B=[B_1,\dots,B_M] $ of length $ M $ . Find a rearrangement of $ A $ in which the number of occurrences of $ B $ as a contiguous subsequence is maximized. Here, a **rearrangement** of $ A $ means an array $ A' $ satisfying the following condition. - $ A' $ is an integer array of length $ N $ , and there exists a permutation $ P $ of $ 1,\dots,N $ such that $ A'_i=A_{P_i}(1 \le i \le N) $ . What is a contiguous subsequence? A **contiguous subsequence** of a sequence is a sequence obtained by taking elements from consecutive positions of the original sequence, in the same order. For example, in the sequence $ [1,2,3,4] $ , $ [2,3] $ is a contiguous subsequence, but $ [1,3] $ is not. Multiple contiguous subsequences may overlap with each other in the same sequence. For example, in the sequence $ [1,2,1,2,1] $ , $ [1,2,1] $ appears a total of $ 2 $ times. What is a permutation? A **permutation** of length $ N $ is a sequence that contains each integer from $ 1 $ to $ N $ exactly once. For example, $ [2,1,4,3] $ is a permutation, but $ [4,2,1,1] $ and $ [1,2,3,5] $ are not.

Input Format

The input is given from Standard Input in the following format: > $ N $ $ M $ $ A_1 $ $ A_2 $ $ \dots $ $ A_N $ $ B_1 $ $ B_2 $ $ \dots $ $ B_M $

Output Format

Output the elements of a rearrangement of $ A $ that maximizes the number of occurrences of $ B $ as a contiguous subsequence, separated by spaces. If there are multiple such rearrangements, output any one of them.

Explanation/Hint

### Constraints - $ 1 \le M \le N \le 1\,000\,000 $ - $ 0 \le A_i, B_i \le 1\,000\,000 $ - All given numbers are integers.