PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000350. Chìa khóa văn phòng (Office Keys)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Chìa khóa văn phòng (Office Keys)

Office Keys

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn people and kk keys along a straight line. The office is at coordinate pp, person ii starts at aia_i, and key jj is at bjb_j.

Each person must travel to a key, collect it, and then reach the office. A key can be collected by only one person, even if several people arrive at it simultaneously. Passing a key without collecting it is allowed. People may move simultaneously at a speed of one distance unit per second.

Find the earliest time when all nn people have arrived at the office, each with a key.

Input

  • The first line gives integers n,k,pn,k,p: the numbers of people and keys and the office coordinate.
  • The second line gives the nn initial positions a1,a2,…,ana_1,a_2,\ldots,a_n in arbitrary order.
  • The third line gives the kk key positions b1,b2,…,bkb_1,b_2,\ldots,b_k in arbitrary order.

Output

Print the minimum time in seconds as an integer.

Subtasks

  • Subtask 1 (20 points): 1≤n≤81\le n\le 8; n≤k≤10n\le k\le 10; 1≤p≤1091\le p\le 10^9; 1≤ai≤1091\le a_i\le 10^9 for all 1≤i≤n1\le i\le n; 1≤bj≤1091\le b_j\le 10^9 for all 1≤j≤k1\le j\le k; the aia_i are pairwise distinct and the bjb_j are pairwise distinct; a person and a key may share a position.
  • Subtask 2 (30 points): 1≤n≤1501\le n\le 150; n≤k≤200n\le k\le 200; 1≤p≤1091\le p\le 10^9; 1≤ai≤1091\le a_i\le 10^9 for all 1≤i≤n1\le i\le n; 1≤bj≤1091\le b_j\le 10^9 for all 1≤j≤k1\le j\le k; the aia_i are pairwise distinct and the bjb_j are pairwise distinct; a person and a key may share a position.
  • Subtask 3 (50 points): 1≤n≤10001\le n\le 1000; n≤k≤2000n\le k\le 2000; 1≤p≤1091\le p\le 10^9; 1≤ai≤1091\le a_i\le 10^9 for all 1≤i≤n1\le i\le n; 1≤bj≤1091\le b_j\le 10^9 for all 1≤j≤k1\le j\le k; the aia_i are pairwise distinct and the bjb_j are pairwise distinct; a person and a key may share a position.

Examples

Input 1

2 4 50
20 100
60 10 40 80

Output 1

50

Explanation

The person at 2020 collects the key at 4040 and reaches 5050 in 20+10=3020+10=30 seconds. The person at 100100 uses the key at 8080 and takes 20+30=5020+30=50 seconds. The second person must travel at least 5050 units to the office, so no earlier finish is possible.

Input 2

1 2 10
11
15 7

Output 2

7

Explanation

Using the key at 1515 takes 4+5=94+5=9 seconds; using the key at 77 takes 4+3=74+3=7 seconds. The latter is faster.