# 50019. Medicine

## Problem Description

Write a program to process medicine. A medicine has up to 2 active ingredients, and up to 5 inactive ingredients. A ingredient has a name (up to 127 characters) and a weight (a non-negative integer no more than 5000). Now implement the following functions about a medicine.

• void init(Medicine *medicine);
Initialize the medicine so that it does not have any ingredients.
• int addActive(Medicine *medicine, char *name, int weight);
Add the weight of active ingredient of name by the given weight, and return the weight after addition. If we try to add a new active ingredient and the number of active ingredient is already 2, then do not add it and return -1 as warning.
• int addInactive(Medicine *medicine, char *name, int weight);
Add the weight of inactive ingredient of name by the given weight, and return the weight after addition. If we try to add a new inactive ingredient and the number of active ingredient is already 5, then do not add it and return -1 as warning.
• void print(Medicine *medicine);
Print active ingredients then inactive ingredients. You should print both in non-decreasing order of weight. If the two ingredients have the same weight then print the ingredients in dictionary order.
• int totalWeight(Medicine *medicine);
Return the total weight of this medicine.
• char *maxIngredient(Medicine *medicine);
Return the name of the ingredient that has the maximum weight. If there are two ingredients with the same weight, then return the one appears earlier in dictionary order. If there are no ingredients in the medicine, return NULL.

• 10pt. Implement init, addActive, and totalWeight. It is guaranteed that we will not add more than 2 active ingredients.
• 15pt. Implement init, addActive, totalWeight, and maxIngredient. It is guaranteed that we will not add more than 2 active ingredients.
• 15pt. Implement init, addActive, addInactive, totalWeight, and maxIngredient. It is guaranteed that we will not add more than 2 active ingredients or 5 inactive ingredients.
• 30pt. Implement init, addActive, addInactive, and totalWeight. We may add more than 2 active ingredients or 5 inactive ingredients.
• 30pt. Implement all functions including print.

## main.c

12345678910111213141516171819202122232425262728293031323334353637383940#include "medicine.h"#include <stdio.h>#include <stdbool.h>#include <stdlib.h>#include <string.h>#include <assert.h> void test_all() {    int n, m;    scanf("%d %d", &n, &m);     Medicine *medi = (Medicine *) malloc(sizeof(Medicine) * n);    int cmd, mid, weight;    char name[128];    for (int i = 0; i < n; i++)        init(&medi[i]);    for (int i = 0; i < m; i++) {        scanf("%d %d", &cmd, &mid);        if (cmd == 1) {         // addActive            scanf("%s %d", name, &weight);            int af = addActive(&medi[mid], name, weight);            printf("af %d\n", af);        } else if (cmd == 2) {    // addInactive            scanf("%s %d", name, &weight);            int bf = addInactive(&medi[mid], name, weight);            printf("bf %d\n", bf);        } else if (cmd == 3) {    // print            print(&medi[mid]);        } else if (cmd == 4) {    // totalWeight            printf("weight = %d\n", totalWeight(&medi[mid]));        } else if (cmd == 5) {    // maxIngredient            char *ret = maxIngredient(&medi[mid]);            printf("main ingredient = %s\n", ret == NULL ? "NOT FOUND" : ret);        }    }}int main() {    test_all();    return 0;}


## Sample Input

2 321 1 light 104 15 11 1 land 905 13 14 01 0 pangfeng 984 05 01 0 pangfeng 14 01 0 struggle 01 0 struggle 04 01 0 struggle 14 01 0 struggle 03 04 02 0 morris 12 0 fool 15 02 0 fool 25 02 0 morris 33 04 03 14 15 05 1


## Sample Output

af 10weight = 10main ingredient = lightaf 90main ingredient = land----- Active Ingredient begin   -----90 land10 light----- end                       ---------- Inactive Ingredient begin ---------- end                       -----weight = 0af 98weight = 98main ingredient = pangfengaf 99weight = 99af 0af 0weight = 99af 1weight = 100af 1----- Active Ingredient begin   -----99 pangfeng1 struggle----- end                       ---------- Inactive Ingredient begin ---------- end                       -----weight = 100bf 1bf 1main ingredient = pangfengbf 3main ingredient = pangfengbf 4----- Active Ingredient begin   -----99 pangfeng1 struggle----- end                       ---------- Inactive Ingredient begin -----4 morris3 fool----- end                       -----weight = 107----- Active Ingredient begin   -----90 land10 light----- end                       ---------- Inactive Ingredient begin ---------- end                       -----weight = 100main ingredient = pangfengmain ingredient = land