#C643. Task Combinations
Task Combinations
Task Combinations
You are given a list of positive integers, where each integer represents the time required to complete a task, and a target value \(T\). Your objective is to find all unique subsets of tasks such that the sum of the tasks in each subset equals \(T\). Each task can be used at most once in each subset, and the list may contain duplicate values.
For example, if the list of tasks is [2, 3, 6, 7] and \(T = 7\), the only valid subset is [7]. If the list is [2, 3, 5, 7] and \(T = 10\), the valid subsets are [2, 3, 5] and [3, 7].
Solve this problem using an efficient backtracking approach. In other words, find all subsets \(S\) such that
\(\sum_{i\in S} tasks[i] = T\).
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains an integer \(n\), the number of tasks.
- The second line contains \(n\) space-separated positive integers, representing the time it takes to complete each task.
- The third line contains a single integer \(T\), which is the target sum.
outputFormat
Print each valid subset on a separate line. In each line, print the numbers in the subset in non-decreasing order, separated by a single space. The overall output should be sorted in lexicographical order based on the subsets. If no valid subset exists, print -1
(without quotes).
4
2 3 6 7
7
7
</p>