knapsack and triangulation implementation