#Partially ordered knapsack

def partially_ordered_knapsack(items, capacity):
    n = len(items)

    # Sort the items based on their partial order
    items.sort(key=lambda x: x[2])

    # Initialize the dynamic programming table
    dp = [[0] * (capacity + 1) for _ in range(n + 1)]

    # Fill the table
    for i in range(1, n + 1):
        weight, value, order = items[i - 1]
        for w in range(1, capacity + 1):
            if weight > w:
                dp[i][w] = dp[i - 1][w]
            else:
                dp[i][w] = max(dp[i - 1][w], dp[order][w - weight] + value)

    # Determine the selected items
    selected_items = []
    w = capacity
    for i in range(n, 0, -1):
        if dp[i][w] != dp[i - 1][w]:
            weight, value, _ = items[i - 1]
            selected_items.append((weight, value))
            w -= weight

    return dp[n][capacity], selected_items[::-1]

# Example usage
items = [(2, 10, 0), (3, 15, 1), (5, 20, 2), (7, 25, 0)]  # (weight, value, partial order) triples
capacity = 10

max_value, selected_items = partially_ordered_knapsack(items, capacity)
print("Maximum value:", max_value)
print("Selected items:")
for item in selected_items:
    print("Weight:", item[0], "Value:", item[1])
