atcoder#ARC114D. [ARC114D] Moving Pieces on Line

[ARC114D] Moving Pieces on Line

Score : 600600 points

Problem Statement

Let X=10100X = 10^{100}. We have a graph with a vertex for each integer XiX-X \leq i \leq X, and an undirected edge connecting Vertex ii and Vertex i+1i + 1 (we will call it Edge {i,i+1}\{ i, i + 1 \}) for each XiX1-X \leq i \leq X-1.

All edges in this graph are initially painted red. Additionally, we have NN pieces, and the ii-th of them is on Vertex aia_i.

Maroon can repeat the following operation:

  • Choose a piece. If that piece is on Vertex ii, move it to Vertex i1i-1 or Vertex i+1i + 1. Then, if the traversed edge is now painted red, repaint it blue, and vice versa.

During the process, there may be multiple pieces on the same vertex.

Maroon wants to do this operation zero or more times to make the edges painted in his desired combination of colors. The desired combination of colors is represented by an even number KK and KK integers t1<t2<<tKt_1 < t_2 < \cdots < t_K: it means that Edge {i,i+1}\{ i, i + 1 \} is red for i<t1i < t_1, blue for t1i<t2t_1 \leq i < t_2, \cdots, red for tKit_K \leq i. More formally, for each odd number j=1,3,,K1j = 1, 3, \cdots, K-1, Edge {i,i+1}\{ i, i + 1 \} is blue for each ii such that tji<tj+1t_j \leq i < t_{j+1}; all other edges are red.

Find the minimum number of operations required to make the edges painted in the desired combination of colors. If it is impossible to do so, print 1-1.

Constraints

  • 1N50001 \leq N \leq 5000
  • 2K50002 \leq K \leq 5000
  • KK is even.
  • ai109|a_i| \leq 10^9
  • ti109|t_i| \leq 10^9
  • ti<ti+1t_i < t_{i+1}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN KK

a1a_1 a2a_2 \cdots aNa_N

t1t_1 t2t_2 \cdots tKt_K

Output

Print the minimum number of operations required to make the edges painted in the desired combination of colors. If it is impossible to do so, print 1-1.

2 2
2 -1
-2 2
4

As shown below, we can achieve the desired state in four operations, which is optimal since we need to repaint four edges.

The following is the initial situation. For convenience, we have omitted edges to the left of 3-3 and the right of 33.

0

We move the piece on 1-1 to 2-2 to get the following:

1

Then, we move the piece on 22 to 11 to get the following:

2

Then, we move the piece on 11 to 00 to get the following:

3

Then, we move the piece on 00 to 1-1 to get the following, which is the desired state:

last

2 2
2 2
5 8
9

There can be multiple pieces on the same vertex already in the beginning.

3 4
1 3 5
0 2 4 6
-1
4 4
3 4 5 6
3 4 5 6
2