We need to interpret the problem carefully. We have N pupils, each needs ti lessons (so that many lessons to be given to that pupil). There are W weeks remaining. Each week has 7 days (presumably). On each day, we can give at most one lesson total (only to one pupil). So total days available = 7 * W. But we also have constraints: each pupil is only available on certain days of the week. That is, for each pupil i, they have a set of convenient days (given as day names). On a given day of the week (like Monday), if that day is convenient for the pupil, we can schedule a lesson for that pupil on that day (provided we don't exceed one lesson per day). But note: The convenience is per day of the week, not per specific date. That is, each week, on those specific weekdays, the pupil is available. So across the W weeks, on each occurrence of those weekdays, that pupil can potentially take a lesson.
Thus we have a scheduling problem: We have W weeks, so each weekday occurs W times (e.g., there are W Mondays, W Tuesdays, etc.). On each such day slot, we can assign at most one pupil. Each pupil i requires ti lessons, but can only be assigned on days that belong to its convenient set. We need to check if there exists an assignment of lessons to days such that each pupil gets exactly ti lessons, and total lessons assigned sum to sum ti <= total days (7W), obviously necessary but not sufficient.
This is essentially a bipartite matching flow problem: On one side we have "day slots" (each weekday repeated W times). But we can aggregate: The constraints are based only on weekday type. So we have 7 types of days, each with capacity W (i.e., there are W slots for Monday, W for Tuesday, etc.). On the other side we have pupils, each needing ti lessons, but each pupil can only be served by certain day types. We need to assign lessons (each lesson consumes one slot) to pupils respecting capacities per day type.
That is a classic bipartite flow: source -> pupils with capacity ti; pupil -> day types (only those they are available) with infinite capacity (or large enough); day types -> sink with capacity W (each day type can provide at most W lessons). Then check if max flow equals total needed lessons. N up to 100, but ti up to 1e10, W up to 1e10, so capacities are huge. But we can treat this as a flow problem with capacities large, but the graph is small: 1 source, N pupil nodes, 7 day nodes, 1 sink. So we can solve using maximum flow algorithms? The capacities are huge (1e10) but the graph size is tiny. However, standard algorithms like Edmonds-Karp would not work due to large capacities because they iterate over augmenting paths many times. But because the graph is tiny and bipartite with only 7 day nodes, we could perhaps use combinatorial reasoning: it's a supply-demand problem with 7 resources. This is essentially a feasibility of a flow with integer capacities? But we need to check if there exists integer assignment, which is equivalent to checking if the bipartite flow (with fractional also works due to total unimodularity? The graph is bipartite and capacities are integers, so the extreme points are integral. So we can treat as linear programming feasibility.
But maybe we can derive a simpler condition. Since there are only 7 day types, we can treat each pupil as requiring ti units of flow from a subset of the 7 day types, with each day type having supply W. This is a classic "scheduling with weekly patterns" problem. It's like: we have W copies of each day, so total supply per day type is W. The pupils demand certain amounts from sets of day types. This is like checking if the sum of demands can be satisfied given per-day capacities. This is a flow on a small graph; we can compute max flow using greedy or specialized algorithm? Since day nodes only 7, we can try to solve using linear programming or by reduction to a matching of big numbers? But we can do maximum flow even with huge capacities: Because the graph is tiny, we could use augmenting path algorithm but we would need to augment potentially large amounts. But we can augment the maximum possible each time: each augmenting path from source to sink goes source->pupil->day->sink. But there might be multiple pupils and days. Actually there may be edges from multiple pupils to multiple days, so paths may be longer? Actually the graph is bipartite: source connects to each pupil (capacity ti). Pupil connects to each day in its available set (capacity infinite or large). Day connects to sink (capacity W). So any augmenting path is source->pupil->day->sink. There is no direct pupil-pupil or day-day edges. So each augmenting path is length 3. The maximum flow can be found by saturating each day's capacity from pupils. But we need to allocate flow from pupils to days. Since edges from pupil to day have infinite capacity, the flow problem reduces to: assign each pupil's demand across the days they can use, respecting each day's capacity.
This is exactly a bipartite flow with source and sink. The maximum flow can be computed by solving a transportation problem. Because the graph is small (N up to 100, 7 day nodes), we can do a standard max flow algorithm like push-relabel with scaling? But capacities are up to 1e10, but the number of nodes is ~108, edges ~Nci up to 1007=700, plus source and sink. A push-relabel algorithm would run in O(V^2 * sqrt(E)) or something, but it's fine. But the issue is that we need to store capacities as 64-bit integers and perform operations on them. But we can just use a standard max flow that uses BFS (Edmonds-Karp) but the runtime could be too slow if we augment by 1 each time; we need to augment by the bottleneck capacity. However, in Edmonds-Karp each augmenting path may have bottleneck of at least 1, but capacities are huge (up to 1e10) and the total flow could be up to N*1e10 = 1e12? Actually N <=100, ti up to 1e10, so total flow up to 1e12. If we augment by 1 each time, we'd need trillions of iterations, impossible. But if we augment by the bottleneck (which can be large), then each augmenting path can saturate either a pupil's remaining demand or a day's remaining capacity. In the worst case, each augmentation reduces the number of unsaturated source or sink edges. But there can be at most N+7 augmentations? Actually consider: Each augmenting path from source to sink will have bottleneck = min(residual capacity from source to pupil, residual capacity from day to sink). Since pupil->day capacity is infinite, the bottleneck is essentially min(remaining demand of pupil i, remaining capacity of day d). After such an augmentation, either the pupil's demand is fully met (source edge becomes saturated) or the day's capacity is fully used (sink edge becomes saturated) or both. So each augmentation saturates at least one edge from source or to sink. There are N source edges and 7 sink edges (day->sink). So total number of augmentations is at most N+7. That's great! Because each augmentation can be of a potentially large amount, but we can compute the amount and update capacities accordingly. The only tricky part is that we might need to augment along paths that are not just source->pupil->day->sink? But is that the only type? Since the graph is three layers, any augmenting path from source to sink must go through exactly one pupil and one day. Because source connects only to pupils, pupils connect only to days (if available), days connect only to sink. So indeed any path is of length 3. So we can simply simulate the flow by greedy assignment? However, we must consider that after some augmentations, there may be reverse flows allowing reassignments. For example, we might assign some flow from pupil A to Monday, but later we want to move some of that to Tuesday to free up Monday for another pupil. That would involve a reverse edge: source->pupil (forward) is still unsaturated? Actually if we assigned flow from pupil A to Monday, then we have a reverse edge from Monday to pupil A with capacity equal to the flow assigned. This could allow a later augmenting path like source->pupil B -> Monday? Wait, standard augmenting path in residual graph includes reverse edges. So a possible augmenting path could be: source->pupil B (forward) -> Monday? But Monday to sink forward capacity might be saturated. Then we could go Monday -> pupil A (reverse) -> some other day? Because if we previously sent flow from pupil A to Monday, the reverse edge from Monday to pupil A exists. Then we could go from pupil A to another day, say Tuesday, then to sink. This effectively reroutes some of pupil A's lessons from Monday to Tuesday, freeing up Monday capacity for pupil B. So the augmenting paths could be longer than 3. For instance, source->pupil B (forward) -> Monday (forward) is blocked because Monday->sink is saturated, but we could go Monday->pupil A (reverse) -> Tuesday (forward) -> sink. That's a path of length 5: source->pupilB->Monday->pupilA->Tuesday->sink. This allows reassigning. So the augmenting paths can involve multiple pupils and days. Thus the simplistic "saturate source or sink edges each augmentation" might not hold because an augmentation could saturate none of those edges? Actually each augmentation still reduces the deficit between supply and demand? In flow, each augmentation increases flow value by some amount. But number of augmentations could be larger than N+7 because of these reassignments. However, with such a small graph, we could run a max flow algorithm that handles capacities efficiently, like Dinic with scaling, or use the fact that it's a bipartite flow with two layers (pupils and days) and the day side only 7 nodes, we can perhaps formulate it as a linear programming problem and check feasibility using Hall's marriage-like condition? But the numbers are large, we need to check if there exists a feasible flow. This is a classic "bipartite b-matching" where left side (pupils) have demand ti and right side (day types) have capacity W each, and edges exist if pupil can use that day. This is a flow problem with capacities on both sides. Because the right side only has 7 nodes, we can apply some kind of "check all subsets of day types" condition using max flow/min cut.
The min cut in this graph: source s, pupils Pi, days Dj, sink t. The cut capacity: sum over pupils not in S? Actually consider any cut (S,T) with s in S, t in T. The cut edges are those from S to T. They can be: from s to Pi if Pi in T; from Pi to Dj if Pi in S, Dj in T; from Dj to t if Dj in S. Edges from Pi to Dj have infinite capacity (or very large). To avoid infinite capacity in cut, we must ensure that if Pi in S and Dj in T, then edge Pi->Dj is not cut, but that edge capacity is infinite, so we must avoid cutting infinite edges. Therefore, any finite capacity cut must not cut any Pi->Dj edge. That imposes constraints: For any Pi in S and Dj in T, there must be no edge between them, i.e., pupil Pi cannot be assigned to day Dj. Equivalently, if there exists an edge from Pi to Dj, then we cannot have Pi in S and Dj in T simultaneously. So in a finite cut, the set of pupils in S must have their available days all also in S (i.e., no edge from S-pupil to T-day). Similarly, for pupils in T, it's okay because edges from s to Pi (if Pi in T) are cut, capacity ti, and from Pi to Dj with Pi in T, Dj in S? That would be Dj->Pi? Actually edges are directed from Pi to Dj, so if Pi in T and Dj in S, then the edge direction is from T to S, which does not contribute to cut (cut counts edges from S to T). So we only care about edges from Pi in S to Dj in T. So to keep cut finite, we must have for every Pi in S, all days Dj that are neighbors of Pi must also be in S. In other words, S must be "closed" under the adjacency: if a pupil is in S, all its available days must also be in S. Days can be in S or T freely as long as they are not neighbors of a pupil in S that are in T.
Thus any finite capacity cut (S,T) can be characterized by a set of pupils X (those not in S? Actually let's define: s is in S, t in T. Let P be all pupils, D be all days. Let A be the set of pupils in S (since source is in S). Let B be the set of days in S. For the cut to be finite, we require: for every pupil i in A, all its neighboring days must also be in S (i.e., in B). Because if any neighbor day is in T, that edge would be cut with infinite capacity. So condition: For all i in A, N(i) ⊆ B. There is no restriction on pupils not in A (i.e., in T). Days in T (i.e., not in B) can have any neighbors, but those neighbors in A are not allowed as above.
Now cut capacity = sum over pupils i not in A of ti (since edges s->i for i in T) + sum over days j in B of W (edges day->t are from day to t, so if day is in S, edge day->t is from S to T, so capacity W added). Also, there could be edges from pupils in A to days in T, but we avoided those by condition. So capacity = sum_{i∉A} ti + sum_{j∈B} W.
Thus min cut = min_{A⊆P, B⊆D, such that for all i∈A, N(i)⊆B} (∑{i∉A} ti + ∑{j∈B} W). Equivalent to min over subsets of pupils A (those included with source) with the constraint that the set of days B must contain all neighbors of A. Since we can choose B as any superset of N(A) (the union of convenient days for pupils in A). To minimize the expression, given A, we should set B = N(A) exactly, because adding extra days to B would increase the term ∑{j∈B} W (since W is positive). So optimal B = N(A). Therefore, min cut = min{A⊆P} ( ∑{i∉A} ti + |N(A)| * W ). Because ∑{j∈N(A)} W = W * |N(A)|.
Thus the max flow equals total demand T = ∑ ti if and only if for every subset A of pupils, we have ∑{i∉A} ti + W * |N(A)| ≥ T. Rearranging: W * |N(A)| ≥ ∑{i∈A} ti? Actually T = ∑{i∈P} ti = ∑{i∈A} ti + ∑{i∉A} ti. So inequality becomes ∑{i∉A} ti + W * |N(A)| ≥ ∑{i∈A} ti + ∑{i∉A} ti → cancel ∑{i∉A} ti: W * |N(A)| ≥ ∑{i∈A} ti. So condition: For all subsets A of pupils, ∑_{i∈A} ti ≤ W * |N(A)|.
That's necessary and sufficient by max-flow min-cut theorem, given that capacities are integral and infinite edges are okay (the infinite edges effectively enforce the condition on A). So the problem reduces to: Check that for every subset of pupils, the total required lessons of that subset does not exceed W times the number of distinct weekdays that are convenient for at least one pupil in the subset.
But careful: The derivation assumed that edges from pupils to days have infinite capacity. In the input, we are not given any limit on number of lessons per day per pupil; the only limit is one lesson per day overall. So indeed pupil can take multiple lessons on different days but not more than one per day; but can a pupil have two lessons on the same weekday across different weeks? Yes, because each weekday repeats W times; the pupil is available on that weekday, so they could have up to W lessons on that weekday (one each week). But our formulation: each day type j has capacity W, representing the total number of slots on that weekday across all weeks. And pupil i can use any number of those slots up to its demand, provided it's within the day's capacity. So the edges from pupil to day should have capacity W as well? Actually, a pupil could be assigned multiple lessons on the same day type, up to the day type's capacity. But we don't need to limit the edge from pupil to day because the day's capacity is enforced by the edge from day to sink. So we can set pupil->day capacity to infinity or to W, it doesn't matter as long as it's at least the possible flow. Using infinite ensures that the min cut condition derived holds as above. However, if we set it to W, the condition might be slightly different because cutting that edge could become part of min cut. But we can set it to a sufficiently large number, say sum ti. The problem's constraints essentially treat that a pupil can take any number of lessons on any of its convenient days, limited only by day availability. So infinite capacity is appropriate.
Thus we need to check the condition: For all subsets A of pupils, sum_{i in A} ti <= W * |N(A)|, where N(A) is the set of weekdays that are convenient for at least one pupil in A.
Now N(A) is a subset of the 7 days. Since there are only 7 days, we can perhaps simplify the condition. The number of subsets of pupils is up to 2^100, too many to enumerate. But we can reduce: The condition must hold for all subsets A. Equivalent to checking that the maximum over subsets of (sum_{i in A} ti - W * |N(A)|) is ≤ 0. This is like a weighted set cover condition. Since |N(A)| can only take values from 1 to 7, we might group pupils by their availability patterns. There are 2^7 - 1 = 127 possible non-empty patterns (subsets of days). Each pupil belongs to one pattern. Then the condition becomes: For any union of some patterns (i.e., a set of pupils with various patterns), the total demand of those pupils must not exceed W times the size of the union of their patterns. The union of patterns is the set of days that appear in at least one of those pupils' patterns. Since patterns are subsets of days, we need to check for every possible union of patterns U (which is a subset of days), that the sum of ti over all pupils whose pattern is a subset of U? Wait, careful: For a given set of pupils A, the union of their patterns is N(A). So the condition is for every A, sum_{i in A} ti <= W * |N(A)|. This is equivalent to: For every subset U of days (non-empty), the sum of ti over pupils whose pattern is a subset of U? Actually, if we fix U, the worst-case A that maximizes sum ti while having N(A) ⊆ U? But we need to bound sum ti for any A such that N(A) = U (or subset). The condition must hold for any A, but we can consider for each U, the maximum possible sum ti over all A with N(A) = U (or ⊆ U) because if it holds for the maximum sum for each U, it holds for all. However, note that for a given U, the sum of ti for any A with N(A) ⊆ U is at most sum of ti of all pupils whose pattern is a subset of U. But is that the maximum? Actually if we take all pupils whose pattern is a subset of U, then N(A) would be the union of those patterns, which is a subset of U but not necessarily equal to U. It could be a proper subset. But the condition must hold for that A as well: sum_{i: pattern ⊆ U} ti <= W * |union of their patterns|, which is ≤ W * |U|. So a necessary condition for all A is that for every U, sum_{i: pattern ⊆ U} ti <= W * |U|? But is it sufficient? Let's test: Suppose there exists some A violating the condition, i.e., sum_{i∈A} ti > W * |N(A)|. Let U = N(A). Then sum_{i∈A} ti > W * |U|. Since A is a subset of pupils with patterns that are subsets of U (because each pupil's pattern is a subset of U by definition of N(A)), it follows that sum_{i∈A} ti <= sum_{i: pattern ⊆ U} ti. So if we have sum_{i: pattern ⊆ U} ti > W * |U|, then the condition fails. Conversely, if for all U, sum_{i: pattern ⊆ U} ti <= W * |U|, then for any A, with U = N(A), sum_{i∈A} ti <= sum_{i: pattern ⊆ U} ti <= W * |U| = W * |N(A)|. So indeed the condition is equivalent to: For every non-empty subset U of days (i.e., U ⊆ {1..7}, U ≠ ∅), the total demand of all pupils whose convenient days are a subset of U is ≤ W * |U|.
But is that correct? We need to be careful: The set A we consider is arbitrary. For a given U, the condition we derive must hold for any A where N(A) ⊆ U. However, the most restrictive is to consider the maximum sum ti over A with N(A) ⊆ U. That maximum sum is indeed the sum over all pupils whose pattern is a subset of U, because adding more pupils can only increase sum ti and cannot cause N(A) to exceed U as long as we include only pupils whose patterns are subsets of U. But if we include all such pupils, N(A) becomes the union of all their patterns, which could be a proper subset of U, say U' ⊆ U. Then the condition would be sum_{i: pattern ⊆ U} ti <= W * |U'|, which is stronger than sum_{i: pattern ⊆ U} ti <= W * |U|? Actually |U'| ≤ |U|, so the inequality sum <= W*|U'| is more restrictive. But if we enforce sum_{i: pattern ⊆ U} ti <= W * |U|, it might not guarantee sum_{i: pattern ⊆ U} ti <= W * |U'|. However, the actual condition for A = all pupils with pattern ⊆ U would be sum <= W * |N(A)|, where N(A) = union of all patterns ⊆ U. That union may be some U' subset of U. So the condition we need to check is actually: For every possible union U' (which is the actual N(A)), we need to check the sum of ti over some subset of pupils whose patterns are contained in U' (maybe all) is ≤ W*|U'|. But our condition that for every U, sum_{i: pattern ⊆ U} ti <= W * |U| is not equivalent, because the union of patterns of all pupils with pattern ⊆ U might be smaller than U, say U' ⊂ U. Then the true condition for that A is sum <= W * |U'|, which is stricter than sum <= W * |U| (since |U'| < |U|). So if sum_{i: pattern ⊆ U} ti <= W * |U|, it does not guarantee sum_{i: pattern ⊆ U} ti <= W * |U'|. However, is it necessary to check A = all pupils with pattern ⊆ U? That A may be an invalid set for the condition because we need to consider only sets A that have exactly N(A) = U'? Actually the condition must hold for all A. So if we take A = all pupils with pattern ⊆ U, we have N(A) = U' (some subset of U). The condition then requires sum <= W * |U'|. If sum <= W * |U| but sum > W * |U'|, then this A would violate the condition, even though our proposed condition on all U (including U itself) holds. So we need to check for every possible union of patterns (i.e., every subset of days that can arise as N(A) for some A), not necessarily every superset. However, the union of patterns of a set of pupils is always some set that is the union of some of the patterns present among pupils. Since there are only 2^7 = 128 possible subsets of days, we can enumerate all possible unions U that can be formed by taking some pupils. But that's still many possible combinations? But we can systematically check the condition: For each subset U of days (non-empty), we need to ensure that for any selection of pupils A such that N(A) = U, sum_{i∈A} ti <= W * |U|. But the maximum sum for a given U is achieved by taking all pupils whose pattern is a subset of U, because adding any pupil whose pattern is not a subset of U would cause N(A) to be larger than U. So to check the condition for a particular U, we need to consider the maximum sum over A with N(A) ⊆ U? But we want the condition for A with N(A) exactly U, but it's okay if N(A) is a subset; the condition must hold for all A, even those with N(A) smaller. So the hardest to satisfy is the A that gives the largest sum while having N(A) = some U (or subset). Actually for a fixed U, the condition that must hold for all A with N(A) ⊆ U (including those with N(A)=U') is that sum_{i∈A} ti <= W * |N(A)|. For each A, the RHS depends on |N(A)|. So it's not simply a condition on U alone; it's a condition that for every possible A, the sum of its ti is bounded by W times the size of its union. So we need to verify that there is no A violating it.
We can approach this by considering the min-cut condition directly: min_{A⊆P} (∑_{i∉A} ti + W * |N(A)|) >= total T. We can compute the min cut value by some algorithm that takes advantage of small number of day types. Since there are only 7 days, we can treat the problem as a kind of knapsack over days. Alternatively, we can solve the max flow using linear programming due to small size.
Given that N up to 100, and only 7 day nodes, we could actually run a max flow algorithm that handles large capacities efficiently. But we need to be careful about augmentations that may involve many steps if we use simple Ford-Fulkerson with BFS that might take many iterations due to reassignments. However, we can use the fact that the graph is small and we can treat the flow as a linear programming problem: maximize flow subject to constraints. Since it's a bipartite flow with 7 right nodes, we can reduce to checking if there is a feasible assignment. We could treat the days as resources, each with capacity W. The pupils have demands ti and can use resources from specific subsets. This is a feasibility of a linear system:
Variables: x_{i,j} >= 0, integer? But flow integrality ensures existence of integer solution if capacities are integers. However, we don't need to enforce integrality because the max flow will produce integer flows; but we can treat as real? For feasibility, if a fractional solution exists, integer solution exists because the constraint matrix is totally unimodular? Actually the matrix for bipartite flow with integer capacities is totally unimodular, so basic feasible solutions are integral. So we can consider linear programming.
We need to decide if there exists x_{i,j} (nonnegative integers) such that sum_j x_{i,j} = ti for each i, and sum_i x_{i,j} <= W for each j, and x_{i,j}=0 if day j not convenient for i.
This is a classic problem: "scheduling with weekly availability". Since there are only 7 resource types, we can check feasibility by greedy? Not exactly.
Given that N can be up to 100, but 7 days, we can treat this as a linear programming problem: maximize nothing, just check constraints. But we need to handle large numbers (ti, W up to 1e10). However, we can solve the linear program using simplex? Not necessary.
Alternatively, we can check the condition derived from min-cut: For every subset A of pupils, sum_{i∈A} ti <= W * |N(A)|. This is equivalent to the max flow condition. But we need an efficient way to check this condition without enumerating all 2^N subsets. Since the RHS depends only on the union of patterns, we can apply a technique similar to checking all subsets of days because the condition only cares about which patterns are included? Actually, for a given set of pupils A, the LHS sum depends on which pupils are selected; the RHS is W * size of union of their patterns. So the worst violation would come from a subset A that maximizes sum_{i∈A} ti - W * |N(A)|. This is like a submodular function? Actually f(A) = W * |N(A)| - sum_{i∈A} ti is a function that is supermodular? Not sure.
But we can transform the problem: Since there are only 7 days, the union N(A) can be any subset of days. For each subset U of days, the maximum sum of ti among subsets A with N(A) = U (or ⊆ U) is of interest. However, to check the condition, it's sufficient to check for every U the condition: sum_{i: pattern ⊆ U} ti <= W * |U|? Not exactly as argued, because for U that is not the union of all patterns of pupils with pattern ⊆ U, the condition may be too weak. But perhaps it is sufficient: Consider any A. Let U = N(A). Then all pupils in A have pattern ⊆ U, so they are included in the set S_U = {i | pattern_i ⊆ U}. Therefore sum_{i∈A} ti <= sum_{i∈S_U} ti. So if we ensure that for every U, sum_{i∈S_U} ti <= W * |U|, then for A we have sum_{i∈A} ti <= sum_{i∈S_U} ti <= W * |U| = W * |N(A)|. But wait, the RHS for A is W * |N(A)| = W * |U|. So it holds. However, note that if we take S_U, the union of its patterns is not necessarily U; it is the union over all i with pattern ⊆ U, which might be a subset of U, say U' ⊆ U. But the condition we impose is sum_{i∈S_U} ti <= W * |U|, not W * |U'|. This is weaker than what is actually required for A = S_U: For A = S_U, we would need sum_{i∈S_U} ti <= W * |U'|, because N(S_U) = U' (which might be smaller than U). Our imposed condition sum_{i∈S_U} ti <= W * |U| does not guarantee that sum_{i∈S_U} ti <= W * |U'|. However, is it possible that sum_{i∈S_U} ti > W * |U'| while still sum_{i∈S_U} ti <= W * |U|? Yes, if |U'| < |U|. Then A = S_U would violate the actual condition (since N(A)=U') but our check on U (where we used |U|) would not catch it because we didn't check the condition for the actual union U'. But we are checking for all U, including U'. So we also check for U' the condition sum_{i∈S_{U'}} ti <= W * |U'|. But note: S_{U'} is the set of pupils with pattern ⊆ U'. Since U' is smaller than U, S_{U'} might be a subset of S_U. In the scenario where sum_{i∈S_U} ti > W * |U'|, could it be that sum_{i∈S_{U'}} ti <= W * |U'|? Possibly, because S_{U'} is smaller. So the violation by A = S_U (where some pupils have patterns that are subsets of U but not subsets of U') might not be caught by checking S_{U'} because those pupils with patterns that are not subsets of U' are not included in S_{U'}. But we need to catch that violation. So we need to check for each U (the actual union of patterns of the considered A), not just supersets. In the violation case, A = S_U has union U' (since patterns of S_U are all subsets of U, but they might not cover all of U; they collectively cover U'). So the violation is that sum_{i∈S_U} ti > W * |U'|. This would be caught when we check condition for U' (the actual union) because we need to consider the set of all pupils whose patterns are subsets of U'. But S_U is not necessarily a subset of S_{U'}? Actually S_{U'} consists of pupils with pattern ⊆ U'. Since some pupils in S_U have pattern ⊆ U but possibly not ⊆ U', they are not in S_{U'}. So S_{U'} does not include them. Therefore the sum over S_{U'} might be smaller. To catch the violation, we need to check for the specific set A = S_U, but A is not necessarily equal to S_{U'} nor S_{any}. However, we can test condition by checking all subsets of days U and also consider the maximum sum over pupils whose patterns are subsets of U but with the additional constraint that the union of their patterns is exactly some subset? Actually, the condition must hold for every A. So we could try to compute the maximum of (sum_{i∈A} ti - W * |N(A)|) over all A, and see if it's ≤ 0. Since the number of days is tiny, we can use a DP over subsets of days: For each subset of days U, we can compute the maximum sum of ti we can achieve with exactly union = U? Or we can compute the maximum sum we can achieve where the union is a subset of U, then the penalty is W * |U|? But the actual penalty depends on the union.
Another approach: Since N <= 100, we could implement a max flow algorithm that handles large capacities by using, for example, Dinic's algorithm with scaling. The graph is small, so Dinic's algorithm will be fast. The number of augmentations in Dinic is O(E * sqrt(V)) maybe but with scaling it's O(E * log C). But typical Dinic on unit capacities is O(min(V^{2/3}, sqrt(E)) * E) but here capacities are huge but graph is tiny. We can implement Dinic and it should run quickly even if we do BFS/DFS with 64-bit flows. Because the number of levels and blocking flows will be small. Let's analyze: Dinic's algorithm on a graph with V ~108, E ~ Nci + N + 7 ≈ up to 707 plus source/sink edges. Each BFS builds level graph, then DFS sends flows until blocking. The number of augmenting phases (BFS) is O(V) in worst case? Actually Dinic's time complexity is O(E * V^2) in general, but that's for integer capacities with each augmenting phase saturating at least one edge. Since V is about 108, that's 108^2=11664, times E~700 gives ~8e6, which is fine. But that's worst-case for arbitrary capacities. However, with huge capacities, each DFS may send a lot of flow along many paths, but the number of augmentations per phase is bounded by the number of edges? In Dinic, each phase finds blocking flow, which may consist of many augmenting paths. The total number of phases is O(V) because each phase increases the distance from source to sink. With V ~108, that's fine. The total work per phase is O(E) times some factor. Actually, a blocking flow can be found by DFS that visits each edge at most once per phase if we use current edge pointers. So overall complexity is O(V * E) ~ 108700 = 75600, which is trivial. However, the capacities are huge, but we are dealing with integers; the algorithm will push flow along paths, but the amount pushed along each path in a DFS call is determined by the minimum residual capacity along the path. This could be large, but we just store as 64-bit. The number of DFS calls could be up to O(E) per phase, but each call pushes flow and may saturate an edge. Since there are at most E edges that can be saturated, and each saturation removes an edge from the residual graph for the rest of the phase? Actually, in Dinic, each edge may become saturated and then not used in that phase. But across phases, edges can become unsaturated due to reverse flow? No, once saturated, the forward edge is no longer usable, but the reverse edge may be used in later phases. So each phase can saturate some edges. Over all phases, each edge can be saturated at most once? Actually, an edge could be saturated, then later have flow pushed back via its reverse edge, making it unsaturated again, allowing it to be saturated again in a later phase. So it's not strictly bounded by O(E). However, Dinic's algorithm on a general graph has time O(V^2 E) worst-case, but that is for arbitrary capacities and can be achieved with adversarial graphs (like dense). For our tiny graph, it's definitely fine. Even O(V^2 E) with V=108, E=700 gives 108^2*700 ≈ 8 million, still fine. The bigger concern is the number of phases and DFS calls might be larger due to large capacities, but I think it's safe.
Given that we are programming for an online judge, we can implement Dinic with 64-bit integers. However, we must be careful about recursion depth for DFS; we can implement iterative stack or recursion with safe depth (V is small). We'll implement standard Dinic.
But wait: The problem's input includes multiple data sets, up to unknown number, but likely not huge. With Dinic, it's fine.
Nevertheless, maybe we can find an even simpler condition: Since there are only 7 days, we can also solve the max flow by reducing to a linear programming problem that can be solved by checking a small number of constraints. For instance, we can treat the days as resources, and each pupil has a pattern. The condition we derived is necessary and sufficient: For all subsets A, sum_{i in A} ti <= W * |N(A)|. This is a system of inequalities known as the "Hoffman circulation conditions" for bipartite b-matching. Since there are only 7 days, we can rewrite this condition as: For all subsets of days S (non-empty), the total demand of pupils that can only be served by days in S (i.e., those whose convenient set is a subset of S) must not exceed W * |S|. But is that sufficient? Let's examine: In the bipartite matching literature, for a bipartite graph with left nodes L (pupils) having demand a_i, and right nodes R (days) having capacities b_j, a necessary and sufficient condition for existence of a feasible flow is the Hall-type condition: For all subsets X ⊆ L, sum_{i∈X} a_i ≤ sum_{j∈N(X)} b_j, where b_j = W for all j. That's exactly our condition: sum_{i∈X} ti ≤ W * |N(X)|. That's the same as before, with X = A. So we need to check that for every subset X of pupils. This is exactly the Hall condition for bipartite b-matching. So the condition is indeed that. But we need to check for all subsets X, not just those defined by patterns.
Now, we can attempt to simplify by considering that the right side capacities are all equal W. The condition is: For any subset X of pupils, |N(X)| >= ceil(sum_{i∈X} ti / W). Actually sum ti may exceed W * |N(X)|. So we need to check that sum_{i∈X} ti <= W * |N(X)|.
Now, because there are only 7 days, we can transform this into checking a set of inequalities that involve grouping pupils by their neighborhood sets. This is reminiscent of checking the condition for all subsets of left nodes can be reduced to checking only those subsets that are "closed" under neighborhood? Actually, the worst-case violation would be for X that is "tight" in some sense. There is known technique: For bipartite graphs with small right side, the condition can be checked by considering all subsets of right nodes and computing the total demand of left nodes that have their neighborhood contained in that subset. This is because if we take any X, let T = N(X). Then all pupils in X have neighborhoods subsets of T. So sum_{i∈X} ti <= sum_{i: N(i) ⊆ T} ti. The condition that must hold for X is sum_{i∈X} ti <= W * |T|. But to guarantee for all X, it is sufficient and necessary to check that for all subsets T of days, sum_{i: N(i) ⊆ T} ti <= W * |T|. Is that true? Let's test. Suppose there is a violation: some X with T = N(X) such that sum_{i∈X} ti > W * |T|. Then consider the set of all pupils i with N(i) ⊆ T; call this set Y. Since X ⊆ Y, sum_{i∈Y} ti >= sum_{i∈X} ti > W * |T|. So then the condition for T fails: sum_{i: N(i) ⊆ T} ti > W * |T|. Conversely, if for every T, sum_{i: N(i) ⊆ T} ti <= W * |T|, then for any X, with T = N(X), we have sum_{i∈X} ti <= sum_{i: N(i) ⊆ T} ti <= W * |T|. So indeed the condition is equivalent to: For every non-empty subset T of days, the total demand of pupils whose available days are a subset of T must be ≤ W * |T|. And we also need to consider possibly the total demand across all pupils ≤ 7W, but that's covered by T = set of all days.
Thus our earlier concern about union being smaller is resolved: We must check for each T (any subset of days), not only those that are unions of some pupils' patterns. Because if T is a superset, the condition may be less restrictive, but we need to check for all T. However, the condition we derived (for all T, sum_{i: N(i) ⊆ T} ti <= W * |T|) is indeed necessary and sufficient. Let's prove formally:
Necessity: For a given T, consider X = {i | N(i) ⊆ T}. Then N(X) ⊆ T (since each i's neighbors are within T, the union is also within T). Actually N(X) may be a subset of T, but certainly N(X) ⊆ T. The Hall condition for X requires sum_{i∈X} ti ≤ W * |N(X)| ≤ W * |T|. So sum_{i∈X} ti ≤ W * |T|. So necessary.
Sufficiency: For any arbitrary X, let T = N(X). Then for each i in X, N(i) ⊆ T (since N(i) is a subset of T by definition of union? Actually if i ∈ X, then N(i) ⊆ N(X) = T, so indeed N(i) ⊆ T. Therefore X ⊆ {i | N(i) ⊆ T}. So sum_{i∈X} ti ≤ sum_{i: N(i) ⊆ T} ti ≤ W * |T| (by assumption). So Hall condition holds.
Thus the condition reduces to checking for each non-empty subset T of the 7 days: total demand of pupils whose available days are a subset of T ≤ W * |T|.
This is very simple! We just need to compute for each of the 127 non-empty subsets T, the sum of ti over pupils whose set of convenient days is a subset of T. Then compare to W * |T|. If all hold, answer Yes; else No.
We also need to consider that the total number of lessons must be ≤ total days 7W, but that's covered by T = all 7 days.
Thus we can solve each test case in O(N * 2^7) = O(128*100) = 12,800 operations per case, which is trivial.
We must be careful: The pupils have ci convenient days, listed as names. We need to map day names to indices 0-6. Also note: ti and W can be up to 1e10, so sum ti could be up to 100 * 1e10 = 1e12, which fits in 64-bit (signed 2^63 ≈9e18). So we can use 64-bit (long long in C++). For the condition, we compute sum for each T and compare to W * |T|. If any violation, answer No; else Yes.
Edge Cases: N could be 0? Input says 0 < N, so N positive. W positive. The list of days: The days are given as strings like "Monday", "Tuesday", etc. We need to parse them. There are exactly ci strings on that line. They could be separated by spaces. We need to read the line and split.
We also need to handle multiple data sets until "0 0". Input example shows two data sets. Output "Yes" and "No".
Now confirm with the example:
First data set: N=2, W=2. Pupil1: t1=6, c1=3, days: Monday Tuesday Wednesday. Pupil2: t2=8, c2=4, days: Thursday Friday Saturday Sunday. We need to check subsets T.
Pupil1 pattern = {Mon,Tue,Wed} (indices 1,2,3 maybe). Pupil2 pattern = {Thu,Fri,Sat,Sun} (4,5,6,0). For T = {Mon,Tue,Wed} (size 3): pupils with pattern subset of T: only pupil1 (since pupil2 pattern includes Thu etc not subset). sum=6, W*|T|=23=6 OK. For T = {Thu,Fri,Sat,Sun} size 4: sum=8, W4=8 OK. For T = all 7 days size 7: sum=6+8=14, W7=14 OK. For any other T? Example T = {Mon,Tue,Wed,Thu} size 4: pupils with pattern subset? pupil1 pattern subset? Yes, because {Mon,Tue,Wed} ⊆ T. pupil2 pattern? {Thu,Fri,Sat,Sun} not subset of T (since Fri, Sat, Sun not in T). So sum=6, W4=8 OK. For T = {Mon,Tue,Wed,Thu,Fri} size5: same sum=6, 2*5=10 OK. T = {Mon} size1: pupil1 pattern? {Mon,Tue,Wed} not subset of {Mon}, so only pupils with pattern subset? none? sum=0 ≤ 2 OK. So all hold, Yes.
Second data set: N=2, W=2. t1=7, pattern Mon Tue Wed. t2=9, pattern Thu Fri Sat Sun. Check T = {Mon,Tue,Wed} size3: sum=7, W*3=6 → 7 > 6, violation => No. So output No. Matches.
Thus the condition works.
Now we must be careful: The condition must hold for all subsets T, including the empty set? But empty set T would have size 0, and pupils with pattern subset of empty set are only those with no convenient days? But each pupil has ci>0, so pattern non-empty, so sum over empty T is 0, and W*0=0, holds trivially. So we can ignore empty.
Thus algorithm:
For each dataset:
- Read N, W.
- Initialize an array pupil_pattern[N] as bitmask of 7 bits (0..6). Also store t_i.
- For i in 0..N-1:
- read line: two integers ti, ci.
- read next line: list of ci day names. Parse each name, map to index (e.g., Sunday=0, Monday=1, Tuesday=2, Wednesday=3, Thursday=4, Friday=5, Saturday=6). Build bitmask.
- Then for each non-empty subset T of {0..6} (i.e., mask from 1 to (1<<7)-1):
- Compute total_demand = sum_{i: (pupil_mask[i] & ~T) == 0} ti. That is, pupil_mask[i] is subset of T.
- if total_demand > W * popcount(T) *? Actually W * |T|, where |T| = number of bits set in T.
- if condition violated, output "No" and skip remaining subsets.
- If all passed, output "Yes".
Complexity fine.
Potential pitfalls: Input lines may have extra spaces? The list of days is on a single line, separated by spaces. We can read the whole line and split. Use getline after reading the previous integers. But careful: The input format: For each pupil, there are two lines. The first line contains two integers ti ci. The second line is the list of ci day names. The day names are separated by spaces, and there are exactly ci names. They are proper English day names with capital first letter, rest lowercase? Probably as given. We'll map case-sensitive.
Reading: Typically using cin >> ti >> ci; then cin.ignore() to consume newline, then getline(cin, line) to read the days line. But need to handle cases where there may be trailing spaces. Alternatively, we can just read each day name using cin, since they are separated by spaces, and after reading ci, we can read ci strings. That would be simpler: after reading ti and ci, then loop ci times reading a string day_name. That will automatically skip whitespace and newline. The only issue is that after reading ci, the next token in the input is the first day name; newline does not matter. So we can do:
cin >> ti >> ci;
for (int j=0; j<ci; ++j) { string day; cin >> day; ... }
That works because cin >> will skip whitespace (including newlines). So it's safe. Then we don't need getline. Yes, that's easiest.
But careful: After reading the last day name for a pupil, the next token will be the next ti for next pupil or 0 0. So reading with >> is fine.
Thus implement.
Now, about the bitmask: We'll define mapping: Sunday -> 1<<0, Monday -> 1<<1, Tuesday -> 1<<2, Wednesday -> 1<<3, Thursday -> 1<<4, Friday -> 1<<5, Saturday -> 1<<6 (or any order as long as consistent). The problem statement lists days as "Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday" (in that order). So we can map accordingly. In input example they used Monday, Tuesday,... etc. So we need to recognize all seven names.
We'll create a map<string, int> dayToBit = {
{"Sunday", 1<<0},
{"Monday", 1<<1},
{"Tuesday", 1<<2},
{"Wednesday", 1<<3},
{"Thursday", 1<<4},
{"Friday", 1<<5},
{"Saturday", 1<<6}
};
But note: The names might appear exactly as these, but the problem statement says "Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday". So we'll use these strings.
Now, we must ensure to use 64-bit integers for sums. Use long long (or unsigned long long). Compare: if (total_demand > W * (long long)popcnt) — but W up to 1e10, popcnt up to 7, product up to 7e10, fits in 64-bit.
Potential overflow? 1e10 * 7 = 7e10, well within 2^63-1 ~9e18. So safe.
Now, we need to consider possibility that W is huge (1e10) and ti also huge, but the sum for a T can be up to N*ti = 1e12, still within 64-bit. So all good.
Now, is the condition also sufficient? Yes, as proven by Hall's theorem for bipartite graphs with capacities on the right. Actually, Hall's theorem for bipartite b-matching: For a bipartite graph with left vertices having demands a_i and right vertices having capacities b_j, there exists a feasible flow if and only if for every subset X of left vertices, sum_{i∈X} a_i ≤ sum_{j∈N(X)} b_j. That's exactly our condition. And we have transformed it to checking subsets of right vertices as described. So it's both necessary and sufficient.
Thus the problem is solved.
Now, we need to handle multiple data sets until 0 0.
We'll implement in C++.
Pseudo:
#include
#include
#include
#include
#include
using namespace std;
map<string, int> dayMask = {
{"Sunday", 1},
{"Monday", 2},
{"Tuesday", 4},
{"Wednesday", 8},
{"Thursday", 16},
{"Friday", 32},
{"Saturday", 64}
};
int main() {
int N;
long long W;
while (cin >> N >> W) {
if (N == 0 && W == 0) break;
vector t(N);
vector pattern(N);
for (int i = 0; i < N; ++i) {
int ci;
cin >> t[i] >> ci;
int mask = 0;
for (int j = 0; j < ci; ++j) {
string day;
cin >> day;
mask |= dayMask[day];
}
pattern[i] = mask;
}
bool possible = true;
// iterate over all non-empty subsets of days (1..127)
for (int mask = 1; mask < (1 << 7); ++mask) {
long long sum_t = 0;
for (int i = 0; i < N; ++i) {
if ((pattern[i] & ~mask) == 0) { // pattern[i] subset of mask
sum_t += t[i];
}
}
int days_cnt = __builtin_popcount(mask); // GCC builtin; if not, compute manually
if (sum_t > W * days_cnt) {
possible = false;
break;
}
}
cout << (possible ? "Yes" : "No") << endl;
}
return 0;
}
Note: __builtin_popcount works for int. mask is up to 127, fine.
Alternative: If using non-GCC, implement a popcount function.
Edge Cases: N up to 100, W up to 1e10, sum_t could overflow 32-bit, so use long long. Also days_cnt is small.
Double-check: The condition requires for all subsets T of days. We check for all mask. Good.
Now test with the given example. Should produce Yes and No.
We should also test some border cases: If a pupil has all 7 days, pattern = 127. Then for any T that is not all days, that pupil will not be subset of T unless T is all days (since pattern not subset). So only contributes to T=all days. That's fine.
What if there is a pupil with a pattern that is exactly one day, say Monday only. Then for any T that contains Monday, that pupil contributes. The condition for T={Monday} would require t_i <= W, i.e., one pupil's demand cannot exceed W (since only one day available, and that day appears W times). That matches intuition: if a pupil only free on Mondays, they can have at most W lessons. So condition checks that.
Thus algorithm seems correct.
Now, are there any tricky aspects? The input may have multiple spaces or lines? But using >> should handle.
Potential pitfalls: The list of days might be given as "MondayTuesday" without spaces? No, it's space-separated.
Thus solution is straightforward.
Now we need to output exactly "Yes" or "No" with capital Y and N as in example.
Now, I'll provide final answer in the required format. The solution is O(2^7 * N) per test case.
We could also precompute for each mask the sum quickly using DP? But N small.
Thus final answer.
But wait, is the Hall condition exactly sum_{i∈X} ti <= W * |N(X)|? Yes. However, note that W is the number of weeks, so each day type has exactly W slots. But is there any nuance about the fact that each day slot can only host one lesson, but the same pupil could potentially have two lessons on the same weekday across different weeks? Yes, that's allowed. So our modeling is correct.
Thus answer is to check the condition.
Now, confirm with a more complex scenario: Suppose there are two pupils, both available on Monday and Tuesday. W=1. t1=1, t2=1. Then total demand 2, total slots 2 (Monday and Tuesday). Check T={Monday,Tuesday}: sum of pupils with pattern subset = both (since both patterns are {Mon,Tue}) sum=2, W*2=2 OK. T={Monday}: pupils with pattern subset? Both patterns are {Mon,Tue} not subset of {Monday} (since includes Tuesday), so sum=0 <=1. T={Tuesday} similar. So condition holds. Feasibility: We can assign one lesson on Monday to pupil1, Tuesday to pupil2. Yes.
Suppose t1=2, t2=0? Actually t2=0 not allowed (ti>0). t1=2, t2=0? No. So test t1=2, t2=0 impossible.
Suppose three pupils, patterns: A: {Mon}, B:{Mon}, C:{Tue}. W=1. tA=1, tB=1, tC=1. Total slots: Mon and Tue (2 slots) but demand 3 >2, so impossible. Check T={Mon,Tue}: sum of pupils with pattern subset = all three => sum=3 > 2 => violation. So No.
Now test scenario where pattern subsets condition might be subtle: Suppose two pupils: P1 pattern {Mon, Tue}, demand 3; P2 pattern {Tue, Wed}, demand 3; W=2. Total slots per day type =2, total slots = 6, total demand =6. Is it feasible? Let's check condition:
All days {Mon,Tue,Wed} size 3: sum=6 <= 2*3=6 OK.
Check T={Mon,Tue}: pupils with pattern subset? P1 pattern {Mon,Tue} subset, yes. P2 pattern {Tue,Wed} not subset because Wed not in T. So sum=3 <=2*2=4 OK.
T={Tue,Wed}: P2 subset yes, P1 not (Mon not in T). sum=3 <=4 OK.
T={Mon,Wed}: P1? pattern {Mon,Tue} not subset (Tue missing). P2? {Tue,Wed} not subset (Tue missing). So sum=0 <=4 OK.
T={Mon}: P1? not subset, P2? not, sum=0 <=2 OK.
T={Tue}: both patterns not subset (P1 has Mon, P2 has Wed) -> sum=0 <=2 OK.
T={Wed}: sum=0 <=2 OK.
T={Mon,Tue,Wed} already. So condition holds => predicted Yes.
Now manually try to schedule: We have 2 Mondays, 2 Tuesdays, 2 Wednesdays. Need to assign 3 lessons to P1, 3 to P2. Each lesson occupies a day slot.
We can try: P1 can take up to 2 on Monday, up to 2 on Tuesday; P2 up to 2 on Tuesday, up to 2 on Wednesday. Since total Tuesday capacity is 2, we need to split between them. Let's see if we can assign: Let x1M, x1T, x2T, x2W be numbers. Constraints: x1M + x1T = 3, x2T + x2W = 3, x1M ≤ 2, x1T ≤ 2, x2T ≤ 2, x2W ≤ 2, and also x1T + x2T ≤ 2 (Tuesday capacity). And x1M ≤2, x2W≤2. So we need to solve: From first eq, x1M = 3 - x1T. Since x1M ≤ 2 => 3 - x1T ≤ 2 => x1T ≥ 1. Also x1M ≥ 0 => x1T ≤ 3. And x1T ≤ 2, so x1T can be 1 or 2. Similarly, x2W = 3 - x2T, with x2W ≤2 => 3 - x2T ≤2 => x2T ≥1, and x2T ≤2. So x2T ∈ {1,2}. Then Tuesday capacity: x1T + x2T ≤ 2. With both at least 1, the only possibility is x1T=1, x2T=1, sum=2. Then x1M = 2, x2W = 2. So schedule: P1: 2 on Monday, 1 on Tuesday; P2: 1 on Tuesday, 2 on Wednesday. Works. So Yes.
Now consider a scenario that might break the simplified condition but satisfy the Hall? Actually the simplified condition is equivalent. So fine.
Thus solution stands.
Now, what about the possibility of having more than 7 days? No, weekdays are fixed 7.
Now, we should consider if there is any catch about the days being "convenient for each pupil" meaning that the pupil is available on those days, but perhaps a pupil can only take at most one lesson per week? No, the problem statement: "You can give just one lesson only to one pupil on each day." That means each day, only one lesson total. There's no explicit limit per pupil per week, only per day. So our model is correct.
One more nuance: The weeks are contiguous, but the specific dates don't matter; we only care about weekdays. So indeed we have W copies of each weekday.
Thus answer is to use Hall's condition.
Now implement.
But we need to be careful: The input may contain trailing spaces, but our reading method is robust.
Potential pitfalls: The day names might be given in different cases? The problem statement likely uses proper capital first letter. We'll assume exactly as listed.
Now code.
Testing with sample input:
2 2
6 3
Monday Tuesday Wednesday
8 4
Thursday Friday Saturday Sunday
2 2
7 3
Monday Tuesday Wednesday
9 4
Thursday Friday Saturday Sunday
0 0
Our code would read:
First: N=2,W=2. For pupil1: t=6, ci=3, then read "Monday","Tuesday","Wednesday". mask = Monday|Tuesday|Wednesday = bits for Mon(2),Tue(4),Wed(8) = 14? Actually Monday=2, Tuesday=4, Wednesday=8 sum=14. For pupil2: t=8, ci=4, read Thursday, Friday, Saturday, Sunday: Thursday=16, Friday=32, Saturday=64, Sunday=1 sum=113. Then iterate masks. Condition holds.
Second: similar, but t1=7, t2=9, so T={Mon,Tue,Wed} sum=7 >6 => No.
Output Yes No? Actually the sample output shows "Yes" then "No", each on separate lines. So we output accordingly.
Now, ensure we handle large N up to 100, and many data sets. The inner loop 127*100 = 12700 per dataset, trivial.
Now, we need to be mindful of the fact that W and ti can be up to 10^10, but product W*days_cnt could be up to 7e10, which fits in long long. However, using int for days_cnt is fine; we cast to long long for multiplication.
Now, edge case: If total demand > 7*W, then T=all days will fail, so output No. So fine.
Thus solution is correct.
Now, we can also note that the condition is essentially checking a set of linear constraints. Could there be any missing constraints about the distribution of lessons across weeks? For example, could there be a conflict where two lessons need to be scheduled on the same weekday across different weeks but the same pupil can take at most one lesson per day, but that's okay because weeks are separate days. So no further constraints.
Thus final answer.
But I want to double-check the transformation from Hall condition to "for all subsets T of days" is correct. The standard reduction: For bipartite graph with left nodes L (demands a_i) and right nodes R (capacities b_j), a necessary and sufficient condition for existence of a feasible flow is that for every subset X of L, a(X) ≤ b(N(X)). This is known as the Gale's theorem (or Hall's for b-matching). Now, we claim that checking for all subsets T of R, a( { i : N(i) ⊆ T } ) ≤ b(T) is equivalent. Proof: If condition holds for all T, then for any X, let T = N(X). Since for each i in X, N(i) ⊆ T, we have X ⊆ { i : N(i) ⊆ T }, thus a(X) ≤ a({i: N(i) ⊆ T}) ≤ b(T) = b(N(X)). Conversely, if the Hall condition holds, then for any T, let X = { i : N(i) ⊆ T }. Then N(X) ⊆ T, so b(N(X)) ≤ b(T). By Hall, a(X) ≤ b(N(X)) ≤ b(T). So it holds. So indeed equivalent. Good.
In our case, b(T) = W * |T| because each day j in T contributes capacity W, and we consider sum over j in T of b_j = W * |T|. But careful: b(T) should be the sum of capacities of nodes in T, i.e., sum_{j in T} b_j, which is W * |T|. So condition for T: sum_{i: N(i) ⊆ T} ti ≤ W * |T|.
Thus correct.
Now, note: This condition must also hold for T = empty set? Then sum over i with N(i) ⊆ empty = only pupils with no neighbors (which there are none because ci>0), so sum=0, and b(empty)=0, so holds trivially. So we can skip.
Thus solution final.
Now, one more nuance: The problem statement says "You can give just one lesson only to one pupil on each day." That means each day can have at most one lesson, but also maybe you can't give more than one lesson to the same pupil on the same day, but that's already implied. So our model is correct.
Now, we should also consider if weeks are consecutive and there is any issue about overlapping? No.
Thus implement.
Now, I'll write final answer code in C++.
But we must be careful about using __builtin_popcount which counts bits in an int. On some compilers (like MSVC), it's not available. Since we are likely in a competitive programming environment (like ICPC, maybe GCC), we can use __builtin_popcount. Alternatively, we can precompute popcount for 0..127. Or use bitset<7>(mask).count(). But that's slower but okay. I'll use __builtin_popcount.
Alternatively, we can write a simple loop to count bits.
Now, note: The bitmask for days: We need to assign bits consistently. Let's define:
const int days = 7;
map<string, int> mp = {
{"Sunday", 0},
{"Monday", 1},
{"Tuesday", 2},
{"Wednesday", 3},
{"Thursday", 4},
{"Friday", 5},
{"Saturday", 6}
};
But then pattern[i] = 1 << index. That's fine.
Now, code.
Potential edge: Input may have extra spaces or blank lines? Usually not.
Now, implement.
Testing: We'll also test a few extra cases:
Case 1: N=1, W=5, t=35, ci=7 all days. Then total demand 35, total slots 75=35. Condition: For any T, sum of pupils with pattern ⊆ T: For T=all days, sum=35, W7=35 OK; For T smaller, pattern of pupil is all days, not subset unless T=all days, so sum=0 for others OK. So Yes. Indeed we can give one lesson each day for 5 weeks =35 lessons, all to that pupil.
Case 2: N=1, W=5, t=36, ci=7. Then total demand 36 >35, so T=all days fails: 36 > 35 => No. So output No.
Case 3: N=2, both have pattern {Monday}, W=1, t1=1, t2=1. Total demand 2, Monday capacity 1 => No. Condition: T={Monday} sum=2 > 1*1 => No.
Case 4: N=2, patterns: P1 {Mon,Tue}, t1=3; P2 {Tue,Wed}, t2=3; W=2 (as before) -> Yes.
Case 5: N=3, patterns: P1 {Mon}, t1=2; P2 {Tue}, t2=2; P3 {Wed}, t3=2; W=2. Total demand 6, total slots 6. Condition: T={Mon} sum=2 ≤4? Actually W*1=2, sum=2 OK. T={Mon,Tue} sum=4 ≤4 OK. T={Mon,Tue,Wed} sum=6 ≤6 OK. So Yes. Indeed each day appears twice, each pupil can take both slots on their only day? But each pupil can take at most one lesson per day, but there are two Mondays (weeks), so P1 can take both Monday slots? Actually each Monday in each week is a separate day, so yes, they can take 2 lessons on Mondays (one each week). So assign: P1 gets both Mondays, P2 both Tuesdays, P3 both Wednesdays. Works.
Now, more complex: Suppose patterns overlap partially, demands high. Should work.
Thus final answer.
Now, note: The problem statement says "The input consists of multiple data sets." So we need to process until 0 0.
Thus final code.
One more: Might need to use faster I/O if many data sets? But N ≤100, and maybe many data sets but input size small. Using cin/cout is fine. However, because we are reading strings with cin, might be slower but okay.
Now, confirm that reading with >> will handle lines correctly. Suppose we have a line with day names, each separated by spaces. After reading ci day names, the next cin >> N >> W will read the next dataset's first numbers. There may be newline after the last day name, but >> skips whitespace, so fine.
Potential tricky: The input ends with "0 0", and there might be no extra newline.
Now, let's produce final answer.
But before finalizing, let's double-check the transformation with an example that might violate the condition but still satisfy the T condition? Since we proved equivalence, no counterexample. However, ensure we didn't miss the fact that b(T) = sum_{j∈T} W, but the Hall condition is sum_{i∈X} ti ≤ sum_{j∈N(X)} b_j = W * |N(X)|. In our reduction we used condition sum_{i: N(i) ⊆ T} ti ≤ W * |T|. That's correct.
But is there any nuance about "N(i) ⊆ T"? In the Hall condition, we need to consider all subsets X of pupils. For a given T, the set X_T = {i: N(i) ⊆ T} is the largest set of pupils whose neighborhoods are contained in T. If Hall condition holds, then sum over X_T ≤ W * |N(X_T)|. But N(X_T) might be a subset of T, not necessarily equal to T. So the condition we check (sum over X_T ≤ W * |T|) is weaker than the Hall condition applied to X_T (which would require ≤ W * |N(X_T)|). Since |N(X_T)| ≤ |T|, the Hall condition for X_T is stronger. So if we only check sum over X_T ≤ W * |T|, we might accept cases where sum over X_T > W * |N(X_T)| but still ≤ W * |T|, which would violate Hall. But earlier we argued that the condition for all T (sum_{i: N(i) ⊆ T} ti ≤ W * |T|) is equivalent to Hall condition. But here we see a discrepancy: Suppose there exists X_T such that sum_{i∈X_T} ti > W * |N(X_T)| but sum_{i∈X_T} ti ≤ W * |T|. Then Hall condition fails because for X_T, we have sum > W * |N(X_T)|. But our condition for T would hold because sum ≤ W * |T|, but we need to check if such a scenario can happen. Let's test with a concrete example.
We need a set of pupils X such that N(X) is a proper subset of T, and sum over X > W * |N(X)| but sum over X ≤ W * |T|. Since T is a superset of N(X), it's possible that sum is between W * (|N(X)|+1) and W * |T|. Then our condition for T (which is any superset) would be satisfied, but the Hall condition for X would be violated. However, we are supposed to check condition for all T, including the specific T = N(X)? Wait, if we take T = N(X), then the set {i: N(i) ⊆ N(X)} includes X, but could also include other pupils whose neighborhoods are subsets of N(X). So sum_{i: N(i) ⊆ N(X)} ti is at least sum_{i∈X} ti. So if sum_{i∈X} ti > W * |N(X)|, then sum_{i: N(i) ⊆ N(X)} ti ≥ sum_{i∈X} ti > W * |N(X)|, which would violate the condition for T = N(X). Thus our check would catch it because we consider T = N(X). So the scenario where Hall condition fails will be caught when we examine T = N(X). Because N(X) is a subset of days, and we check all subsets T. So our condition is equivalent.
But what if N(X) is not exactly equal to any T that we can choose? But we are checking all subsets T of days. N(X) is a subset of days, so it is one of the T we check. Indeed we iterate over all non-empty subsets of days. So if there is a violating X, then T = N(X) will cause failure, because the sum over pupils with pattern ⊆ N(X) is at least sum_{i∈X} ti, which exceeds W * |N(X)|. So the condition for T = N(X) fails.
Thus our approach is correct.
Now, we need to be careful: The set X_T = {i: N(i) ⊆ T} may be larger than X, but that's fine; if sum_{i∈X_T} ti > W * |T|, then condition fails. If it's ≤, then for X subset of X_T, sum_{i∈X} ti ≤ sum_{i∈X_T} ti ≤ W * |T|, but that does not guarantee sum_{i∈X} ti ≤ W * |N(X)| if N(X) is smaller than T. However, if condition holds for all T, then in particular for T = N(X), we have sum_{i: N(i) ⊆ N(X)} ti ≤ W * |N(X)|. Since X ⊆ {i: N(i) ⊆ N(X)} (because for i in X, N(i) ⊆ N(X)), we get sum_{i∈X} ti ≤ W * |N(X)|. So Hall holds. So indeed checking all T ensures Hall condition.
Thus equivalence holds.
Thus our solution is correct.
Now, we should also consider if we need to check the condition for T = empty set? Not needed.
Thus implement.
Now, final code.
One more: We need to use 64-bit for sum_t and for multiplication. Use long long.
Now, provide final answer.
But before final, let's double-check the mapping of days to bits. The order doesn't matter as long as consistent. However, we must ensure that we treat "Sunday" correctly: In some cultures Sunday is considered first day, but the problem lists Sunday first, but it's fine.
Now, code:
#include
#include
#include
#include
#include
using namespace std;
int main() {
map<string, int> day_to_bit;
day_to_bit["Sunday"] = 1 << 0;
day_to_bit["Monday"] = 1 << 1;
day_to_bit["Tuesday"] = 1 << 2;
day_to_bit["Wednesday"] = 1 << 3;
day_to_bit["Thursday"] = 1 << 4;
day_to_bit["Friday"] = 1 << 5;
day_to_bit["Saturday"] = 1 << 6;
int N;
long long W;
while (cin >> N >> W) {
if (N == 0 && W == 0) break;
vector<long long> t(N);
vector<int> pattern(N);
for (int i = 0; i < N; ++i) {
int ci;
cin >> t[i] >> ci;
int mask = 0;
for (int j = 0; j < ci; ++j) {
string day;
cin >> day;
mask |= day_to_bit[day];
}
pattern[i] = mask;
}
bool possible = true;
for (int mask = 1; mask < (1 << 7); ++mask) {
long long sum_t = 0;
for (int i = 0; i < N; ++i) {
if ((pattern[i] & ~mask) == 0) {
sum_t += t[i];
}
}
int cnt = __builtin_popcount(mask);
if (sum_t > W * cnt) {
possible = false;
break;
}
}
cout << (possible ? "Yes" : "No") << endl;
}
return 0;
}
Now, think about potential integer overflow in sum_t: t[i] up to 1e10, N up to 100 => sum_t up to 1e12, safe.
Now, test with sample.
Now, about __builtin_popcount: If using MSVC, it's not available, but we can replace with bitset<7>(mask).count() or manual. To be safe, we can define a small function:
int popcount(int x) {
int c = 0;
while (x) { c++; x &= x-1; }
return c;
}
That's fine.
Now, finalize.
One more nuance: The problem might require reading the list of days line as a whole line, but using >> is simpler. But what if the day names contain spaces? They don't.
Now, answer.
Thus final solution: check Hall condition via subsets of days.
Now, I will write final answer with explanation. ```cpp
#include
#include
#include
#include
using namespace std;
// Map day names to bit positions (0 = Sunday, ... , 6 = Saturday)
const map<string, int> dayBit = {
{"Sunday", 1 << 0},
{"Monday", 1 << 1},
{"Tuesday", 1 << 2},
{"Wednesday", 1 << 3},
{"Thursday", 1 << 4},
{"Friday", 1 << 5},
{"Saturday", 1 << 6}
};
// Count set bits in a small integer (mask <= 127)
int popcount(int x) {
int c = 0;
while (x) {
c++;
x &= x - 1;
}
return c;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
long long W;
while (cin >> N >> W) {
if (N == 0 && W == 0) break;
vector<long long> need(N);
vector<int> availMask(N);
for (int i = 0; i < N; ++i) {
int ci;
cin >> need[i] >> ci;
int mask = 0;
for (int j = 0; j < ci; ++j) {
string day;
cin >> day;
mask |= dayBit.at(day);
}
availMask[i] = mask;
}
bool ok = true;
// Check all non‑empty subsets T of the 7 days
for (int T = 1; T < (1 << 7); ++T) {
long long sumNeed = 0;
// Sum demands of pupils whose availability is a subset of T
for (int i = 0; i < N; ++i) {
if ((availMask[i] & ~T) == 0) // availMask[i] ⊆ T
sumNeed += need[i];
}
if (sumNeed > W * popcount(T)) {
ok = false;
break;
}
}
cout << (ok ? "Yes" : "No") << '\n';
}
return 0;
}