You're about to go on a trip around the world! On this trip you're bringing your trusted backpack, that anything fits into. The bad news is that the airline has informed you, that your luggage cannot exceed a certain amount of weight.

To make sure you're bringing your most valuable items on this journey you've decided to give all your items a score that represents how valuable this item is to you. It's your job to pack you bag so that you get the most value out of the items that you decide to bring.

Your input will consist of two arrays, one for the scores and one for the weights. You input will always be valid lists of equal length, so you don't have to worry about verifying your input.

You'll also be given a maximum weight. This is the weight that your backpack cannot exceed.

For instance, given these inputs:

    scores = [15, 10, 9, 5]
    weights = [1, 5, 3, 4]
    capacity = 8
    
The maximum score will be ``29``. This number comes from bringing items ``1, 3 and 4``.

Note: Your solution will have to be efficient as the running time of your algorithm will be put to a test.

Difficulty: EASY
Tags: Dynamic programming
Skill types: Dynamic programming

Starter code:
def pack_bagpack(scores, weights, capacity):
	

Solutions:
def pack_bagpack(scores, weights, capacity):
	load = [0] * (capacity + 1)
	for (score, weight) in zip(scores, weights):
		load = [max(l, weight <= w and load[w - weight] + score) for (w, l) in enumerate(load)]
	return load[-1]


def pack_bagpack(S, W, C):
	M = [0] * (1 + C)
	for (F, V) in enumerate(S):
		M = [max(U, M[T - W[F]] + V if W[F] <= T else 0) for (T, U) in enumerate(M)]
	return M[-1]


def pack_bagpack(scores, weights, capacity):
	sols = [[0 for j in range(capacity + 1)] for i in range(len(scores) + 1)]
	scores.insert(0, 0)
	weights.insert(0, 0)
	for (y, iy) in enumerate(sols[1:], 1):
		for (x, ix) in enumerate(iy[1:], 1):
			if weights[y] <= x:
				sols[y][x] = max(scores[y] + sols[y - 1][x - weights[y]], sols[y - 1][x])
			else:
				sols[y][x] = sols[y - 1][x]
	return sols[-1][-1]


import numpy as np

def pack_bagpack(scores, weights, capacity):
	print(len(scores))
	print(capacity)
	n = len(scores)
	dp = np.zeros((n + 1, capacity + 1))
	for i in range(1, n + 1):
		for j in range(capacity + 1):
			if j >= weights[i - 1]:
				dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weights[i - 1]] + scores[i - 1])
			else:
				dp[i][j] = dp[i - 1][j]
	return dp[n][capacity]


import sys
print(sys.getrecursionlimit())

def pack_bagpack(scores, weights, capacity):
	if capacity >= sum(weights):
		return sum(scores)
	print('{},{},{}'.format(scores, weights, capacity))
	items = [(i, j) for (i, j) in zip(scores, weights)]

	def fetchBestMax(toConsider, avail, memo={}):
		if (len(toConsider), avail) in memo:
			result = memo[len(toConsider), avail]
		elif toConsider == [] or avail == 0:
			result = (0, ())
		elif toConsider[0][1] > avail:
			result = fetchBestMax(toConsider[1:], avail, memo)
		else:
			nextItem = toConsider[0]
			(withVal, withToTake) = fetchBestMax(toConsider[1:], avail - nextItem[1], memo)
			withVal += nextItem[0]
			(withoutVal, withoutToTake) = fetchBestMax(toConsider[1:], avail, memo)
			if withVal > withoutVal:
				result = (withVal, withToTake + (nextItem,))
			else:
				result = (withoutVal, withoutToTake)
		memo[len(toConsider), avail] = result
		return result
	maxVal = fetchBestMax(items, capacity)
	print(('maxVal', maxVal))
	return maxVal[0]


def pack_bagpack(val, wt, W):
	n = len(val)
	K = [[0 for x in range(W + 1)] for x in range(n + 1)]
	for i in range(n + 1):
		for w in range(W + 1):
			if i == 0 or w == 0:
				K[i][w] = 0
			elif wt[i - 1] <= w:
				K[i][w] = max(val[i - 1] + K[i - 1][w - wt[i - 1]], K[i - 1][w])
			else:
				K[i][w] = K[i - 1][w]
	return K[n][W]


def pack_bagpack(val, w, t):
	if sum(w) <= t:
		return sum(val)
	matrix = [[0 for _ in range(t + 1)] for _ in range(len(w) + 1)]
	for i in range(1, len(w) + 1):
		for j in range(1, t + 1):
			matrix[i][j] = matrix[i - 1][j] if j < w[i - 1] else max(matrix[i - 1][j], val[i - 1] + matrix[i - 1][j - w[i - 1]])
	return matrix[-1][-1]


import time

def pack_bagpack(scores, weights, capacity):
	start_time = time.time()
	lst = list(zip(scores, weights))
	lst_sorted = sorted(lst, key=lambda x: x[0], reverse=True)
	cache_dict = {}

	def calc_item(index, weight_last):
		if index >= len(lst_sorted) or weight_last <= 0:
			return 0
		current = lst_sorted[index]
		cache_key = '{}-{}'.format(index, weight_last)
		if cache_key not in cache_dict:
			score_with_current = 0
			if weight_last >= current[1]:
				score_with_current = current[0] + calc_item(index + 1, weight_last - current[1])
			score_no_current = calc_item(index + 1, weight_last)
			cache_dict[cache_key] = max(score_with_current, score_no_current)
		return cache_dict[cache_key]
	final_score = calc_item(0, capacity)
	print('score: {}  duration---> {}'.format(final_score, time.time() - start_time))
	return final_score


def pack_bagpack(scores, weights, capacity):
	dp = [[0, []] for i in range(capacity + 1)]
	for j in range(1, len(scores) + 1):
		r = [[0, []] for i in range(capacity + 1)]
		for i in range(1, capacity + 1):
			(c, p) = (weights[j - 1], scores[j - 1])
			if c <= i and p + dp[i - c][0] > dp[i][0]:
				r[i] = [p + dp[i - c][0], dp[i - c][1] + [j - 1]]
			else:
				r[i] = dp[i]
		dp = r
	return dp[-1][0]