Black Cells (Codeforces 1821D)
Tags: Math, binary search

You are playing with a really long strip consisting of $$$10^{18}$$$ white cells, numbered from left to right as $$$0$$$, $$$1$$$, $$$2$$$ and so on. You are controlling a special pointer that is initially in cell $$$0$$$. Also, you have a "Shift" button you can press and hold.In one move, you can do one of three actions:   move the pointer to the right (from cell $$$x$$$ to cell $$$x + 1$$$);  press and hold the "Shift" button;  release the "Shift" button: the moment you release "Shift", all cells that were visited while "Shift" was pressed are colored in black.  (Of course, you can't press Shift if you already hold it. Similarly, you can't release Shift if you haven't pressed it.)Your goal is to color at least $$$k$$$ cells, but there is a restriction: you are given $$$n$$$ segments $$$[l_i, r_i]$$$ — you can color cells only inside these segments, i. e. you can color the cell $$$x$$$ if and only if $$$l_i \le x \le r_i$$$ for some $$$i$$$.What is the minimum number of moves you need to make in order to color at least $$$k$$$ cells black?

Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le k \le 10^9$$$) — the number of segments and the desired number of black cells, respectively.The second line contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$1 \le l_1 < l_2 < \dots < l_n \le 10^9$$$), where $$$l_i$$$ is the left border of the $$$i$$$-th segment.The third line contains $$$n$$$ integers $$$r_1, r_2, \dots, r_n$$$ ($$$1 \le r_i \le 10^9$$$; $$$l_i \le r_i < l_{i + 1} - 1$$$), where $$$r_i$$$ is the right border of the $$$i$$$-th segment.Additional constraints on the input:  every cell belongs to at most one segment;  the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. 

Output: For each test case, print the minimum number of moves to color at least $$$k$$$ cells black, or $$$-1$$$ if it's impossible.

Note: In the first test case, one of the optimal sequences of operations is the following:   Move right: pointer is moving into cell $$$1$$$;  Press Shift;  Release Shift: cell $$$1$$$ is colored black;  Move right: pointer is moving into cell $$$2$$$;  Move right: pointer is moving into cell $$$3$$$;  Press Shift;  Move right: pointer is moving into cell $$$4$$$;  Release Shift: cells $$$3$$$ and $$$4$$$ are colored in black.  We've colored $$$3$$$ cells in $$$8$$$ moves.In the second test case, we can color at most $$$8$$$ cells, while we need $$$20$$$ cell to color.In the third test case, one of the optimal sequences of operations is the following:   Move right: pointer is moving into cell $$$1$$$;  Move right: pointer is moving into cell $$$2$$$;  Move right: pointer is moving into cell $$$3$$$;  Press Shift;  Move right: pointer is moving into cell $$$4$$$;  Move right: pointer is moving into cell $$$5$$$;  Release Shift: cells $$$3$$$, $$$4$$$ and $$$5$$$ are colored in black.  We've colored $$$3$$$ cells in $$$7$$$ moves.

Sample Inputs:
4
2 3
1 3
1 4
4 20
10 13 16 19
11 14 17 20
2 3
1 3
1 10
2 4
99 999999999
100 1000000000

Sample Outputs:
8
-1
7
1000000004

Solution (Python):
for _ in range(int(input())):
    (n, k) = map(int, input().split())
    l = list(map(int, input().split()))
    r = list(map(int, input().split()))
    one = 0
    dl2 = 0
    ans = 10 ** 18 + 10
    for i in range(n):
        dist = r[i] - l[i] + 1
        if dist == 1:
            one += 1
        else:
            dl2 += dist
        if dl2 < k and dl2 + one >= k:
            cur = r[i] + 2 * (i + 1 - one) + 2 * (k - dl2)
            ans = min(ans, cur)
        elif dl2 >= k:
            cur = r[i] + 2 * (i + 1 - one) - (dl2 - k)
            ans = min(ans, cur)
            break
    if ans == 10 ** 18 + 10:
        print(-1)
    else:
        print(ans)

Solution (Python):
for _ in range(int(input())):
    (x, y) = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    c = 0
    p = 0
    ans = 2 * 10 ** 18
    for i in range(x):
        p += b[i] == a[i]
        c += b[i] - a[i] + 1
        if c < y:
            continue
        r = c - y
        ans = min(ans, 2 * (i + 1) + b[i] - r - min(r, p))
    if ans != 2 * 10 ** 18:
        print(ans)
    else:
        print(-1)

Solution (Python):
yut = int(input())
for _ in range(yut):
    (bun, kty) = map(int, input().split())
    lr = list(map(int, input().split()))
    r = list(map(int, input().split()))
    cnt = 0
    one = 0
    der = pow(2, 61) - 1
    for i in range(bun):
        cnt += r[i] - lr[i] + 1
        if r[i] == lr[i]:
            one += 1
        if cnt >= kty:
            der = min(der, r[i] - (cnt - kty) + (i + 1) * 2 - min(one, cnt - kty))
    if cnt < kty:
        print(-1)
    else:
        print(der)