We are given a problem: Navi shopkeeper, each day there are N items each with price Pi and weight Wi. The friend can carry at most Wmax total weight, and at most C items (count). The friend wants to maximize total price (sum of Pi) under both constraints: total weight <= Wmax and number of items <= C. N is up to 16. D up to 10. Pi, Wi, Wmax up to 1e9 (large). Need to output maximum total price for each day, or -1 if no solution (i.e., cannot satisfy constraints? "If his friend is not able to purchase any of the item then print -1." That likely means if it's impossible to satisfy constraints with any item? But constraints say at most C items and weight <= Wmax. Usually you can always take 0 items, but maybe they require purchase of at least one item? The statement: "If his friend is not able to purchase any of the item then print -1." So if no selection (non-empty? Or maybe if even the best selection fails? But "any of the item" suggests if he cannot purchase any item due to constraints, i.e., there is no item that individually satisfies weight <= Wmax and count <= C? Actually count constraint is on number of items; if we take 0 items, count=0 which is <= C and weight=0 <=Wmax. That would be a trivial solution with total price 0. But they want -1, so they require at least one item? Or perhaps the constraints are that he must buy at most C items, but also he must buy something? The sample: N=3, items: (10,20),(20,30),(30,40). Wmax=50, C=2. The answer is 30. If we take item1 (10) and item2 (20) total weight 50, total price 30. That's max. Could we take item3 alone? weight 40, price 30, also price 30, but weight 40 (<50) and count 1. That's also 30. But perhaps item3 alone is also 30. So answer is 30. If no selection gives weight <=Wmax and count <=C? Possibly if all items weigh >Wmax individually, but maybe combination of lighter items? But weight is up to 1e9, but Wmax may be smaller. If there's no subset meeting weight <=Wmax, then we cannot purchase any items? But 0 items would be allowed? They said "not able to purchase any of the item", which suggests no items can be purchased (i.e., even single item exceeds weight or C=0 maybe). However C>=1 (constraints C>=1). So we must assume that the friend must purchase at least one item? Possibly "any of the item" meaning if there is no subset that satisfies both constraints, then print -1. But the empty subset always satisfies constraints; but maybe the problem implicitly requires the purchase to be non-empty? Let's check typical knapsack with count constraint: usually maximize profit; if no feasible non-empty selection, you output 0. But they ask -1. So they consider that the friend must buy something; if he cannot buy anything (i.e., even the cheapest item exceeds Wmax? But if he takes 0 items, that would be nothing, so no purchase. So indeed if even a single item cannot be taken due to weight > Wmax, maybe he can take combination of smaller items? But if all items weight > Wmax individually, but sum of some combination could be >? Actually if each item weight > Wmax, then any non-empty subset would have total weight > Wmax because the sum would be at least the smallest weight which > Wmax. So no feasible non-empty subset. In that case, they would output -1. That makes sense. So we need to find maximum total price among all non-empty subsets of items such that total weight <= Wmax and total number of items <= C. If no such subset exists, output -1.
Given N <= 16, we can enumerate all subsets (2^N up to 65536) which is fine for each day (D<=10). For each subset, compute sumP, sumW, count, check constraints, and keep max sumP if count>0? Actually we must consider only subsets with count >=1 (non-empty) because empty subset gives 0 but they'd print -1 if no purchase possible, so we ignore empty. So simply iterate over all non-empty subsets, compute if sumW <= Wmax and count <= C, then update max.
But careful: N up to 16, but Pi and Wi up to 1e9, sums up to maybe 1.6e10, which fits in 64-bit (long long). So use 64-bit ints.
However, there might be some twist: The constraints mention 1 < = C <= 20. So C can be up to 20, but N <=16, so C >= N possible; but constraint is max count.
We can simply brute force.
But is there any other complexity? Since D <=10, each day up to 65536 subsets, it's trivial.
But wait, sample input format: first line D. For each day: first line N. Then N lines each with Pi and Wi. Then a line with Wmax and C. Output: For each day output "For Day #day_number:" then newline, then the max total price or -1.
Let's test with sample: D=1, N=3, items, then line "50 2". Output: "For Day #1:\n30". That matches.
So solution: For day d from 1 to D:
- read N.
- create arrays P[0..N-1], W[0..N-1].
- read N lines, each Pi, Wi.
- read Wmax, C.
- best = -1 (since prices positive? Actually Pi >=1, so best will be at least 1 if feasible. Use -1 to indicate no feasible subset found.)
- iterate over mask from 1 to (1<<N)-1:
sumP=0, sumW=0, count=0.
For i in 0..N-1: if mask & (1<<i): sumP+=P[i]; sumW+=W[i]; count++.
If count <= C and sumW <= Wmax and sumP > best: best = sumP.
- output "For Day #d:" newline best.
But careful about integer overflow: sums up to N * 1e9 = 1.6e10, fits in 64-bit signed (max about 9e18). So safe.
Edge cases: C=0? According to constraints, C >=1, so ignore.
But wait: Could it be that N=0? According to constraints, N>=1? It says 1 <= N <= 16? Actually "1 < = N < = 16" perhaps means 1 <= N <= 16. So N at least 1.
Thus solution straightforward.
But is there any catch? The problem statement: "He will give his requirements as Wmax (maximum weight he can carry) and C (maximum count of the items he can carry)." So friend can carry at most Wmax weight and at most C items. So it's a knapsack with cardinality constraint.
Given N small, brute-force is fine.
But maybe they want to handle large numbers, but that's okay.
Thus answer: implement as described.
But wait: The sample output prints "For Day #1:" on first line and "30" on next line. Note the colon and space? Actually it's "For Day #1:" then newline then "30". So exactly.
I'll implement accordingly.
But maybe we need to consider that the friend may not take any items if constraints cannot be satisfied; but we output -1.
Now we should ensure that we handle input parsing correctly: first line D, then for each day we read N, then N lines each with two ints, then line with Wmax and C. There might be extra spaces or newlines.
Implementation in C++: use long long for sums.
Pseudo:
#include
#include
using namespace std;
int main() {
int D;
cin >> D;
for (int day = 1; day <= D; day++) {
int N;
cin >> N;
vector P(N), W(N);
for (int i = 0; i < N; i++) {
cin >> P[i] >> W[i];
}
long long Wmax;
int C;
cin >> Wmax >> C;
long long best = -1;
for (int mask = 1; mask < (1 << N); mask++) {
int cnt = 0;
long long sumP = 0, sumW = 0;
for (int i = 0; i < N; i++) {
if (mask & (1 << i)) {
cnt++;
sumP += P[i];
sumW += W[i];
}
}
if (cnt <= C && sumW <= Wmax) {
if (sumP > best) best = sumP;
}
}
cout << "For Day #" << day << ":" << endl;
cout << best << endl;
}
return 0;
}
Check sample: day=1, mask 1: sumP=10, sumW=20, cnt=1, check: 1<=2,20<=50 => best=10. mask 2: P=20,W=30,cnt=1 => best=20. mask 3: sumP=30,sumW=50,cnt=2 => best=30. mask 4: sumP=30,W=40,cnt=1 => sumP=30 equal best? still best=30. mask5: items 1&3: sumP=40,sumW=60,cnt=2 => fails weight. mask6: items2&3: sumP=50,sumW=70 fails. mask7: all: sumP=60,sumW=90 fails. So best=30. Output: For Day #1: newline 30.
All good.
But is there any nuance about "maximum total price"? Yes.
Thus answer done.
But wait: Could there be a scenario where best remains -1 because no feasible non-empty subset? Then we output -1. That matches.
Potential pitfalls: We used long long for sums, but Wmax and Wi also up to 1e9, so long long.
Time complexity: O(D * 2^N * N). With D=10, N=16, worst-case 106553616 = ~10 million, fine.
Alternative approach: Use meet-in-the-middle, but not needed.
Thus final answer.
But the user asked: "Navi is a famous shopkeeper..." They likely expect solution.
Thus respond with explanation and code.
Now the instruction: The user says "Navi is a famous shopkeeper...". They want solution. So provide final answer with code and explanation.
We'll provide explanation of brute-force enumeration, complexity, and code in C++.
Make sure to include input reading and output.
Let's output solution in final. We can solve this problem by brute-force enumeration of all subsets because N ≤ 16. For each non-empty subset we compute total price, total weight, and number of items. If the subset satisfies both constraints (total weight ≤ Wmax and item count ≤ C), we update the maximum price. If no feasible non-empty subset exists, we output -1.
The constraints D ≤ 10 make this approach efficient, with a total of at most 10 × 2^16 × 16 ≈ 10 million operations.
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
int D;
cin >> D;
for (int day = 1; day <= D; ++day) {
int N;
cin >> N;
vector<ll> price(N), weight(N);
for (int i = 0; i < N; ++i)
cin >> price[i] >> weight[i];
ll Wmax;
int C;
cin >> Wmax >> C;
ll best = -1;
for (int mask = 1; mask < (1 << N); ++mask) {
int cnt = 0;
ll sumP = 0, sumW = 0;
for (int i = 0; i < N; ++i) {
if (mask & (1 << i)) {
++cnt;
sumP += price[i];
sumW += weight[i];
}
}
if (cnt <= C && sumW <= Wmax) {
if (sumP > best) best = sumP;
}
}
cout << "For Day #" << day << ":\n";
cout << best << "\n";
}
return 0;
}