#K3771. Greedy Tray Packing
Greedy Tray Packing
Greedy Tray Packing
You are given a tray with a fixed capacity and a list of weights representing the produce items. You need to determine the minimal number of trays required to store all produce using a greedy algorithm.
The greedy approach works as follows: for each produce item (with weight w), try to place it in an existing tray if the sum of weights in that tray plus w is at most the tray capacity C. If no such tray exists, start a new tray. Formally, if a tray currently has a total weight S, you can add an item of weight w if and only if \[ S + w \le C \]
Your task is to compute the minimal number of trays needed to accommodate all produce items.
inputFormat
The input is given from stdin in the following format:
C w1 w2 w3 ... wn
Where C (an integer) is the capacity of each tray, and w1, w2, \ldots, wn are the weights of the produce items separated by spaces.
outputFormat
Output a single integer to stdout representing the minimal number of trays required.
## sample10
2 3 7 8 1 4 2 6
4