#C6401. Maximum Server Slot Occupation
Maximum Server Slot Occupation
Maximum Server Slot Occupation
You are given n servers and m new processes. Each server i has a certain capacity, meaning it can accommodate processes up to that many slots. Each process requires a given number of slots. A process must be assigned entirely to a single server, and the total slots allocated on any server cannot exceed its capacity. Your task is to select a subset of processes and assign them to the servers such that the total number of slots occupied (i.e. the sum of the process slot requirements that are assigned) is maximized.
Note: Not every process may be assigned if it cannot fit into any server’s remaining capacity. This is a multi-knapsack problem where each process has a weight (and value) equal to its slot requirement.
The input guarantees that the numbers of servers and processes are small. Use an algorithm (for example, backtracking with memoization) to compute the solution, ensuring that the program runs within the time limits.
inputFormat
The input is given via standard input (stdin) in the following format:
n m s1 s2 ... sn p1 p2 ... pm
Where:
n
is the number of servers.m
is the number of processes.- The second line contains
n
integers (s1, s2, ..., sn
), wheresi
denotes the number of slots available on the i-th server. - The third line contains
m
integers (p1, p2, ..., pm
), wherepi
denotes the number of slots required by the i-th process.
outputFormat
Output a single integer to standard output (stdout) — the maximum total number of slots occupied by the processes that can be assigned to the servers without exceeding any server’s capacity.
## sample5 3
4 6 2 7 5
3 2 1
6