/* file : suitcasePacking.c */ /* author : Oscar MirĂ³ (o.miro.i.lopez.feliu@student.rug.nl) */ /* Description: * Expects inputs size and maxWeight, then size lines of input each describing an item * weight and value. The program finds the maximum value obtainable by adding different * items without exceeding maxWeight. */ #include int getMax (int a, int b) { if (a > b) { return a; } return b; } int suitcase(int size, int listWeight[], int listVal[], int weight) { // stops the recursive function when max weight has been reached or no more items on // the stack if (size == 0 || weight == 0) { return 0; } // if the next item has more weight than remaining available weight, you skip it if (listWeight[size - 1] > weight) { return suitcase(size - 1, listWeight, listVal, weight); } // returns the maximum satisfaction when using the current item or skipping it int a = listVal[size - 1] + suitcase(size - 1, listWeight, listVal, weight - listWeight[size - 1]); int b = suitcase(size - 1, listWeight, listVal, weight); return getMax(a, b); } int main(int argc, char *argv[]) { int size, maxWeight; scanf("%d %d", &size, &maxWeight); int listWeight[size], listVal[size]; // assigns the values, %*s to skip the name of the item for (int i = 0; i < size; i++) { scanf("%*s %d %d", listWeight+i, listVal+i); } printf("%d\n", suitcase(size, listWeight, listVal, maxWeight)); return 0; }