Perform Easily (Codeforces 1435C)
Tags: DP, two pointers

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.  

Input: 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.

Output: Print the minimal possible difference of the maximal and the minimal indices of used frets.

Note: 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$$$.  

Sample Inputs:
1 4 100 10 30 5
6
101 104 105 110 130 200

1 1 2 2 3 3
7
13 4 11 12 11 13 12

Sample Outputs:
0

7

Solution (Python):
from heapq import *
import sys
input = sys.stdin.readline
a = sorted(map(int, input().split()))
n = int(input())
b = sorted(map(int, input().split()))
u = [6 * (i - a[5]) + 5 for i in b]
heapify(u)
h = b[-1] - a[5]
m = h - u[0] // 6
for i in range(5 * n):
    v = heappop(u)
    if v % 6:
        w = v - 1 + 6 * (a[v % 6] - a[v % 6 - 1])
        if w // 6 > h:
            h = w // 6
        heappush(u, w)
        if h - u[0] // 6 < m:
            m = h - u[0] // 6
    else:
        break
print(m)

Solution (Python):
from heapq import *
import sys
Y = sys.stdin.readline
Z = lambda : sorted(map(int, Y().split()))
a = Z()
n = Z()[0]
b = Z()
u = [6 * (i - a[5]) + 5 for i in b]
heapify(u)
h = b[-1] - a[5]
m = h - u[0] // 6
for i in range(5 * n):
    v = heappop(u)
    if v % 6:
        w = v - 1 + 6 * (a[v % 6] - a[v % 6 - 1])
        if w // 6 > h:
            h = w // 6
        heappush(u, w)
        if h - u[0] // 6 < m:
            m = h - u[0] // 6
    else:
        break
print(m)

Solution (Python):
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[i] == k:
        break
    mn = min(b[i] - a[c[i]], mn)
    heapq.heappush(pq, (-b[i] + a[c[i]], i))
print(x)