Minperm

Time limit: 0.1s
Memory limit: 128MB
Input:
Output:

The beautiful city of Julcvari decided to host a city tour(online, of course). They have nn different attractions, the ii th one having a beauty equal to ii. An initial order for the presentation of the attractions has been selected, but the people of Julcvari started worrying that it might not be the best one.

They believe that the best order for visiting the attractions is the one for which the number of pairs of attractions (i,j)(i, j), where ii was visited before jj but ii is more beautiful than jj is minimised. But organizing events is not so easy, a lot of paperwork needs to be done in order to change something like this.

So, the host managed to get kk swap plans: let l1l_1, l2l_2, \dots, lkl_k be an array of integers representing the swap plans. The host can perform any number of swaps on the initial order, such that the swapped positions are at a distance equal to one of the elements from array ll. There is not much time left, so the people of Julcvari ask you for help in finding the best order in which the attractions are presented.

In other words, a permutation of size nn and an array of size kk with distinct elements are given. You can only perform swaps between positions ii and jj such that ji|j - i| is equal to an element of ll. What is the permutation with the minimum number of inversions that can be obtained?

Input data

The first line of the input contains nn and kk.

The second line of the input contains nn integers, representing the initial order of the attractions.

The third line of the input contains kk integers, representing the kk available swap plans.

Output data

The only line of the output should contain nn elements, representing the best order for showing the attractions.

Constraints and clarifications

  • 1kn5 0001 \leq k \leq n \leq 5 \ 000
  • For tests worth 1010 points, 1kn81 \leq k \leq n \leq 8
  • For tests worth 3030 more points, 1kn1001 \leq k \leq n \leq 100

Example 1

stdin

8 1
1 4 3 5 7 2 8 6
8

stdout

1 4 3 5 7 2 8 6

Example 2

stdin

8 7
2 4 1 5 3 6 7 8
7 6 3 8 2 5 1

stdout

1 2 3 4 5 6 7 8

Log in or sign up to be able to send submissions!