We are asked to solve a typical problem: given N books (or items), each with a cost C_i and M types of understanding (or parameters) A_{i,j}. We need to achieve at least X in each of the M parameters, by selecting a subset of books. The goal is to minimize total cost. If not possible, output -1.
Constraints: N,M up to 12, X up to 1e5, C_i and A_{i,j} up to 1e5. So N and M are small, but the values can be large.
This is a variant of knapsack / multi-dimensional knapsack with small M and N. However, the values can be large, up to 1e5, and N is 12, so we can brute-force all subsets (2^12 = 4096) and check if for each subset the sum of A over selected books meets X for each j. Among those that meet, take min cost.
But note: M is up to 12 as well, so checking each subset is fine. But wait: N and M are both up to 12. So brute-force subsets 2^N = 4096, each check M sums = up to 124096 ~ 50k operations, trivial. However, careful: The values could be up to 1e5, but sums could be up to N1e5 = 1.2e6, still fine.
So solution: enumerate subsets via bitmask from 0 to (1<<N)-1. Compute total cost and array of M sums. If all sums >= X, update answer with min cost.
If answer remains INF, output -1 else answer.
But we must ensure to handle large numbers: cost up to 1e5 * 12 = 1.2e6, within int. But X and A could be up to 1e5, but we are just summing. Use int (32-bit) is enough (sum up to 121e5=1.2e6). But if M=12, N=12, sum per dimension is at most 12100000=1.2e6, which fits in 32-bit signed int. But also C_i up to 1e5, total cost up to 1.2e6. So int is fine.
But wait: The example output for the third test is 1067, which is relatively small. So straightforward.
Edge Cases: If N=0? But N >= 1 per constraints. M can be 0? M >= 1? The problem says 1 <= N, M <= 12, so M at least 1.
We'll implement: read N, M, X. Then for each i, read C_i and M integers A_i_j. Then loop mask 1..(1<<N)-1 (including 0? The objective to achieve X, if no book selected, sum=0 for each, likely not unless X=0 but X>=1, so ignore 0). For each mask, compute total cost and an array sum of size M initialized to 0. For each bit i set, add C_i to cost and add A_i_j to sum[j]. After that, check if all sum[j] >= X. If yes, update min_cost = min(min_cost, cost). Finally, if min_cost is INF, print -1 else print min_cost.
Time complexity: O(2^N * N * M) which is about 4096 * 12 * 12 ~ 589k operations, trivial.
But we could also optimize with DP (like DP over bitset) but not needed.
Thus answer is simple.
But we need to confirm that the problem statement says "All values in input are integers." and constraints given. That's it.
But wait: The problem might be from AtCoder ABC 167 "Skill Up" or similar. Indeed, I recall AtCoder ABC 167 B? Actually ABC 167 C? Actually ABC 167 C is "Skill Up" with N up to 12, M up to 12, X up to 10^5. That's exactly this problem. So solution is indeed brute force.
Thus we can code.
We should note that N and M are both up to 12, but input example 3 has N=8, M=5. So 2^8=256 subsets, fine.
Thus we can proceed.
Implementation details: In Python, we can use bitmask and loops.
Pseudo:
def solve():
import sys
input = sys.stdin.readline
N, M, X = map(int, input().split())
C = []
A = []
for _ in range(N):
line = list(map(int, input().split()))
C.append(line[0])
A.append(line[1:])
ans = float('inf')
for mask in range(1, 1 << N):
cost = 0
understand = [0]*M
for i in range(N):
if mask >> i & 1:
cost += C[i]
for j in range(M):
understand[j] += A[i][j]
if all(x >= X for x in understand):
ans = min(ans, cost)
if ans == float('inf'):
print(-1)
else:
print(ans)
That's it.
But we might want to early break? Not needed.
Alternatively, we could use itertools.combinations, but bitmask is easier.
Now we have answer.
Given we are ChatGPT and need to produce final answer, we can just present the solution.
But there is nuance: We must ensure we read input correctly: each line includes C_i then M integers.
All good.
Thus final output: print answer.
We can also use a more efficient method: DP with bitset for each skill? But not needed.
Double-check constraints: X up to 1e5, and sum of A can be large but within int. But Python int is arbitrary, so fine.
Edge case: N=12, M=12, worst-case 4096 masks, each mask we iterate over N=12, each j M=12, so operations ~ 40961212 = 589k, fine.
So solution is done.
Now we need to output -1 if not possible.
Now I'll produce final answer.
But the prompt says: "Constraints ... Input ... Output ... Examples" and we need to provide solution? Actually this is a typical coding problem, and ChatGPT is expected to output code? Or just answer? The problem says "If the objective is not achievable, print -1; otherwise, print the minimum amount of money needed to achieve it." So typical programming problem. As ChatGPT, we should produce a solution in some language? Usually these problems are solved by writing code. The user likely expects a solution explanation and maybe code. However, the instruction at top: "You are an AI programming assistant. ... Provide a well-reasoned, step-by-step approach to solving the problem, and then write the final code implementation." So we need to provide reasoning and final code.
We must answer in English.
We'll provide a step-by-step explanation and then provide Python code.
Make sure to mention that we brute force all subsets because N is small.
Thus final answer: Python solution.
But also we could use recursion or DP. We'll just provide bitmask.
Now produce final answer. We can solve the problem by enumerating all subsets of books. Since N ≤ 12, there are at most 2^12 = 4096 subsets, which is very small. For each subset we compute the total cost and the total understanding for each of the M algorithms. If for every algorithm the total is at least X, we update the answer with the minimum cost. If no subset satisfies the condition, we output -1.
Steps:
Read N, M, X.
Store the cost C[i] and the understanding values A[i] for each book i.
Initialize ans = INF (e.g., a very large number).
Iterate over all bitmasks from 1 to (1<<N)-1 (skip the empty set because X ≥ 1):
For each set bit i, add C[i] to a running cost and accumulate A[i][j] into an array sums of length M.
After processing all bits, if every sums[j] >= X, update ans = min(ans, cost).
If ans remains INF, print -1; otherwise, print ans.
The complexity is O(2^N * N * M), which is well within limits.
Below is the Python implementation.
python
import sys
def solve() -> None:
input = sys.stdin.readline
N, M, X = map(int, input().split())
C = []
A = []
for _ in range(N):
row = list(map(int, input().split()))
C.append(row[0])
A.append(row[1:])
INF = 10**18
ans = INF
# Iterate over all non-empty subsets
for mask in range(1, 1 << N):
cost = 0
understand = [0] * M
for i in range(N):
if (mask >> i) & 1:
cost += C[i]
ai = A[i]
for j in range(M):
understand[j] += ai[j]
if all(x >= X for x in understand):
if cost < ans:
ans = cost
if ans == INF:
print(-1)
else:
print(ans)
if __name__ == "__main__":
solve()