#K77362. Resource Allocation Optimization
Resource Allocation Optimization
Resource Allocation Optimization
You are given a list of tasks and a set of available resources. Each task requires a certain amount of two resources: cpu and memory and has an associated cost. The tasks must be processed in the given order. For each task, if the available resources are sufficient to meet its requirements, the task is allocated and its required resources are deducted from the available pool. The total cost is the sum of the costs of the tasks that have been allocated.
Formally, let the available resources be represented as \(A_{cpu}\) and \(A_{memory}\). For a task with requirements \(r_{cpu}\) and \(r_{memory}\) and cost \(C\), the task can be allocated if and only if:
[ A_{cpu} \ge r_{cpu} \quad \text{and} \quad A_{memory} \ge r_{memory}. ]
If allocated, the available resources are updated as follows:
[ A_{cpu} := A_{cpu} - r_{cpu}, \quad A_{memory} := A_{memory} - r_{memory}. ]
The output should consist of the total cost followed by the details of the allocated tasks in the order of allocation. If no tasks can be allocated, the total cost is 0.
inputFormat
The input is given via standard input in the following format:
- Line 1: An integer
n
, representing the number of tasks. - Line 2: Two space-separated integers representing the available resources for cpu and memory respectively.
- Next
n
lines: Each line contains three space-separated integers: the required cpu, required memory, and the cost of the task.
It is guaranteed that all numbers are non-negative integers.
outputFormat
The output should be written to standard output and include:
- Line 1: An integer representing the total cost of all allocated tasks.
- For each allocated task (in the order given in the input), output a line with three space-separated integers: the task's required cpu, required memory, and its cost.
If no tasks are allocated, only output the total cost (which will be 0).
## sample0
4 8
0
</p>