Under construction

Still being built. Things may change

Permutation Magic

Time limit2sMemory limit1024 MB

Summary
Apply a permutation of 1 to M to sequence A so it matches B in the most positions, and output the lexicographically smallest such result.
Level

Medium7 of 10

Topics
Graph, Greedy
Solved
No attempts yet

Problem

There are sequences A=(a1,…,aN)A = (a_1, \dots, a_N) and B=(b1,…,bN)B = (b_1, \dots, b_N) with the same length NN. aia_i is the ii-th element of AA, and its value is an integer between 11 and MM. The same holds for bjb_j, the jj-th element of BB.

You can apply a magic trick to AA exactly once. Prepare a permutation P=(p1,…,pM)P = (p_1, \dots, p_M) of the integers from 11 through MM, and change AA into A′A' by setting ai′=paia'_i = p_{a_i} (1≤i≤N1 \le i \le N).

You want A′A' to be closer to BB after the trick. The distance between two sequences is the Hamming distance, which is the number of positions where the corresponding values differ.

Among all possible A′A', find a sequence that satisfies both of the following conditions.

  • No other possible A′A' has a smaller distance to BB than this sequence.
  • Among the possible sequences with that same distance to BB, it is the lexicographically smallest.

A sequence X=(x1,…,xN)X = (x_1, \dots, x_N) is lexicographically smaller than Y=(y1,…,yN)Y = (y_1, \dots, y_N) if there is an index ii such that xj=yjx_j = y_j for all j<ij < i and xi<yix_i < y_i.

Input

The input is given in the following format.

NN MM

a1a_1 …\dots aNa_N

b1b_1 …\dots bNb_N

The first line contains two integers NN (1≤N≤100,0001 \le N \le 100{,}000) and MM (1≤M≤601 \le M \le 60). Each sequence has NN values between 11 and MM.

The second line contains NN integers, where the ii-th integer is aia_i (1≤ai≤M1 \le a_i \le M).

The third line contains NN integers, where the ii-th integer is bib_i (1≤bi≤M1 \le b_i \le M).

Output

Print NN integers separated by spaces. The ii-th integer is the ii-th element of a sequence that satisfies all the conditions above. Print each element as an integer.

Examples2

  1. Example 1

    Input
    4 3
    2 2 3 3
    2 2 2 2
    
    Expected output
    1 1 2 2
    
  2. Example 2

    Input
    5 3
    2 2 3 3 2
    2 2 2 2 3
    
    Expected output
    3 3 2 2 3