#C9719. Maximum Container Loading Weight
Maximum Container Loading Weight
Maximum Container Loading Weight
You are given a set of ships and a set of containers. Each ship has a maximum weight capacity and each container has a weight. The goal is to maximize the total weight of containers loaded onto the ships without exceeding any ship's capacity. A greedy strategy is used by sorting the ships and containers in descending order, and then iteratively loading each ship with the heaviest remaining container that fits.
The mathematical formulation of the problem is as follows: [ \text{Maximize } W = \sum_{i=1}^{n} w_i \quad \text{subject to} \quad \sum_{j \in S_k} w_j \leq C_k, \quad \forall k, ] where:
- (C_k) is the capacity of the (k)-th ship,
- (w_j) is the weight of the (j)-th container,
- (S_k) is the set of containers loaded on the (k)-th ship.
This description aims to model the container loading optimization with a greedy selection process.
inputFormat
The input is given via standard input (stdin) and has the following format:
The first line contains two integers (s) and (c) separated by a space, where (s) is the number of ships and (c) is the number of containers. The second line contains (s) integers representing the capacities of the ships. The third line contains (c) integers representing the weights of the containers.
outputFormat
Output a single integer representing the maximum total weight of containers that can be loaded onto the ships, printed via standard output (stdout).## sample
2 3
5000 6000
2000 3000 4000
9000