We need to understand the problem: there are n students, each has a unique power (1..n distinct). Actually p_i are distinct? Input says "n different integers" so yes all powers are distinct. So we can order students by power.
Each student belongs to a school s_i (1..m). At least one student per school.
The Technogoblet selects the strongest (highest power) student from each school (original schools). Also we can make up new school names; we can assign some students to these new schools (by replacing their school in the Technogoblet's data). Each made-up name can be used at most once; effectively we can "reassign" some students to new distinct schools, each new school will also have its strongest student selected.
We want exactly k specific students (chosen ones) to be selected by the Technogoblet (i.e., they must be the strongest in their school, after possibly reassignments). We need minimal number of made-up schools (i.e., number of reassignments) to achieve that.
We can think of the initial configuration: for each school j (1..m), find the strongest student in that school (max power among students with s_i=j). That student is selected. For the chosen ones, some may already be selected (if they are strongest in their original school). Others are not.
We are allowed to change the school of some students: we can assign them to new schools (each new school can have multiple students? Wait: The problem says: "You can't use each made-up name more than once. In that case, Technogoblet would select the strongest student in those made-up schools too." So each made-up school name is used only once; meaning we can create a set of new schools, each with its own name, and we can assign some students to these new schools (by replacing their school name in the data). The new schools will also have a strongest student selected. But can we assign multiple students to the same made-up school? The text says "you can't use each made-up name more than once." That suggests each made-up name can be used exactly once, i.e., we can assign that name to one student? Or can we assign multiple students to the same made-up school? Typically, each school has a name. If we make up a new name, we can assign it to a student (changing his school). But can we assign the same made-up name to multiple students? The phrase "you can't use each made-up name more than once" suggests that each made-up name can only be used for one assignment. In other words, if we make up a school name X, we can set the school of exactly one student to X (or maybe more? But if we set it for multiple, that would be using the name multiple times, which is prohibited). So each made-up school can contain at most one student? Actually, think: If we have multiple students assigned to the same made-up school, then the name is used multiple times (as each student's school entry). But the phrase "you can't use each made-up name more than once" likely means you cannot assign the same made-up school name to more than one student. Therefore each made-up school name is used exactly once, i.e., each new school will have exactly one student (unless we assign multiple to it, which would be multiple uses). So each made-up school can have at most one student, because if we assigned two students to the same made-up school, we would have used that name twice (for both students). So we are effectively allowed to "reassign" some students to brand new singleton schools. However, note that the original schools may have many students; new schools we create can only have one student each (because we cannot reuse the made-up name). This is important.
But is it necessary? Let's read the problem statement again: "Arkady can make up some new school names and replace some names from Technogoblet with those. You can't use each made-up name more than once. In that case, Technogoblet would select the strongest student in those made-up schools too." He replaces some names from Technogoblet: that means he can change the school field of some students to a made-up name. If he uses the same made-up name for two different students, that would be using it more than once? Actually, he "uses" a made-up name each time he writes it into the Technogoblet. If he writes it for two students, that is using the name twice, which is not allowed because "you can't use each made-up name more than once". So each made-up name can appear at most once in the school list after replacement. Hence, each made-up school will contain exactly one student (the one whose school field was changed to that name). So indeed, if we reassign a student to a new school, that school will have only that student. So that student automatically becomes the strongest (and only) in that school, thus will be selected.
Thus, the operation: pick a student, change his school to a brand new unique school (which costs 1 new school). The student becomes selected (since he is the only one in that school). However, if the student is already selected in his original school (strongest) and we change him to a new school, that might cause his original school to lose its strongest, possibly affecting other chosen ones. So we need to carefully decide which students to reassign to new schools to ensure all chosen ones are selected, with minimal number of reassignments.
We have to guarantee that for each chosen student c, he is selected. That means either he remains in his original school and is the strongest there, or he is reassigned to a new school (then he is selected). But note: if we reassign any other student from the same school, it does not affect the strongest unless that student was the strongest. However, if we reassign the strongest student away from the school, then the next strongest becomes the strongest, and may or may not be a chosen one.
Goal: ensure each chosen one is selected. We can reassign some students (chosen or non-chosen) to new schools at cost 1 per student reassigned.
We want minimal number of made-up schools = minimal number of reassignments.
Observations: The power values are distinct and range 1..n. We'll treat higher number = stronger (since bigger power is stronger). But note p_i are different and from 1 to n inclusive. However, they could be any permutation. For convenience, we can think of ranking: student with power n is strongest overall.
We need to compute initial strongest per school: for each school j, find max power among students in that school. The student with that max is initially selected (call them school champions). For a chosen one c, if c is already a champion (i.e., strongest in its school), then no action needed for him, as long as we don't disturb his champion status. However, if we later reassign the champion away, he might lose it.
If a chosen one is not a champion, then to get him selected we have either: (a) reassign the champion(s) that are stronger than him in his school to other schools (so that he becomes champion), or (b) reassign him to a new school (so that he is alone and selected). Which option uses fewer reassignments? Typically reassigning the chosen one itself costs 1. Reassigning the stronger students may cost multiple if there are multiple stronger. But note, if we reassign the champion, we might also need to handle other stronger students? Actually, within a school, the champion is the strongest. If there are multiple students stronger than the chosen one, only the strongest among them is the champion, but all stronger than chosen one might still prevent him from being champion if we only remove the champion? Let's think: In a school, the selected student is the one with maximum power. If we want a specific student c to be selected in that school, we need to ensure that no student with power > p_c remains in that school. Because if any such student remains, they would outrank c. So to make c champion in his original school, we must remove all students with power > p_c from that school (by reassigning them to new schools). That could be many. Alternatively, we could reassign c itself to a new school, costing 1, and leave others as is. So in many cases, it's cheaper to reassign c itself.
But there is also the possibility of making c selected by reassigning him to a new school and also maybe some other students? Actually, if we reassign c to a new school, he becomes alone and selected. That's straightforward, cost 1 for c. However, we must also consider that after reassignments, the set of selected students includes champions of all original schools (except those whose champion we reassigned) plus champions of new schools (the reassigned students themselves). Reassigning c to a new school means we are moving him out of his original school. If c was originally not the champion, moving him out does not affect the champion of that school, but if he was champion, moving him out might cause the next strongest to become champion, potentially affecting chosen ones (maybe that next strongest is also a chosen one? If so, that could be good or bad). So we need to consider the global effect.
We need to guarantee that all chosen ones are selected. They can be selected either as champion of their original school or as a singleton in a new school. Also, they could be selected as champion of an original school after removal of stronger students. This is equivalent to moving all stronger students out of that school. That might be more expensive than moving the chosen one.
Thus, the minimal number of reassignments seems to be: for each chosen one that is not initially selected, we have two options: either we reassign him (cost 1) or we reassign all students in his school with higher power (cost count). But note that if multiple chosen ones belong to the same school, they share the stronger students. For example, school S has students A (power 10), B (power 8, chosen), C (power 5, chosen). If we want both B and C selected, we could: reassign A (cost 1) => then B becomes champion, but C is still not champion because B > C. So we also need to reassign B? Actually, if we remove only A, then B becomes champion and is selected. But C is not selected because B is stronger. To get C selected, we would need to also remove B, or move C. So it's more complex.
Alternatively, we could move both B and C to new schools (cost 2) and leave A as champion. That yields both selected.
Or we could move A to new school, then B becomes champion (selected), and move C to new school (cost 2). Or move A and B, then C becomes champion (selected) (cost 2). So the optimal may depend.
But note that there is also the constraint that we have only k chosen ones we care about. We don't care about other students being selected or not, except that they might interfere.
We need to compute minimal number of reassignments such that every chosen one is selected.
Given n <= 100, we could try to brute force or use DP/flow. But we need to find a simpler combinatorial answer.
Let's analyze further.
First, identify initially selected students: for each school, the student with maximum power in that school is selected. Let's call them "initial champions".
For each chosen one c:
-
If c is an initial champion, then currently he is selected. However, if we later reassign him to a new school, he would still be selected (since alone), but that would cause his original school to lose its champion, potentially affecting other chosen ones. So it might not be beneficial to reassign a chosen champion unless it helps elsewhere. If we leave him in his school, he remains selected as long as we don't remove him. But also, if we remove any stronger student? None stronger in that school because he is champion. So it's safe to keep him.
-
If c is not an initial champion, then currently he is not selected. To get him selected, we have two families of strategies:
a) Make him champion of his original school by removing all students in his school with power > p_c.
b) Move him to a new school (alone). This always works regardless of others.
Option a may involve reassigning multiple students, potentially including some chosen ones? If some stronger student is also a chosen one, we might consider moving that chosen one anyway, which could serve both purposes. But note: If a stronger student is also a chosen one, and we remove him (i.e., reassign him to a new school), then he becomes selected in his new school (since alone). So that might be beneficial if we want both selected. So we could, for a school with multiple chosen ones, reassign the stronger chosen ones to new schools, allowing the weaker chosen one to become champion. But careful: The stronger chosen one would then be selected in his new school, which is fine. However, if we reassign him, we incur cost 1. But if we kept him in the original school, he would be selected only if he is champion, which he might be if he is the strongest among remaining. So there's tradeoff.
Goal: minimize total reassignments.
Let's formalize: We have n students, each with power pi (distinct) and school si. For each school j, we have a set of students, and we know the maximum power student in that school (the champion). We can reassign any student to a new singleton school at cost 1 per student. After reassignments, the selected students are:
- For each original school that still has at least one student (i.e., we didn't reassign all of them? Actually, if we reassign all students from an original school, that school becomes empty; but the problem says at least one student studies in each school initially, but after reassignments, some schools may become empty? The Technogoblet selects the strongest student from each school. If a school has no students, does it still select? I think there is no selection from an empty school. So we consider only schools that still have at least one student. So if we reassign all students from an original school, that school will have no one, and no one selected from it. That's allowed.
- For each new school (each assigned student), the student is selected (since only one).
Thus, after reassignments, the set of selected students = the maximum-power student in each non-empty original school (after removal of reassigned students) plus all reassigned students (since each is alone in a new school).
We need that all chosen ones are in this selected set.
We can think of it as we decide which students to "extract" (reassign to new schools). The extracted students will be selected. The remaining students stay in original schools, and among them, each school's strongest will be selected.
Given that, we need to ensure that for each chosen one, either he is extracted, or he remains and is the strongest in his school among the remaining students.
We want to minimize number of extracted students.
This is reminiscent of a problem: We have schools with students ordered by power. We need to pick a set E of students to extract, such that for each chosen one c:
- if c not in E, then for all other students d in same school with higher power than p_c, we have d in E (i.e., all stronger students in that school are extracted). Because if any stronger student remains, they would be selected over c (unless they are also extracted). Additionally, if there are other students in the same school with higher power, but c is not champion, we need to extract them. However, note that it's possible that the strongest remaining student in that school after extractions might not be c if there is another student with power > p_c not extracted. So condition: For each school S, let R = set of students from S not extracted. Then the selected student from S is argmax_{i in R} p_i. For a chosen one c in R to be selected, we need p_c = max_{i in R} p_i. So indeed, all students in S with power > p_c must be extracted. Also, if there is another student with equal power? But powers are distinct, so only higher matters.
Thus, condition: For each chosen one c, either c in E, or for all i in same school with p_i > p_c, i in E.
Goal: minimize |E|.
That's a clean formulation: Choose a set of students to extract (send to new schools) such that for every chosen student c, either c is extracted, or all stronger students (in same school) are extracted. Minimize size of extracted set.
We are allowed to extract any student, including non-chosen ones.
Note: The extracted set corresponds to reassignments; each extracted student incurs a new school. So number of made-up schools = |E|.
Now, we need to find minimal number of extractions subject to constraints.
This looks like a covering / hitting problem. Because for each chosen c, we have a condition: either c is extracted, or all stronger students in the same school are extracted. Equivalent: At least one of the two sets must be fully extracted: {c} or the set of stronger students in the same school. But we want minimal number of extractions.
Observation: This is similar to a problem where we have a partial order within each school: for any two students in same school, the stronger one dominates the weaker? Actually, the condition for a non-extracted chosen c is that all stronger are extracted. That means that if we choose not to extract c, we must extract all stronger in that school. This may force extractions of many non-chosen stronger students. So it's often cheaper to extract c than to extract all stronger, especially if many stronger exist.
But also, note that extracting a stronger student may help multiple chosen ones in that school: For example, school S has students A (strongest), B (medium, chosen), C (weakest, chosen). If we extract A, then B becomes the strongest if B remains. But B is chosen, but we still need to ensure B is selected. If B is not extracted, after extracting A, B is strongest? But there is also C weaker than B. B would be selected. So condition for B: either extract B, or extract all stronger than B (which is A). So extracting A alone satisfies B's condition (since B not extracted). What about C? For C, if C not extracted, we need to extract all stronger than C: that includes A and B (since B > C). So extracting A alone does not satisfy C; we still need either extract C or extract B as well. So to satisfy both B and C without extracting them, we would need to extract A and B (two extractions), then C remains and becomes champion? Actually, after extracting A and B, the remaining students in S are C and possibly others weaker; C would be the strongest, so C selected. That costs 2 extractions (A and B). Alternatively, we could extract only C (cost 1) and keep A and B; then B is selected as champion (since A is still there? Wait, if we extract C, then B is still not selected because A is stronger. So B's condition: either extract B or extract all stronger (A). Since A remains, B not extracted => not selected. So extracting C alone does not satisfy B. So to satisfy B and C, we could: extract B and C (cost 2) — then A is champion selected? But A is not a chosen one, we don't care. B and C are extracted so they are selected via new schools. That works. Or extract A and B (cost 2) leaving C, then C selected. Or extract A and C (cost 2) leaving B, but then B condition: B not extracted, and all stronger? A is extracted, so B becomes champion and selected. That also works? Check: Extract A and C. Remaining in S: B and maybe others weaker. B is strongest => B selected. C is extracted => selected. So that works (cost 2). Or extract all three (cost 3) etc.
Thus, there is an interplay.
We need to find minimal extractions.
This is reminiscent of a hitting set or a vertex cover on a directed graph? Each chosen c imposes an "or" constraint: either we pick c or we pick all its dominators (stronger in same school). That is like: For each chosen c, we require that the set E contains either c or the entire set D_c = {i in same school | p_i > p_c}. Since D_c is a set of stronger students. Note that D_c is exactly the set of students in that school that outrank c.
If we think about ordering within a school: sort students by power descending. Then for a chosen student c, its D_c are those with higher power in that school. The condition is: either we extract c, or we extract all students that appear before c in descending order.
This is similar to a "covering" problem: we can choose to extract some students; the condition is that for each chosen c, if c is not extracted, then all students before c in that school are extracted.
This is reminiscent of problems where we need to ensure that for each school, the set of non-extracted chosen ones forms a suffix of the sorted order? Actually, think: If we decide which students to extract, then for each school, consider the remaining students (not extracted). Among those, the strongest is selected. For a chosen student c to be selected among remaining, it must be that in the remaining set, no student with higher power exists. That means that among the non-extracted students, the highest power must be that of c if c is not extracted and c is chosen; but if there are multiple chosen ones in a school, only the strongest among those not extracted can be selected. Actually, note: The condition requires that every chosen student is selected, not just one per school. But can multiple chosen students from the same school be selected? They can, but only if they are in different schools after extraction? Actually, after extraction, each original school will select exactly one student (the strongest among those remaining). So at most one non-extracted student per original school can be selected. Therefore, if two chosen students from the same original school both remain in the school (not extracted), then only the stronger of them can be selected; the weaker will not be selected because there is a stronger (the other chosen one) remaining. So it is impossible for two chosen ones from the same original school to both be selected if they both stay in the same school. However, they could both be selected if one of them is extracted (goes to a new school) and the other remains and becomes champion. So indeed, for a school with multiple chosen ones, we cannot have more than one of them remain in the school and be selected; we would need to extract at least (number_of_chosen_in_school - 1) of them, or extract all stronger students to allow the weakest to be champion, but that still leaves the stronger ones as obstacles. Actually, consider school with chosen students sorted by power descending: c1 (strongest), c2, c3,... If we extract none of them, only c1 (the strongest) is selected as champion, c2 and c3 are not selected. To get all selected, we must extract some of them to new schools. For example, extract c2 and c3, leaving c1 as champion, and c2 and c3 are extracted so selected. That works. Or extract c1, then c2 becomes champion (selected), and we still need c3 selected: could either extract c3 or also extract c2? If we extract c1, then c2 is champion; but c3 is not selected because c2 > c3 and still present. So we would need to extract c3 as well (or extract c2, then c3 becomes champion). So multiple options.
In general, for each school, the condition that all chosen ones are selected translates to: For each school, among the chosen ones that are not extracted, there can be at most one, and that one must be the strongest among all non-extracted students in that school (i.e., all stronger non-chosen students must be extracted). Also, any chosen ones that are not extracted must be exactly that strongest one. All other chosen ones must be extracted.
So we need to decide, for each school, which (if any) chosen one will be the "representative" that remains in the school and becomes champion. That representative must be the strongest among the remaining non-extracted students. That means all students in that school that are stronger than that representative (whether chosen or not) must be extracted. Also, all other chosen ones in that school (weaker than the representative) must be extracted, because they would otherwise remain and not be selected (since the representative is stronger). However, it's also possible that the school has no remaining chosen ones (i.e., we extract all chosen ones from that school), and then only non-chosen might be champion, but that's fine as long as all chosen are selected elsewhere (by extraction). So the plan can be: For each school, we either select zero or one chosen student to stay (not extracted). If we select a chosen student c to stay, then we must extract all students (both chosen and non-chosen) with power > p_c (i.e., all stronger than c) in that school. Additionally, we must extract all other chosen students in that school (the ones weaker than c) because they would not be selected otherwise. Actually, could a weaker chosen student also be selected if we extract the stronger ones? Wait: If we keep c (stronger chosen) and also keep d (weaker chosen) without extraction, then d is not selected because c is stronger and remains. So d must be extracted. So yes, all other chosen students in that school must be extracted. So if we keep a chosen c in the school, we extract all other chosen students (both stronger? but if c is kept, there can't be any stronger chosen because they would be stronger than c; but if there is a stronger chosen, we must extract it because it's stronger, so that's covered under extract all stronger). So indeed, if we keep exactly one chosen per school, we must extract all other students in that school that are either stronger than that chosen, or are chosen but weaker (or stronger? Actually stronger are covered). In other words, we extract all students except the chosen c and any students weaker than c that are not chosen? But wait: what about non-chosen students weaker than c? They can stay or be extracted; they don't affect c's selection because they are weaker, and they are not chosen, so it's fine if they stay. However, they would not be selected (since c is champion), but that's irrelevant. But could their presence cause any issue? No, because only the strongest matters. So we can leave them. So if we keep a chosen c in the school, we can leave all weaker non-chosen students untouched. That would not cost extra extraction. But note: They might be chosen? No, only c is chosen. So that's fine.
If we keep no chosen student in the school, then we don't need to keep any specific student; we can leave any set of non-chosen students. However, we must still ensure that all chosen students from that school are extracted (since they are not staying, they must be extracted to be selected). So we must extract all chosen students from that school (since they are not the representative). In addition, we might need to extract some non-chosen? Not required, but we might choose to extract some non-chosen if it helps something else? No, extracting non-chosen only adds cost, no benefit unless it helps some chosen in another school? But extractions are only to satisfy conditions for chosen; extracting a non-chosen doesn't help any chosen directly (except if that non-chosen is stronger than some chosen in same school, but if we are extracting all chosen from that school, we don't need to extract stronger non-chosen because we are not keeping any chosen. However, what about a chosen from another school? No, extraction of a student only affects its own school (by removing it) and adds that student as selected (but that doesn't matter for other schools). There is no cross-school interaction except through the fact that extraction count is minimized. So we would not extract non-chosen unless necessary to satisfy a condition for a chosen in that school. Therefore, if we keep no chosen in a school, we can simply extract all chosen in that school, and leave all non-chosen as is (including stronger ones). That would satisfy that all chosen are extracted, hence selected. However, there is a nuance: if a chosen is extracted, he's selected. So it's fine.
But wait: Is it always allowed to leave non-chosen students in a school without extracting them? Yes, they will be part of the school, and the strongest among them will be selected (if they remain). That selected student is not a chosen one, but that's okay. So no issue.
Thus, the optimization reduces to: For each school, we can either:
-
Option A: Extract all chosen students in that school (so they become selected via new schools). No need to extract any non-chosen. Cost = number of chosen in that school.
-
Option B: Pick one chosen student c in that school to remain (as the champion). Then we must extract all other chosen students from that school (cost = (total_chosen_in_school - 1)) plus extract all non-chosen students that are stronger than c (since they would outrank c). There is no need to extract weaker non-chosen. Cost = (total_chosen_in_school - 1) + (number of non-chosen students in that school with power > p_c).
We need to decide, for each school, which option to take (including which chosen to keep, if any) to minimize total extractions across all schools.
But careful: There might be interactions between schools? Extractions are independent per school because each student belongs to exactly one original school. The only global constraint is that we need all chosen ones selected. If we extract a chosen, that's fine. If we keep a chosen, we must ensure no stronger student (including non-chosen) remains in that school. That's a per-school condition. There's no cross-school constraint because each student's extraction only affects its own school. However, there is a subtle point: If we extract a student, that student becomes selected in a new school, but that doesn't affect other schools. So yes, the problem is separable by school. But is there any scenario where we might want to extract a non-chosen from a school even if we are not keeping any chosen in that school, because that non-chosen might be a stronger student that would otherwise be selected and might somehow interfere with a chosen from another school? No, because selection is per school; a student from school A cannot affect selection in school B. So cross-school interaction does not exist. The only global objective is to minimize total extractions, and the constraints are per chosen student, which involve only students in the same school. So we can solve independently per school and sum costs.
Thus, the answer is sum over schools of minimal cost to satisfy all chosen in that school.
But need to verify: Could there be an advantage to extracting a non-chosen from a school even when not keeping a chosen, if that non-chosen is also a chosen? No, then it's chosen. For non-chosen, extracting them only costs and doesn't help any chosen condition. So not beneficial.
Therefore, the problem decomposes by school.
But wait: there is one more nuance: what about the scenario where a chosen student is initially the champion? If we keep that chosen, we may need to extract stronger non-chosen? But if he is champion initially, there are no stronger students in that school. So cost to keep him would be (total_chosen_in_school - 1) (extract other chosen, if any) + 0 (since no stronger non-chosen). That's possibly cheaper than extracting all chosen (cost total_chosen_in_school). So keeping a champion chosen is cheaper by 1 per school if there are multiple chosen, because we save extracting that particular chosen.
If there is only one chosen in the school and he is champion, keeping him costs 0 (no extraction) vs extracting him costs 1. So definitely keep.
If he is not champion, keeping him costs: (chosen_in_school - 1) + (# stronger non-chosen). That might be more or less than extracting all chosen.
We need to compute, for each school, the minimal extraction cost.
Let's formalize for a given school S.
Let T = set of students in S. Let C = set of chosen ones in S. For each chosen c, define rank: order by power descending. Let stronger_non_chosen(c) = number of non-chosen students in S with power > p_c.
If we decide to keep exactly one chosen c (i.e., c remains, all other students in S with power > p_c are extracted, and all other chosen are extracted), the cost is: (|C| - 1) + stronger_non_chosen(c). Because we extract all other chosen (|C|-1) and all non-chosen stronger than c.
But wait: Is there any need to extract non-chosen that are stronger than c but are also chosen? That's covered: all other chosen are extracted anyway; among those, if there is a chosen stronger than c, they are included in (|C|-1) and we don't double count. So stronger_non_chosen(c) counts only non-chosen.
Also, note: what about non-chosen that are weaker than c? They can stay, no cost.
If we decide to keep no chosen (i.e., extract all chosen), the cost is |C|. Because we extract all chosen, and we don't need to extract any non-chosen. That's it.
Now, is there possibility to keep more than one chosen in the same school? As argued, only one can be selected from the original school because only one champion. So if we keep two chosen, the weaker won't be selected unless we extract the stronger, but then the stronger would need to be extracted to be selected (if extracted) or else not selected. So it's not possible for both to be selected without extracting at least one. So keeping two chosen in same school is not allowed because the weaker would not be selected. Therefore the only valid configurations are: either zero or one chosen remain.
Thus, the minimal cost for the school is min( |C|, min_{c in C} [ (|C|-1) + stronger_non_chosen(c) ] ). But also consider the possibility of keeping a non-chosen? That would not help because then all chosen must be extracted anyway (cost |C|) and we could also keep non-chosen but that doesn't reduce cost. So no.
So answer = sum over schools of min( |C|, min_{c in C} ( |C|-1 + stronger_non_chosen(c) ) ).
But wait: There is also the possibility that we might keep a chosen and also extract some non-chosen weaker than him? That would increase cost, not beneficial.
Also, we might consider extracting some chosen and keeping none, but also extracting some non-chosen? Not beneficial.
Thus, the formula seems correct.
But we must check edge cases: If C is empty (school has no chosen ones), then we don't need to do anything; cost 0. So we only sum over schools with chosen.
Now, is there any scenario where we would want to extract a non-chosen even if we keep no chosen? Possibly if that non-chosen is also a chosen? No. If there are no chosen, we don't need to do anything; extracting any student only adds cost and doesn't help. So not.
Thus answer = sum_{schools} min( |C|, min_{c in C} ( |C|-1 + count_stronger_non_chosen(c) ) ). But if C is empty, term is 0.
Now, let's test with provided examples.
Example 1:
n=7, m=3, k=1.
powers: [1,5,3,4,6,7,2] (student ids 1..7)
schools: [1,3,1,2,1,2,3]
chosen: [3] (id 3)
So chosen student id 3: power 3, school 1.
Now, for school 1: students? student ids: 1 (p1=1, s=1), 3 (p=3, s=1), 5 (p=6, s=1). Also? Check: student 2 is school 3, 4 school 2, 6 school 2, 7 school 3. So school 1 has students: 1 (p=1), 3 (p=3), 5 (p=6). Among these, chosen: only 3.
So |C| = 1.
Compute stronger_non_chosen(c): non-chosen students in school 1 with power > p_c=3: student 5 (p=6) is non-chosen? yes, student 5 is not chosen (chosen are only 3). So count = 1. Also student 1 is weaker, ignore.
Option keep c: cost = (|C|-1) + stronger_non_chosen = 0 + 1 = 1.
Option extract all chosen: cost = 1.
min = 1. But answer in example is 1. Good.
But wait, could there be an alternative: we could also consider extracting student 5 (the stronger non-chosen) and keep student 3, cost 1. That's same as keep c. Or extract student 3, cost 1. So minimal 1.
Now, what about school 2 and 3? They have no chosen, cost 0. Total = 1. Matches output.
Example 2:
n=8, m=4, k=4.
powers: 1 2 3 4 5 6 7 8 (ids 1..8)
schools: 4 3 2 1 4 3 2 1 (i.e., student1 school4, student2 school3, student3 school2, student4 school1, student5 school4, student6 school3, student7 school2, student8 school1)
chosen: 3 4 5 6 (ids 3,4,5,6)
Let's list students by school:
School 1: students 4 (p=4), 8 (p=8) -> chosen: 4 is chosen, 8 not.
School 2: students 3 (p=3), 7 (p=7) -> chosen: 3 is chosen, 7 not.
School 3: students 2 (p=2), 6 (p=6) -> chosen: 6 is chosen? Yes, 6 is chosen (id6). 2 not.
School 4: students 1 (p=1), 5 (p=5) -> chosen: 5 is chosen, 1 not.
Now compute per school.
School 1: C = {4}. |C|=1. stronger_non_chosen(c): non-chosen students with power > p_4=4: student 8 (p=8) is non-chosen? yes, 8 is not chosen. So count=1. Option keep: 0+1=1. Option extract all:1. min=1.
School 2: C = {3}. stronger_non_chosen: student 7 (p=7>3) count=1. min=1.
School 3: C = {6}. stronger_non_chosen: student? students: 2(p=2),6(p=6). Non-chosen stronger than 6? none because 2 is weaker. So count=0. Option keep:0+0=0. Option extract:1. min=0.
School 4: C = {5}. stronger_non_chosen: student? students:1(p=1),5(p=5). none stronger. count=0. Option keep:0, extract:1, min=0.
Sum = 1+1+0+0 = 2. Output is 2. Matches.
Thus formula works for examples.
But wait, need to consider scenario with multiple chosen in a school.
Let's test a custom case: n=3, m=1, k=2. Students: id1 p=3, id2 p=2, id3 p=1; all in school1. Chosen: id2 and id3. So |C|=2. Compute options:
- Extract all chosen: cost=2.
- Keep id2: need to extract stronger non-chosen? stronger_non_chosen(id2) = number of non-chosen with power >2: student1 (p=3) is non-chosen? id1 not chosen, so count=1. Also need to extract other chosen (id3). So cost = (2-1) + 1 = 2.
- Keep id3: stronger_non_chosen(id3) = non-chosen with power >1: students id1 (p=3) and id2 (p=2) but note id2 is chosen, so count only non-chosen: id1 is non-chosen, count=1; but also need to extract other chosen (id2). So cost = (2-1)+1 = 2. So min=2. Could there be a better solution? Possibly we could extract id1 and id2 (cost 2), leaving id3 as champion -> selected; id2 extracted -> selected. Cost 2. Or extract id1 and id3 (cost 2), leaving id2 champion -> selected; id3 extracted -> selected. Or extract all three? cost3. Or extract id2 and id3 (cost2), leaving id1 champion; both chosen extracted -> selected. So min=2. Good.
Consider a case where keeping a chosen yields lower cost than extracting all. Suppose school with 3 chosen: ids: A(p=100), B(p=90), C(p=80) and also non-chosen D(p=95). So students: D (non-chosen) p=95, A 100, B 90, C 80. Chosen: A, B, C. |C|=3.
Option extract all: cost 3.
Option keep A: stronger_non_chosen(A) = non-chosen with power >100? none, because D is 95 <100, so count=0; extract other chosen (B and C) -> cost = (3-1)+0 =2. That's better.
Option keep B: stronger_non_chosen(B)= non-chosen with power>90: D (95) and A (100) but A is chosen, so only non-chosen is D, count=1; plus extract other chosen: A and C -> cost (3-1)+1=2+? Actually (2) + 1 = 3? Wait (|C|-1)=2, plus count=1 => 3. So cost 3.
Option keep C: stronger_non_chosen(C)= non-chosen >80: D(95), A(100), B(90) but A and B are chosen so only non-chosen is D, count=1; plus extract other chosen (A,B) => (2)+1=3. So min=2. So keeping strongest chosen can be beneficial if there are few stronger non-chosen.
Now consider a case with multiple chosen but no non-chosen stronger than the strongest chosen, then keep strongest chosen costs (|C|-1) which is less than |C| if |C|>1. So min = |C|-1.
Example: school with 2 chosen, both are the top two in school (no stronger non-chosen). Then extract all cost 2; keep stronger chosen cost 1 (extract the weaker). So answer 1. Indeed, we can just extract the weaker chosen, leaving stronger as champion. That yields both selected (stronger as champion, weaker as extracted). That's minimal.
Now consider a case where there is a non-chosen stronger than the strongest chosen. Then keep strongest chosen costs (|C|-1) + (number of such stronger non-chosen). Might be > |C|, so better to extract all.
Thus per-school DP is straightforward.
But wait: is there any global constraint that the number of made-up schools (extractions) cannot exceed something? No, just minimize.
Thus answer = sum over schools with chosen of min(|C|, min_{c in C} (|C|-1 + cnt_stronger_non_chosen(c))). For schools without chosen, cost 0.
We need to compute for each school the list of students, determine which are chosen, compute counts.
Edge cases: If there are no chosen in school, cost 0. If there is exactly one chosen, cost = min(1, 0 + cnt_stronger_non_chosen(c)). Actually for single chosen c, |C|-1 =0, so cost = cnt_stronger_non_chosen(c). But we also have option extract all chosen: cost=1. So cost = min(1, cnt_stronger_non_chosen(c)). That makes sense: either extract the chosen (cost 1) or extract all stronger non-chosen (cost maybe 0 if none). If there are no stronger non-chosen, min is 0, meaning we can keep the chosen without any extraction (he is champion). That's correct: if he is already champion, we don't need to do anything. If there are some stronger non-chosen, we compare extracting him (1) vs extracting the stronger non-chosen (count). If count >= 1, then extracting him might be cheaper if count >1? Actually min(1, count). So if count >= 1, min is 1? Wait, min(1, count): if count=0, min=0; if count=1, min=1; if count>=2, min=1. So the minimal cost for a school with a single chosen is either 0 if he is already champion (no stronger non-chosen), else 1. That means we would always prefer to extract the chosen (cost 1) rather than extract multiple stronger non-chosen if count>1, because 1 < count. But is it always valid to extract the chosen? Yes, then he becomes selected in a new school, and we don't need to touch the stronger non-chosen. That works. So the cost is 1 if there is any stronger non-chosen, else 0. That seems right.
But wait, could there be a scenario where extracting the stronger non-chosen(s) is beneficial because it might also help other schools? No, they're in different schools.
Thus answer per school is straightforward.
Now, we must be careful: what about the possibility of keeping a chosen but also extracting some other non-chosen weaker than him? Not needed.
Thus solution is O(n) to compute per school.
But hold on: there is nuance: The problem says: "You can't use each made-up name more than once." That means we cannot assign two students to the same new school. But we assumed each extracted student gets a distinct new school. That's fine. There's no limit on the number of new schools we can make up, we just want minimal.
Thus answer is as described.
Now, let's double-check with more complex scenarios to ensure no hidden interaction: e.g., what if a chosen is not champion, but we decide to extract some stronger non-chosen, but also we might want to extract a stronger chosen? Actually, if we keep a chosen c, we must extract all stronger students (both chosen and non-chosen) and all other chosen (which are weaker). This is captured by (|C|-1) includes chosen stronger? Actually, if there is a chosen stronger than c, that stronger chosen must be extracted because it's both stronger and chosen. But our formula (|C|-1) counts all other chosen regardless of relative power. However, the "stronger_non_chosen(c)" counts only non-chosen stronger than c. So total extractions would be: all other chosen (including those stronger and weaker) + all non-chosen stronger. But is it necessary to extract weaker chosen? Yes, because if we keep c and also keep a weaker chosen d (with power less than c), then d remains in the school and is not selected (since c is stronger). So d must be extracted to be selected. So indeed all other chosen must be extracted, regardless of power relation. So our cost is correct: (|C|-1) + (#non-chosen stronger than c).
But is there a scenario where we could keep a chosen c, and also keep a weaker chosen d, but then also extract c? No, if we keep d, c must be extracted because it's stronger, but then d becomes champion? That would be a different plan: keep d, extract c and others. That would be cost (|C|-1) + stronger_non_chosen(d). So we consider each possible chosen as the one to keep. So the min over c captures the best choice.
Thus the per-school optimum is indeed that.
Now, is there any possibility of not extracting any student from a school and yet having multiple chosen selected? No, as argued.
Therefore answer computed as sum over schools of that min.
Now, we should verify with the note: In example 1, they said answer 1, which matches.
Edge Cases: what if a chosen is the only student in his school? Then |C|=1, stronger_non_chosen(c)=0, cost = min(1,0)=0. Indeed, no action needed: he is champion automatically. Good.
Now, consider a school with multiple chosen but also many non-chosen stronger than the strongest chosen. For strongest chosen c_max, stronger_non_chosen(c_max) = number of non-chosen stronger than him. That might be high. So we might not keep any chosen; instead extract all chosen. That cost = |C|. If |C| is less than (|C|-1 + count), then extract all chosen is better.
But also consider keeping a weaker chosen might have even higher cost due to more stronger non-chosen plus also having to extract more chosen (since other chosen include the stronger ones). So min is straightforward.
Thus algorithm:
-
Input n, m, k.
-
powers p[1..n]
-
schools s[1..n]
-
chosen list c[1..k]
-
Mark chosen students: bool chosen[i] = true if i is in list.
-
For each school j from 1 to m, gather list of student indices in that school. For each student, record power and whether chosen.
-
For each school, compute:
total_chosen = count of chosen in school.
If total_chosen == 0: cost_s = 0.
Else:
For each chosen student in the school, compute stronger_non_chosen = count of non-chosen students in this school with power greater than this chosen's power.
Compute keep_cost = (total_chosen - 1) + stronger_non_chosen.
Compute extract_all_cost = total_chosen.
cost_s = min(extract_all_cost, min over chosen of keep_cost).
-
Sum cost_s.
But note: For a chosen student that is the strongest? Actually, if there is a chosen student who is the overall strongest in the school (i.e., no student stronger, whether chosen or not), then stronger_non_chosen = 0. Then keep_cost = total_chosen - 1. So cost_s = min(total_chosen, total_chosen - 1) = total_chosen - 1. So we save 1 extraction compared to extracting all. That makes sense: keep the strongest chosen, extract all other chosen.
If there is a non-chosen stronger than all chosen, then keep_cost for the strongest chosen includes those non-chosen. Could be high.
Thus answer is sum.
Now, need to double-check if there is any scenario where we might want to extract some non-chosen even if we extract all chosen? That would be extra cost, not beneficial. So no.
Thus solution is simple.
But before finalizing, let's think about the initial reasoning: We assumed that we can arbitrarily reassign students to new schools, but we also must consider that when we reassign a student, that student becomes selected (since alone). However, what if we reassign a student who is not chosen? That would add a selected student that is not needed. That's allowed; it doesn't break anything. So it's fine.
Also, we assumed that each reassigned student gets a distinct new school. That's required but not limiting, we can always create new names.
Now, confirm that our cost model exactly equals number of reassignments (made-up schools). Because each extraction corresponds to reassigning a student to a new school. So number of extractions = number of made-up schools.
Now test some tricky cases:
Case: School with 2 chosen, both are not strongest; there is a non-chosen stronger than both. Suppose students: N (non-chosen, p=100), A (chosen, p=90), B (chosen, p=80). Options:
- extract all chosen: cost 2.
- keep A: need to extract N (since stronger) and B (other chosen) -> cost 2.
- keep B: need to extract N and A -> cost 2.
So min=2. Could we do better by extracting N only? If we extract N, then both A and B remain. Then A is champion (since p=90 >80), so A selected, B not selected. So B not selected. So not allowed. If we extract N and B, then A remains and is champion -> selected; B extracted -> selected. cost 2. If we extract N and A, B remains champion -> selected; A extracted -> selected; cost 2. If we extract only A? Then N remains, N champion; A extracted -> selected; B not selected. Not allowed. So indeed min=2.
Case: School with 2 chosen, and no non-chosen stronger. Suppose both chosen are top two: A(p=100, chosen), B(p=90, chosen). Options:
- extract all: 2.
- keep A: extract B -> cost 1.
- keep B: need to extract A (stronger) -> cost 1 (since no non-chosen stronger). Actually (|C|-1)=1, stronger_non_chosen(B)=0, cost=1.
So min=1. Indeed we can just extract the weaker, keep stronger. Works.
Case: School with 3 chosen, and one non-chosen stronger than all. N(p=100), A(p=90, chosen), B(p=80, chosen), C(p=70, chosen). Options:
- extract all chosen: cost 3.
- keep A: extract N and B and C -> cost 1 (for N?) Actually (|C|-1)=2 (extract B and C) + stronger_non_chosen(A)=1 (N) =3.
- keep B: (|C|-1)=2 (extract A and C) + stronger_non_chosen(B) = ? N is stronger than B, and A is stronger than B but A is chosen, counted in (|C|-1). So non-chosen stronger: N only, count=1 => total 3.
- keep C: (2) + stronger_non_chosen(C): N is stronger, count=1 => 3.
So min=3 (extract all chosen). Could we do better by extracting N and maybe some chosen? If we extract N and keep A and B? But then both A and B remain, only one can be champion; B not selected. So not allowed. If we extract N and B, keep A and C? A remains, champion, C not selected. So need all chosen selected. So must ensure each chosen is either extracted or champion. With N present, if we keep only A (extract N, B, C) cost 3, same. Or extract N, A, B, keep C -> cost 3. So indeed min=3.
Case: School with 3 chosen, non-chosen stronger than some but not all. e.g., N(p=100), A(p=95, chosen), B(p=90, chosen), C(p=85, chosen). Options:
- extract all: 3.
- keep A: extract N, B, C = cost 1+2=3.
- keep B: extract N, A, C = 1+2=3.
- keep C: extract N, A, B = 1+2=3.
So min=3.
What if N is not stronger than A? Actually if N is weaker than A? Then A is champion even with N? Let's try: N(p=88), A(p=95), B(p=90), C(p=85). Then strongest is A. Options:
- extract all: 3.
- keep A: extract B, C (since N is weaker, no need to extract N) => cost 2.
- keep B: need to extract A (stronger) and N? N is non-chosen with power 88 < 90, but A is stronger, so stronger_non_chosen(B) = non-chosen with power >90: N has 88, so none; but A is chosen, counted in (|C|-1). So cost = 2 (extract A and C) =2? Actually (|C|-1)=2, because total chosen=3, so (3-1)=2. Need to extract other chosen: A and C. But A is stronger than B, so extracted. C is weaker, also extracted because it's other chosen. So cost =2. So keep B cost 2. That's better than extracting all (3). However, does that work? Keep B (not extract B), extract A and C. N remains (power 88). Then remaining students in school: B (p=90) and N (p=88). B is strongest -> selected. A and C extracted -> selected. So all chosen selected. Cost 2. So min=2. Our formula for B: (|C|-1)=2, stronger_non_chosen(B)=0 => cost 2. For A: (2)+ stronger_non_chosen(A) = 2 + 0 =2 as well. So min = 2. So better than extract all.
Now, could we do cost 1? Possibly if we keep A and extract only B? Then C not selected. So no.
Thus formula yields 2.
Now consider scenario where there are multiple chosen and some non-chosen stronger than some but not all.
Now, what about scenario where we might keep no chosen but also extract some non-chosen for free? Not free; cost increases. So not beneficial.
Thus the problem reduces to that simple per-school DP.
But wait: There's one more nuance: What about a student who is chosen and also the strongest in his school, but there is a stronger non-chosen? That's impossible: if he is strongest, there is no student with higher power, so stronger_non_chosen = 0.
Now, is there any possibility of extracting a student who is not chosen, and that somehow reduces the number of extractions needed for another school? No, because schools independent.
Thus, solution is straightforward.
Now, we must be careful about indexing: powers are given as p_i (distinct). But they say "different integers p_i", but they are not necessarily sorted. We'll need to compare powers.
Implementation steps:
- Read n, m, k.
- Read array p[1..n] (power)
- Read array s[1..n] (school)
- Read list of chosen ids c[1..k]. Mark chosen[id] = true.
- Create vector for each school (1..m) storing pairs (power, is_chosen) or just list of students with power and chosen flag.
- For each school:
- Let total_chosen = count chosen in that school.
- If total_chosen == 0: continue.
- Compute for each chosen student the number of non-chosen students in same school with power greater than that student's power.
- Compute keep_costs = for each chosen: (total_chosen - 1) + cnt_stronger_non_chosen.
- Compute best_keep = min(keep_costs) if any.
- cost = min(total_chosen, best_keep).
- Add to answer.
Complexity O(n log n) or O(n^2) because n <= 100, so trivial.
But careful: If a school has chosen students, but some of them might be stronger than others, we need to count stronger non-chosen accurately. For each chosen, we need to count non-chosen with power > p_c. Since n small, we can just loop over all students in the school for each chosen.
But we can also sort students in each school by power descending, then process. Since n <=100, any method works.
Edge Cases: There could be schools not referenced? Input says m schools, and at least one student per school, but we have m from 1 to n. So all schools have at least one student.
Now, confirm with some tricky scenarios to ensure no misinterpretation:
-
Could we possibly reassign a student to a new school but also keep him in original? No, reassign means changing his school to a new one; he is no longer in original school.
-
Could we reassign multiple students to the same new school? The problem says you can't use each made-up name more than once. So each made-up name can be used only once. So each reassigned student must have a unique new school name. So each extracted student gets its own new school.
-
Could we reassign a student to an existing school? The problem says "make up some new school names and replace some names from Technogoblet with those." So we can only replace with made-up names, not change to other existing schools. So we cannot move a student to another existing school. So our extraction model is correct: we can only remove a student from his original school and place him in a brand new singleton school.
-
Could we leave a student in original school but also create a new school for him? No.
Thus, extraction = moving to new school.
Now, what about the possibility of not reassigning any student but rather "making up" a new school and assigning a student to it. That's exactly extraction.
Now, verify with examples: In first example, they made up a new school for student 3 (chosen) and that cost 1. They could have instead moved student 5 (stronger) to a new school, also cost 1, leaving student 3 as champion. That would also work. So our model gives cost 1.
Thus answer matches.
Now, check second example: They said change school of student 3 to made-up 5 and student 4 to made-up 6, so they extracted two students: 3 and 4 (both chosen). But our per-school cost gave: school1 cost 1 (extract 4 or extract 8? Actually we computed min=1 for school1, which is either extract 4 or extract 8? Let's see: school1 had chosen 4. Options: extract 4 (cost 1) or extract stronger non-chosen 8 (cost 1). They chose to extract 4? Actually they extracted 3 and 4. But school1: they extracted 4? In the output description, they changed school of student 3 (school2?) Wait, second example: n=8, students: 1 2 3 4 5 6 7 8; schools: 4 3 2 1 4 3 2 1; chosen: 3 4 5 6. They said: "you can change the school of student 3 to the made-up 5 and the school of student 4 to the made-up 6." So they extracted student 3 (from school2) and student 4 (from school1). That's two extractions. But we computed school1 cost 1, school2 cost 1, school3 cost 0, school4 cost 0 => total 2. However, we also need to ensure that after these extractions, all chosen are selected. Let's check: Extractions: student3 (id3) reassigned to new school5, student4 (id4) to new school6. Then remaining:
School1: students: 4 is gone, so remaining: 8 (p=8). So champion is 8 (selected). But chosen 4 is extracted, so selected via new school. Good.
School2: students: originally 3 and 7. 3 is gone, so remaining 7 (p=7) => champion 7 selected. Chosen 3 is extracted, selected.
School3: students: 2 and 6. No extraction. So champion is max(2,6)=6 (chosen 6) selected. Good.
School4: students: 1 and 5. No extraction. Champion is max(1,5)=5 (chosen 5) selected. Good.
So all chosen selected. So 2 extractions. That matches our per-school min: school1 had min=1 (either extract 4 or extract 8). They extracted 4. School2 min=1 (either extract 3 or extract 7). They extracted 3. So total 2.
Could there be a solution with 1 extraction? If we try to extract only one student, say extract 8 from school1, then school1 champion becomes 4 (chosen 4) selected; school2 still has 3 and 7, champion 7, chosen 3 not selected; to get 3 selected we'd need to either extract 7 or extract 3. So need at least 2. So minimal is 2. Our method yields 2.
Now, consider scenario where a school has multiple chosen and we can do better by keeping one and extracting the others, but also need to extract stronger non-chosen. That's accounted.
Thus solution seems correct.
But before finalizing, we must consider if there is any alternative strategy where we extract a non-chosen from a school but also keep a chosen, but maybe we could extract a non-chosen that is not stronger than the chosen but maybe it helps elsewhere? No.
Therefore, the answer is as described.
However, we should double-check the initial reasoning: The condition we derived "for each chosen c, either c in E or all stronger students in same school in E" is necessary and sufficient for the selected set after extraction to include c? Let's verify.
After we extract set E, the selected students are: all students in E (since each gets its own new school) plus the strongest in each original school among those not in E.
For a chosen c:
- If c in E, then c is selected (since in a new school). Good.
- If c not in E, then c remains in its original school. For c to be selected from that school, it must be that no other student in that school (not extracted) has power greater than p_c. That means all students with power > p_c must be in E. So condition: For all i in same school with p_i > p_c, i ∈ E.
That's exactly our condition.
Thus necessary and sufficient.
Now, we want to minimize |E|.
Given that the condition only involves comparisons within same school, the problem is per-school.
Now we need to find minimal |E| subject to for each school, we choose a set of students to extract.
We can treat each school independently because the constraints for different schools are independent; the objective is additive (total extractions). So we can minimize per school and sum.
Now, per school, we need to find the minimal number of students to extract such that for each chosen c in that school, either c is extracted or all stronger students are extracted.
This is a covering problem on a set partially ordered by power. But we can think of it as we can decide which chosen to "keep" (i.e., not extract) such that at most one chosen is kept per school (since if we keep two, the weaker won't satisfy condition unless we extract the stronger, but then the stronger is extracted, contradicting "keep"). Actually, if we keep two chosen, say c1 (stronger) and c2 (weaker), then for c2 to be selected, all stronger students must be extracted, including c1. So c1 cannot be kept. So indeed at most one chosen can be kept per school. So either we keep zero chosen (extract all chosen) or keep exactly one chosen.
If we keep exactly one chosen c, then for c to be selected, all stronger students (whether chosen or not) must be extracted. Also, all other chosen (weaker ones) must be extracted (because they are not kept and need to be selected either by extraction or by being champion; but they cannot be champion because c is stronger and remains). So they must be extracted. So condition: E must contain all students in the school except possibly c and any students weaker than c that are not chosen. But we don't need to extract weaker non-chosen because they don't affect c's selection and they are not required to be selected (they are not chosen). So the minimal extraction set for this strategy is: all chosen except c, plus all non-chosen with power > p_c. That's exactly keep_cost(c).
If we keep zero chosen, then we need to extract all chosen. There is no requirement to extract any non-chosen. So extraction set = all chosen. That's extract_all_cost.
Thus per-school min is as described.
One more nuance: Could we keep a chosen and also extract some weaker non-chosen? Not needed, adds cost, so not optimal.
Thus solution stands.
Now, implement.
Implementation details:
-
Input: n, m, k.
-
powers: array p of length n (1-indexed).
-
schools: array s of length n.
-
chosen: boolean array of size n+1, initially false.
-
Read k ids, set chosen[id]=true.
-
Create a vector for each school (1..m) of pairs (power, is_chosen) or indices.
-
For school in 1..m:
- Let students = list of (power, is_chosen).
- total_chosen = count of is_chosen.
- If total_chosen == 0