0/1 knapsack
#include <stdio.h>


int max(int a, int b)
{
    return (a > b) ? a : b;
}


int main()
{
    int n, W, i, w;
    printf("Enter number of items and knapsack capacity: ");
    scanf("%d %d", &n, &W);


    int weight[n], profit[n];
    printf("Enter weight and profit of each item:\n");
    for (i = 0; i < n; i++)
        scanf("%d %d", &weight[i], &profit[i]);


    int dp[n + 1][W + 1];


    for (i = 0; i <= n; i++)
    {
        for (w = 0; w <= W; w++)
        {
            if (i == 0 || w == 0)
                dp[i][w] = 0;
            else if (weight[i - 1] <= w)
                dp[i][w] = max(profit[i - 1] + dp[i - 1][w - weight[i - 1]], dp[i - 1][w]);
            else
                dp[i][w] = dp[i - 1][w];
        }
    }


    printf("Maximum Profit: %d\n", dp[n][W]);
    return 0;
}
