from typing import Sequence


def knapsack_dynamic(capacity: int, weights: Sequence, profits: Sequence) -> float:
    """Solve 0-1 knapsack problem (maximize total profits) with dynamic programming."""
    # Time complexity: O(len(weights) * capacity)
    
    if len(weights) != len(profits):
        raise ValueError("weights and profits should be of the same length.")
    
    # table[i][j] is the max profit that can be obtained using the first i elems if the 
    # max capacity is j. table is of size (len(weights)+1) * (capacity+1).
    table = [[0] * (capacity + 1) for _ in range(len(weights) + 1)]
    for i in range(len(weights)):
        for j in range(capacity + 1):
            if weights[i] > j:
                table[i+1][j] = table[i][j]
            else:
                table[i+1][j] = max(table[i][j], profits[i] + table[i][j-weights[i]])
    return table[-1][-1]