Knapsack (Codeforces 1447C)
Tags: Greedy, sortings

You have a knapsack with the capacity of $$$W$$$. There are also $$$n$$$ items, the $$$i$$$-th one has weight $$$w_i$$$. You want to put some of these items into the knapsack in such a way that their total weight $$$C$$$ is at least half of its size, but (obviously) does not exceed it. Formally, $$$C$$$ should satisfy: $$$\lceil \frac{W}{2}\rceil \le C \le W$$$. Output the list of items you will put into the knapsack or determine that fulfilling the conditions is impossible. If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights of items in the knapsack.

Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains integers $$$n$$$ and $$$W$$$ ($$$1 \le n \le 200\,000$$$, $$$1\le W \le 10^{18}$$$). The second line of each test case contains $$$n$$$ integers $$$w_1, w_2, \dots, w_n$$$ ($$$1 \le w_i \le 10^9$$$) — weights of the items.The sum of $$$n$$$ over all test cases does not exceed $$$200\,000$$$.

Output: For each test case, if there is no solution, print a single integer $$$-1$$$. If there exists a solution consisting of $$$m$$$ items, print $$$m$$$ in the first line of the output and $$$m$$$ integers $$$j_1$$$, $$$j_2$$$, ..., $$$j_m$$$ ($$$1 \le j_i \le n$$$, all $$$j_i$$$ are distinct) in the second line of the output  — indices of the items you would like to pack into the knapsack.If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights items in the knapsack.

Note: In the first test case, you can take the item of weight $$$3$$$ and fill the knapsack just right.In the second test case, all the items are larger than the knapsack's capacity. Therefore, the answer is $$$-1$$$.In the third test case, you fill the knapsack exactly in half.

Sample Inputs:
3
1 3
3
6 2
19 8 19 69 9 4
7 12
1 1 1 17 1 1 1

Sample Outputs:
1
1
-1
6
1 2 3 5 6 7

Solution (Python):
t = int(input())
res = []
for _ in range(t):
    (n, W) = input().split()
    (n, W) = (int(n), int(W))
    ll = [int(s) for s in input().split()]
    flags = [True] * n
    for (i, l) in enumerate(ll):
        if l > W:
            flags[i] = False
        elif l * 2 >= W:
            res.append(1)
            res.append(i + 1)
            break
    else:
        summa = 0
        indexes = []
        for (i, l) in enumerate(ll):
            if flags[i]:
                summa += l
                indexes.append(i)
                if summa * 2 >= W:
                    break
        if summa * 2 >= W:
            res.append(len(indexes))
            res.append(' '.join([str(i + 1) for i in indexes]))
        else:
            res.append(-1)
for e in res:
    print(e)

Solution (Python):
def verificar_mochila(itens, capacidade, quantidade_itens):
    soma_pesos = 0
    itens_selecionados = []
    indice = 0
    while indice < quantidade_itens:
        if itens[indice] <= capacidade:
            if soma_pesos + itens[indice] > capacidade and itens[indice] * 2 < capacidade:
                indice += 1
                continue
            else:
                if itens[indice] * 2 >= capacidade:
                    return (False, indice + 1)
                itens_selecionados.append(indice + 1)
                soma_pesos += itens[indice]
        indice += 1
    if itens_selecionados and len(itens_selecionados) != 0 and (soma_pesos * 2 >= capacidade) and (soma_pesos <= capacidade):
        return (True, itens_selecionados)
    return (False, -1)

def solve():
    numero_casos = int(input())
    contador = 0
    while contador < numero_casos:
        (quantidade_itens, capacidade_mochila) = map(int, input().split())
        (verifica, itens_selecionados) = verificar_mochila(list(map(int, input().split())), capacidade_mochila, quantidade_itens)
        if verifica:
            print(len(itens_selecionados))
            print(*itens_selecionados)
        else:
            print(-1 if itens_selecionados == -1 else 1)
            print(itens_selecionados if itens_selecionados != -1 else '')
        contador += 1
solve()

Solution (Python):
from math import ceil
T = int(input())
for t in range(T):
    (n, W) = list(map(int, input().split()))
    sum = 0
    lowLim = ceil(W / 2)
    found = False
    ans = []
    count = 0
    arr = list(map(int, input().split()))
    for i in range(n):
        if found:
            break
        inp = arr[i]
        if inp < W + 1:
            if inp >= lowLim and found == False:
                print(1)
                print(i + 1)
                found = True
                break
            elif inp < lowLim and found == False:
                ans.append(i + 1)
                sum += inp
                count += 1
                if (sum >= lowLim and sum <= W) and found == 0:
                    print(len(ans))
                    print(' '.join(map(str, ans)))
                    found = True
                    break
    if not found:
        print(-1)