Memory has free holes of the given sizes. Processes arrive in order, and each one is put in the first hole that is big enough (first fit). The process uses only what it needs: the rest stays a smaller hole in the same place. A process that fits nowhere must wait.
Input
The first line has h and p (1 ≤ h, p ≤ 20). The second line has the h hole sizes, and the third line the p process sizes (each 1 to 10000).
Output
For each process, in order: Pi -> hole H (holes are numbered from 1), or Pi -> wait. Then free: followed by the final sizes of all h holes.
Examples
5 4 100 500 200 300 600 212 417 112 426
P1 -> hole 2 P2 -> hole 5 P3 -> hole 2 P4 -> wait free: 100 176 200 300 183
2 3 50 50 50 1 50
P1 -> hole 1 P2 -> hole 2 P3 -> wait free: 0 49
Hints
Hint 1
For each process, scan the holes from the first one.
Hint 2
The first hole at least as big as the process gets it: subtract the size from that hole.
Hint 3
If no hole is big enough, the process waits and the holes do not change.
Solution
Show one correct solution (try it yourself first)
#include <stdio.h>
int main(void) {
int h, p, hole[20], proc[20];
scanf("%d %d", &h, &p);
for (int i = 0; i < h; i++) scanf("%d", &hole[i]);
for (int i = 0; i < p; i++) scanf("%d", &proc[i]);
for (int i = 0; i < p; i++) {
int chosen = -1;
for (int j = 0; j < h; j++) {
if (hole[j] >= proc[i]) {
chosen = j;
break;
}
}
if (chosen < 0) {
printf("P%d -> wait\n", i + 1);
} else {
hole[chosen] -= proc[i];
printf("P%d -> hole %d\n", i + 1, chosen + 1);
}
}
printf("free:");
for (int i = 0; i < h; i++) printf(" %d", hole[i]);
printf("\n");
return 0;
}