#P7343. Electron Arrangement Problem

    ID: 20541 Type: Default 1000ms 256MiB

Electron Arrangement Problem

Electron Arrangement Problem

You are given a row of electrons, each with a distinct energy. Your task is to arrange the electrons in ascending order by energy using only adjacent swaps. In other words, the electron with the smallest energy should be as far left as possible, the next smallest should follow, and so on.

However, there are m mysterious forces present, which act as barriers. For each given position xi, the electron at position xi and the electron at position xi+1 cannot be swapped. This means that the electrons are partitioned into segments (or components) such that no electron can cross a barrier. Your goal is to make the electron arrangement as sorted as possible under these conditions. That is, within each contiguous segment (between barriers), you must sort the electrons in ascending order.

Note: Use LaTeX format for formulas (e.g., $m$ and $x_i$).

inputFormat

The input consists of three lines:

  • The first line contains two integers n and m — the number of electrons and the number of barriers.
  • The second line contains n integers representing the energy of each electron.
  • The third line contains m integers, where each integer xi indicates that the electron at position xi (1-indexed) cannot be swapped with the electron at position xi+1. If m is 0, this line will be empty.

outputFormat

Print the final arrangement of the electrons after sorting each segment individually, with the energies separated by a space.

sample

5 1
5 3 4 1 2
3
3 4 5 1 2