CSES - Shared codeLink to this code:
https://cses.fi/paste/69039f8484382a29516f3f/
from sys import stdin
input = stdin.readline
n, x = map(int,input().split())
peopleWeights = list(map(int,input().split()))
#Tuple = (NumberOfElevatorRides, WeightOfLastRide) for the chosen
#subset of people
dp = [() for _ in range (1 << n)]
dp[0] = (1, 0)
#We need to use bitmasking to compute the dp table
for subset in range (1, (1 << n)):
#First ride with no people for empty subset of chosen people
dp[subset] = (21, 0)
for person in range (n):
#We check if the person is a part of the chosen subset of people
if ((subset >> person) & 1):
rides, weight = dp[subset ^ (1 << person)]
if (weight + peopleWeights[person] > x):
rides += 1
weight = min (peopleWeights[person], weight)
else:
weight += peopleWeights[person]
dp[subset] = min (dp[subset], (rides, weight))
print(dp[(1 << n) - 1][0])