EPS = 1e-07

def solve(arr, total, n):
    while True:
        xs = np.random.choice(arr, n - 1)
t = xs.sum()
i = arr.searchsorted(total - t)
if abs(t + arr[i] - total) < EPS:
    return np.append(xs, arr[i])
filling numpy array with random element from another array