编辑代码

def bean_knapsack(weights, values, capacity):
    n = len(weights)
    ratios = [(values[i] / weights[i], weights[i], values[i]) for i in range(n)]
    ratios.sort(reverse=True, key=lambda x: x[0])

    total_value = 0
    current_weight = 0

    for ratio, weight, value in ratios:
        if current_weight + weight <= capacity:
            current_weight += weight
            total_value += value

    return total_value

# 示例
weights = [2, 3, 4, 5]
values = [3, 4, 5, 6]
capacity = 5

result = bean_knapsack(weights, values, capacity)
print("Maximum value:", result)