Perform Easily
After battling Shikamaru, Tayuya decided that her flute is too predictable, and replaced it with a guitar. The guitar has $$$6$$$ strings and an infinite number of frets numbered from $$$1$$$. Fretting the fret number $$$j$$$ on the $$$i$$$-th string produces the note $$$a_{i} + j$$$.Tayuya wants to play a melody of $$$n$$$ notes. Each note can be played on different string-fret combination. The easiness of performance depends on the difference between the maximal and the minimal indices of used frets. The less this difference is, the easier it is to perform the technique. Please determine the minimal possible difference.For example, if $$$a = [1, 1, 2, 2, 3, 3]$$$, and the sequence of notes is $$$4, 11, 11, 12, 12, 13, 13$$$ (corresponding to the second example), we can play the first note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $$$10$$$, the minimal one will be $$$3$$$, and the answer is $$$10 - 3 = 7$$$, as shown on the picture.  
In the first sample test it is optimal to play the first note on the first string, the second note on the second string, the third note on the sixth string, the fourth note on the fourth string, the fifth note on the fifth string, and the sixth note on the third string. In this case the $$$100$$$-th fret is used each time, so the difference is $$$100 - 100 = 0$$$.  In the second test it's optimal, for example, to play the second note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $$$10$$$, the minimal one will be $$$3$$$, and the answer is $$$10 - 3 = 7$$$.  
The first line contains $$$6$$$ space-separated numbers $$$a_{1}$$$, $$$a_{2}$$$, ..., $$$a_{6}$$$ ($$$1 \leq a_{i} \leq 10^{9}$$$) which describe the Tayuya's strings.The second line contains the only integer $$$n$$$ ($$$1 \leq n \leq 100\,000$$$) standing for the number of notes in the melody.The third line consists of $$$n$$$ integers $$$b_{1}$$$, $$$b_{2}$$$, ..., $$$b_{n}$$$ ($$$1 \leq b_{i} \leq 10^{9}$$$), separated by space. They describe the notes to be played. It's guaranteed that $$$b_i > a_j$$$ for all $$$1\leq i\leq n$$$ and $$$1\leq j\leq 6$$$, in other words, you can play each note on any string.
Print the minimal possible difference of the maximal and the minimal indices of used frets.
import sys
import heapq
input = sys.stdin.readline
a = list(set(map(int, input().split())))
k = len(a)
n = int(input())
b = list(set(map(int, input().split())))
n = len(b)
a.sort()
b.sort()
c = [0 for i in range(n)]
mn = float('inf')
pq = []
for i in range(n):
    heapq.heappush(pq, (-b[i] + a[c[i]], i))
mn = b[0] - a[0]
x = float('inf')
while True:
    td = heapq.heappop(pq)
    i = td[1]
    x = min(x, -td[0] - mn)
    c[i] += 1
    if c[-1] == k:
        break
    mn = min(b[i] - a[c[i]], mn)
    heapq.heappush(pq, (-b[i] + a[c[i]], i))
print(x)
import sys
import heapq
input = sys.stdin.readline
a = list(set(map(int, input().split())))
k = len(a)
n = int(input())
b = list(set(map(int, input().split())))
n = len(b)
a.sort()
b.sort()
c = [0 for i in range(n)]
mn = float('inf')
pq = []
for i in range(n):
    heapq.heappush(pq, (-b[i] + a[c[i]], i))
    mn = min(mn, b[i] - a[c[i]])
x = float('inf')
while True:
    td = heapq.heappop(pq)
    i = td[1]
    x = min(x, -td[0] - mn)
    c[i] += 1
    if c[-1] == k:
        break
    mn = min(b[i] - a[c[i]], mn)
    heapq.heappush(pq, (-b[i] + a[c[i]], i))
print(x)
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = 'x' in file.mode or 'r' not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b'\n') + (not b)
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            (self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode('ascii'))
        self.read = lambda : self.buffer.read().decode('ascii')
        self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline()

def RL():
    return map(int, sys.stdin.readline().split())

def RLL():
    return list(map(int, sys.stdin.readline().split()))

def N():
    return int(input())

def print_list(l):
    print(' '.join(map(str, l)))
from heapq import *
a = RLL()
a.sort()
n = N()
b = RLL()
res = float('inf')
heap = [(a[0] - b[i], i) for i in range(n)]
heapify(heap)
m = min(b) - a[0]
count = [0] * n
while 1:
    (v, i) = heap[0]
    res = min(res, -v - m)
    count[i] += 1
    if count[i] == 6:
        break
    t = b[i] - a[count[i]]
    m = min(m, t)
    heapreplace(heap, (-t, i))
print(res)