Knapsack and subset sum problems in nilpotent, polycyclic, and co