A team of three programmers is going to play a contest. The contest consists of n problems, numbered from 1 to n. Each problem is printed on a separate sheet of paper. The participants have decided to divide the problem statements into three parts: the first programmer took some prefix of the statements (some number of first paper sheets), the third contestant took some suffix of the statements (some number of last paper sheets), and the second contestant took all remaining problems. But something went wrong — the statements were printed in the wrong order, so the contestants have received the problems in some random order.
The first contestant has received problems a_{1, 1}, a_{1, 2}, ..., a_{1, k_1}. The second one has received problems a_{2, 1}, a_{2, 2}, ..., a_{2, k_2}. The third one has received all remaining problems (a_{3, 1}, a_{3, 2}, ..., a_{3, k_3}).
The contestants don't want to play the contest before they redistribute the statements. They want to redistribute them so that the first contestant receives some prefix of the problemset, the third contestant receives some suffix of the problemset, and the second contestant receives all the remaining problems.
During one move, some contestant may give one of their problems to other contestant. What is the minimum number of moves required to redistribute the problems?
It is possible that after redistribution some participant (or even two of them) will not have any problems.
Input
The first line contains three integers k_1, k_2 and k_3 (1 ≤ k_1, k_2, k_3 ≤ 2 ⋅ 10^5, k_1 + k_2 + k_3 ≤ 2 ⋅ 10^5) — the number of problems initially taken by the first, the second and the third participant, respectively.
The second line contains k_1 integers a_{1, 1}, a_{1, 2}, ..., a_{1, k_1} — the problems initially taken by the first participant.
The third line contains k_2 integers a_{2, 1}, a_{2, 2}, ..., a_{2, k_2} — the problems initially taken by the second participant.
The fourth line contains k_3 integers a_{3, 1}, a_{3, 2}, ..., a_{3, k_3} — the problems initially taken by the third participant.
It is guaranteed that no problem has been taken by two (or three) participants, and each integer a_{i, j} meets the condition 1 ≤ a_{i, j} ≤ n, where n = k_1 + k_2 + k_3.
Output
Print one integer — the minimum number of moves required to redistribute the problems so that the first participant gets the prefix of the problemset, the third participant gets the suffix of the problemset, and the second participant gets all of the remaining problems.
Examples
Input
2 1 2
3 1
4
2 5
Output
1
Input
3 2 1
3 2 1
5 4
6
Output
0
Input
2 1 3
5 6
4
1 2 3
Output
3
Input
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
In the first example the third contestant should give the problem 2 to the first contestant, so the first contestant has 3 first problems, the third contestant has 1 last problem, and the second contestant has 1 remaining problem.
In the second example the distribution of problems is already valid: the first contestant has 3 first problems, the third contestant has 1 last problem, and the second contestant has 2 remaining problems.
The best course of action in the third example is to give all problems to the third contestant.
The best course of action in the fourth example is to give all problems to the second contestant.
We need to solve a problem: we have three sets of problem numbers, each originally assigned to a participant (1,2,3). The total problems are n = sum of k_i, numbers from 1 to n exactly once each (since each problem taken by exactly one participant, and all numbers 1..n appear). We need to rearrange the assignment so that participant 1 gets a prefix (some first problems), participant 3 gets a suffix (some last problems), and participant 2 gets the middle (the rest). Participants can exchange problems via moves: each move, a contestant may give one of their problems to another contestant. Essentially, we want to minimize the number of transfers. Since a move transfers a problem from one person to another, we can think of the final assignment: we want to assign each problem to a participant 1,2,3 according to a pattern: there exist integers x and y (0 <= x <= n, 0 <= y <= n, x+y <= n) such that participant 1 gets problems {1,2,...,x}, participant 2 gets problems {x+1, ..., n-y}, participant 3 gets problems {n-y+1, ..., n}. (Note that x can be 0, y can be 0, and x+y can be n leaving participant 2 empty). The initial assignment is given. We need to change assignment to this pattern with minimal number of moves. Since each move transfers a problem from one person to another, the number of moves equals the number of problems that need to change owner (since each problem that is initially assigned to a participant different from its final desired participant must be moved at least once). However, sometimes you might move a problem multiple times? But we can always rearrange so that each problem moves at most once: if a problem is with a participant who is not its final owner, we can directly give it to the final owner in one move. There's no need for intermediate transfers because all participants can directly give to any other. So the minimal number of moves equals the number of problems that are initially not in their correct final place, assuming we choose the best final partition (x,y). But careful: if a problem is initially with the correct final owner, we don't need to move it. If it is with a wrong owner, we need at least one move. Since we can always achieve the target with exactly that many moves (by moving each misplaced problem directly), the problem reduces to: find a partition (x, y) that minimizes the number of problems that are initially in the wrong set according to that partition.
Let us denote initial assignments: for each problem number p (1..n), we know its initial owner: either 1,2, or 3. Let owner(p) ∈ {1,2,3}.
For a given partition defined by x (size of prefix for participant 1) and y (size of suffix for participant 3), the desired final owner is:
If p ≤ x: should be 1.
If p > n-y: should be 3.
Else: should be 2.
We want to minimize the count of p such that owner(p) ≠ desired(p). That is minimize the mismatches.
Thus we need to compute for all possible x,y the number of positions that are correct, and find max correct, then answer = n - max correct (since moves = mismatches). Equivalent to minimize mismatches.
Constraints: n up to 2e5, so O(n log n) or O(n) is fine.
We need to efficiently evaluate for each possible partition the number of correctly placed problems.
Define arrays: For each p, we have initial owner. Let's create arrays: for each p (1-indexed), we have a value A[p] = owner.
We need to choose x and y such that x >=0, y>=0, x+y <= n.
For each p, define desired based on x,y: if p <= x then desired=1; elif p > n-y then desired=3; else desired=2.
Thus a problem p is correctly placed if A[p] equals the desired owner for that (x,y).
We can think of scanning possible x and compute something about y.
Alternate viewpoint: The assignment of problems to participants should be like: segment for 1, then segment for 2, then segment for 3. So we are essentially permuting the problems into three contiguous blocks according to the desired owner. The initial assignment gives each problem an owner. So we want to assign a block partition (three intervals) to minimize mismatches. This is similar to "minimum moves to sort colors" or "minimum changes to make array consist of three types in order". Actually it's a classic problem: given an array of labels (1,2,3) of length n, we want to reorder the array into non-decreasing order (like 1's, then 2's, then 3's) with minimum number of element swaps? But here we can only move items between participants, not swap positions. However, since we are free to assign any problem to any participant as long as final set is contiguous, we are essentially allowed to reassign owners arbitrarily, but with the constraint that the final owner pattern must be the three blocks. This is exactly: we want to find a way to partition the indices (positions) into three blocks (prefix, middle, suffix) to maximize the number of positions where the label matches the block's intended owner.
This can be solved by dynamic programming or prefix-suffix computations.
Let f(x) = number of p ≤ x with A[p] = 1 (these are correctly placed in prefix if we set first block size x and all those should be 1). But careful: For prefix, desired is 1, so correct positions in prefix are those where A[p]=1. For the middle block (positions x+1 .. n-y), desired is 2, so correct positions are those with A[p]=2. For suffix (positions n-y+1 .. n), desired is 3, so correct are those with A[p]=3.
Thus total correct = (count of 1s in prefix) + (count of 2s in middle) + (count of 3s in suffix).
We can write: For given x and y, total correct = prefix1[x] + (pref2[n-y] - pref2[x]) + (total3 - pref3[n-y])? Actually suffix 3 count = number of 3s in positions > n-y = total3 - pref3[n-y] (where pref3[i] = count of 3s in first i positions). Similarly, count 2s in middle = pref2[n-y] - pref2[x].
But note: total correct = (number of 1s in positions 1..x) + (number of 2s in positions x+1 .. n-y) + (number of 3s in positions n-y+1..n). Let's define prefix arrays:
Let cnt1[i] = number of 1s among first i positions.
cnt2[i] = number of 2s among first i positions.
cnt3[i] = number of 3s among first i positions.
But careful: total3 is constant. So maximize correct = maximize (cnt1[x] - cnt2[x] + cnt2[n-y] - cnt3[n-y]).
Alternatively, we can precompute something like for each possible cut between blocks, we want to maximize sum of certain prefix and suffix contributions.
Let’s define for each index i (0..n), we consider the boundary between block1 and block2 at i: first block = positions 1..i (size i). Then the remaining positions i+1..n will be split into block2 and block3 by some boundary j (where block3 starts at position n-y+1? Better to think of suffix length y, i.e., block3 = positions n-y+1..n, block2 = i+1..n-y). So we have i (size of block1) and s = n-y (the last index of block2). Then s must be >= i, and s can range from i to n (since y >=0, s <= n). Actually y = n - s, so s = n - y, the index of the last element of block2. Then block2 is from i+1 to s inclusive. So we need 0 <= i <= s <= n.
We can define A[i] = cnt1[i] - cnt2[i]; B[s] = cnt2[s] - cnt3[s]. Then correct = A[i] + B[s] + total3, with constraint 0 <= i <= s <= n.
We want to maximize correct, so maximize A[i] + B[s] subject to i <= s.
Since total3 constant, answer = n - (max_{0 <= i <= s <= n} (A[i] + B[s]) + total3) = n - total3 - max_{i<=s} (A[i] + B[s]).
But careful: n - total3 - max(...) gives moves? Let's compute: moves = n - max_correct. So moves = n - (max_{i<=s} (A[i] + B[s]) + total3) = (n - total3) - max_{i<=s} (A[i] + B[s]).
Alternatively we can precompute something to find max over pairs i<=s of A[i] + B[s]. That can be done by scanning s from 0 to n, keeping the best A[i] for i <= s, then compute A_best_up_to_s + B[s] and take max.
We need to define A[i] and B[s] for i,s from 0 to n. For i=0, cnt1[0]=0, cnt2[0]=0, so A[0]=0. For i=n, etc.
Similarly B[s] = cnt2[s] - cnt3[s].
But careful: The definitions assume cnt2[s] is number of 2's in positions 1..s? Yes. And cnt3[s] similarly. That's fine.
So we can compute prefix sums of counts for each type. Since n up to 2e5, easy.
One more nuance: The participants may end up with empty sets. That corresponds to i=0 (no prefix) or y=0 (no suffix) or i=s (block2 empty) etc. So our ranges include all.
Now, after computing max_{i<=s} (A[i]+B[s]), we can get answer.
But is this correct? Let's test on examples.
Example 1:
k1=2, k2=1, k3=2. Problems:
1: 3,1 -> Actually first participant got problems: 3 and 1. So initial owners: problem1 owned by? Let's list all problems: n=5. Input: first line: 2 1 2; second: 3 1; third: 4; fourth: 2 5. So owners: participant1 has {3,1}; participant2 has {4}; participant3 has {2,5}. So owner array: p=1:1? Wait problem1 is owned by participant1 (since 1 is in list of participant1). p=2:3; p=3:1; p=4:2; p=5:3. So A = [1,3,1,2,3] (index 1..5). Now compute total3 = count of 3s = p2,p5 -> 2.
Max is 2 (i=3,s=4). Then max_correct = max(A+B) + total3 = 2 + 2 = 4. n=5, moves = 5-4=1. Matches output 1.
Great.
Example 2:
k1=3, k2=2, k3=1. Owners: first: 3,2,1; second:5,4; third:6. So n=6. Owners: p1:1, p2:1? Wait first participant has 3,2,1: so problems 1,2,3? Actually numbers: first list: 3,2,1. So participant1 has problems 1,2,3. participant2 has 5,4 => problems 4,5. participant3 has 6 => problem6. So A: p1=1, p2=1, p3=1, p4=2, p5=2, p6=3.
Max = 5 (i=3,s=5). Then max_correct = 5 + total3=5+1=6. moves = 6-6=0. Output 0. Good.
Example 3:
k1=2,k2=1,k3=3. Owners: first:5,6; second:4; third:1,2,3. So n=6. Owners: p1:3? Actually third participant has 1,2,3, so p1=3, p2=3, p3=3. p4=2 (second), p5=1, p6=1. So A: [3,3,3,2,1,1]. total3 = count of 3's = 3.
Now compute max A[i]+B[s] i<=s.
We can compute best = max over s of (max_{i<=s} A[i] + B[s]).
Scan:
s=0: i=0 ->0; max=0.
s=1: best i=0..1: max A: max(0,0)=0; +B[1]=-1 => -1.
s=2: max A up to 2: still 0; + -2 = -2.
s=3: 0-3=-3.
s=4: max A up to4: max over i=0..4: A values: 0,0,0,-1,0? Actually A[0]=0,A[1]=0,A[2]=0,A[3]=0,A[4]=-1 => max=0; +B[4]=-2 => -2.
s=5: max A up to5: A[5]=0, so max=1? Actually check: A[0]=0, A[1]=0, A[2]=0, A[3]=0, A[4]=-1, A[5]=0 => max=0? Wait A[5]=0, so max still 0. +B[5]=-2 => -2.
s=6: max A up to6: A[6]=1, so max =1; +B[6]=-2 => -1.
So max A+B = 0? Actually we have 0 at s=0. Also maybe i=0,s=0 gives 0. So max = 0. Then max_correct = 0 + total3 = 3. n=6, moves = 6-3=3. Output 3 matches.
Example 4:
k1=1,k2=5,k3=1. Owners: first:6; second:5,1,2,4,7; third:3. n=7. Owners: p1:2? Actually second has 5,1,2,4,7: so p1=2, p2=2, p3=3, p4=2, p5=2? Wait second list: 5,1,2,4,7. So problems 1,2,4,5,7 belong to participant2? Actually numbers: 1,2,4,5,7. Participant1 has 6, participant3 has 3. So owners: p1=2, p2=2, p3=3, p4=2, p5=2? Need to ensure p5: value? Actually we have numbers 1..7:
p1:1 -> participant2 (since 1 in second list)
p2:2 -> participant2 (2 in second list)
p3:3 -> participant3 (3 in third list)
p4:4 -> participant2 (4 in second list)
p5:5 -> participant2 (5 in second list)
p6:6 -> participant1 (6 in first list)
p7:7 -> participant2 (7 in second list)
Thus A = [2,2,3,2,2,1,2].
total3 = count of 3's = 1 (p3).
Now compute max_{i<=s} A[i]+B[s].
We can scan s from 0 to 7:
s=0: max A upto0=0, sum=0.
s=1: max A upto1: max(0,-1)=0; + B[1]=1 => 1.
s=2: max A upto2: max(0,-1,-2)=0; + B[2]=2 => 2.
s=3: max A upto3: max(0,-1,-2,-2)=0; + B[3]=1 => 1.
s=4: max A upto4: max(0,-1,-2,-2,-3)=0; + B[4]=2 => 2.
s=5: max A upto5: max(0,-1,-2,-2,-3,-4)=0; + B[5]=3 => 3.
s=6: max A upto6: max(0,-1,-2,-2,-3,-4,-3)=0; + B[6]=3 => 3.
s=7: max A upto7: max(0,-1,-2,-2,-3,-4,-3,-4)=0; + B[7]=4 => 4.
Thus max = 4. Then max_correct = 4 + total3 = 4+1=5. n=7, moves=7-5=2. Output 2 matches.
All examples check out.
So algorithm:
Read k1,k2,k3.
Read three lists; combine into owner array of length n = k1+k2+k3. We need to assign owner for each problem number p.
Approach: create an array owner of size n+1 (1-indexed) initialized to 0. Then read first list of length k1, for each x set owner[x] = 1. Similarly second list set owner[x] = 2, third set owner[x] = 3.
Ensure that each number appears exactly once? But input guarantee, so we can just assign.
Compute arrays A[i] = cnt1[i] - cnt2[i] for i=0..n.
Compute B[i] = cnt2[i] - cnt3[i] for i=0..n.
Find max_{0 <= i <= s <= n} (A[i] + B[s]).
Traverse s from 0 to n, maintain best_A = max_{i <= s} A[i]. Then current = best_A + B[s]. Keep global maximum.
Let max_sum = global max (maybe negative? But at least 0 because i=s=0 gives 0). So non-negative.
max_correct = max_sum + total3.
answer = n - max_correct.
Print answer.
Complexity O(n).
Edge cases: n up to 200k, fine.
But is this derivation sound? Let's double-check the derivation. The correct count is cnt1[x] + (cnt2[n-y] - cnt2[x]) + (total3 - cnt3[n-y]), with x = i and s = n-y. That is correct because middle is positions x+1 to s, suffix is s+1 to n. Wait careful: we defined s = n-y, so y = n-s. Then suffix is positions s+1 .. n. So count of 3s in suffix = total3 - cnt3[s]. So correct = cnt1[i] + (cnt2[s] - cnt2[i]) + (total3 - cnt3[s]) = cnt1[i] - cnt2[i] + cnt2[s] - cnt3[s] + total3. Yes.
Thus A[i] = cnt1[i] - cnt2[i], B[s] = cnt2[s] - cnt3[s]. Works for i=0 and s=0.
Now, is there any scenario where the optimal partition might have i > s? That would mean the prefix and suffix overlap or the middle negative, but not allowed. So constraint i <= s.
Thus algorithm is correct.
But we need to be careful: The problem statement allows that after redistribution some participant may have no problems. That corresponds to i=0 (no prefix) or i=n (all prefix, middle and suffix empty? Actually if i=n, then s must be >= i, so s=n. Then block2 is from n+1 to s? empty, block3 from s+1..n empty, so only participant1 gets all. That's allowed because participant2 and 3 empty. Similarly y=0 (s=n) allowed.
Thus our range covers all.
Potential pitfalls: Input reading: The three lines each contain variable lengths. Need to read integers accordingly.
Implementation:
We'll use vectors for owner size n+1 (0-index? but easier 1-indexed). We'll fill with 0, then set owner[x]=1 for first list, then for second list set owner[x]=2, third set 3.
Alternatively we can just create an array of length n+1 and assign.
Now compute prefix arrays as int arrays of size n+1. Use long? Values up to n, so int is fine.
Now step 6: Iterate s from 0 to n, maintain best_A = max_{i<=s} A[i]. Initially best_A = A[0] = 0. For each s, update best_A = max(best_A, A[s]) after computing current sum? Actually we need best_A for i <= s. So we can set best_A = max(best_A, A[s]) after using it? If we compute current = best_A + B[s] using best_A from indices 0..s-1? But we should also allow i=s, so before computing current we should update best_A to include A[s]? Let's define: When we are at s, we want max over i in [0,s] of A[i] + B[s]. That's equal to B[s] + max_{i in [0,s]} A[i]. So we can first update best_A = max(best_A, A[s]), then current = best_A + B[s]. Or we can compute current before updating, but then we would miss i=s. So after updating is correct. However, careful: best_A initially = A[0] (since s=0, i=0 only). At s=0: update best_A = max(previous, A[0]) = A[0]; current = best_A + B[0] = 0+0=0. Good. Then for s=1: update best_A = max(previous best_A, A[1]); then current = best_A + B[1]. That includes i=1. So that's correct.
So algorithm:
best_A = A[0]; // i=0
max_sum = best_A + B[0]; // s=0
for s from 1 to n:
best_A = max(best_A, A[s]);
current = best_A + B[s];
if current > max_sum: max_sum = current;
But note: we need to include s=0 in loop, but best_A already includes A[0]. So we can either start loop at s=0, but then we need to avoid updating best_A before computing for s=0. Simpler: as above, initialize best_A = A[0]; max_sum = best_A + B[0]; for s = 1..n: best_A = max(best_A, A[s]); current = best_A + B[s]; update max_sum.
Alternatively, loop s from 0 to n, with best_A = -inf initially, and within loop first best_A = max(best_A, A[s]) then compute current. That works for s=0: best_A becomes A[0] (since initially -inf). So that's fine.
We'll just do loop from 0 to n:
int best_A = -1e9; // but A[0]=0, and values may be negative, so we need a very low number. But we can set best_A = A[0] before loop and then for s from 0 compute? Actually let's do:
int best_A = -1e9;
int max_sum = -1e9;
for (int s = 0; s <= n; ++s) {
best_A = max(best_A, A[s]);
int cur = best_A + B[s];
if (cur > max_sum) max_sum = cur;
}
That works because at s=0, best_A becomes max(-inf, A[0]) = A[0]; cur = A[0]+B[0]; then max_sum set.
But need to be careful if n=0? Not possible because each ki >= 1? Actually each ki >=1 according to constraints? It says 1 ≤ k_1, k_2, k_3 ≤ 2*10^5, so all positive. So n>=3. So it's fine.
Now after we get max_sum, compute ans = n - (max_sum + total3). However, max_sum could be negative? But at worst, i=s=0 gives 0, so max_sum >= 0. But if we set best_A to -inf, but we include s=0, it will become 0, so max_sum will be at least 0. So okay.
Now complexity O(n) time and O(n) memory for prefix arrays. But we don't need to store all prefix arrays; we can compute A and B on the fly? Possibly we need both A and B for all indices? But we can compute while scanning s? However, we need A[s] for updating best_A and B[s] for computing current. But also we need to know A[s] for s from 0 to n. That can be computed as we iterate through positions from 1..n, we can compute cumulative counts and store A and B. Since n is 200k, storing arrays of size n+1 is fine.
But we can further compress: since we only need A[s] and B[s] once while scanning s, we can compute them on the fly from prefix counts, but we need to update best_A based on A[s] before using B[s] for current. So we could compute cumulative cnt1, cnt2, cnt3 as we go, and compute A and B. That's also O(1) memory if we don't store all. However, we need to do the max_{i<=s} A[i] + B[s], which requires us to know A[s] for current s, and we need to know best_A which is the max of all A up to s. But we don't need to store all A values if we just update best_A as we go. So we can compute prefix counts iteratively, and at each step s compute A = cnt1 - cnt2, B = cnt2 - cnt3, update best_A, compute cur, update max_sum. Then we can forget A after that. So we can do it in O(1) extra space beyond input? The input we have owner array of size n+1, but we could even avoid storing owner entirely by processing input as we read. But we need to assign owner for each problem number, and then iterate p from 1 to n to compute prefix counts. So we can store owner array (size n+1) as ints. That's okay.
But we can also read all numbers, build owner array, then compute. Memory: owner array of size ~200001 integers (maybe 4 bytes each) plus a few arrays of same size for prefix counts if we store. If we want to avoid storing prefix arrays, we can compute on the fly. Let's consider: we need to compute cnt1, cnt2, cnt3 cumulatively as we go from p=1 to n. At each p we can update counts, then after processing p (i.e., at index s = p) we can compute A and B and do the update. But careful: we also need to consider i=0 before any p, which corresponds to s=0. So we need to handle s=0 separately. Then for each p from 1 to n, we update counts and compute for s=p. That works.
So we can do:
owner array of length n+1, initialize to 0.
read lists and set owner.
Then:
int cnt1=0, cnt2=0, cnt3=0;
int best_A = 0; // for i=0, A[0]=0
int max_sum = best_A + (0 - 0)??? Actually B[0] = cnt2 - cnt3 = 0-0=0. So max_sum = 0 initially.
Then for p = 1 to n:
if (owner[p] == 1) cnt1++;
else if (owner[p] == 2) cnt2++;
else cnt3++;
// now at s = p:
int A_cur = cnt1 - cnt2;
int B_cur = cnt2 - cnt3;
best_A = max(best_A, A_cur); // because i can be any index up to s, including current.
int cur = best_A + B_cur;
if (cur > max_sum) max_sum = cur;
But careful: In the above, best_A is updated before using B_cur? That's correct: we want max over i <= s. At s, we consider i = s as well, so we need to include A_cur in best_A before computing cur. So we do best_A = max(best_A, A_cur) then cur = best_A + B_cur. This yields max_{i<=s} A[i] + B[s]. Good.
But is it correct that best_A initially is A[0]? Yes. At p=0, we handled separately: best_A = 0 (A[0]). max_sum = 0 + B[0] = 0 (since B[0]=0). Then for each p, we update.
Now, after loop, max_sum is computed. However, we also need to consider s = n? The loop goes to p = n, so s = n is included. Good.
Then total3 = cnt3 (after loop). So ans = n - (max_sum + total3).
But note: total3 is the final cnt3 after processing all n. Yes.
Thus we can implement without storing prefix arrays explicitly, just need owner array.
Memory: owner array of size n+1 (int), okay.
Alternatively, we could even avoid storing owner by processing input into a combined list and sorting? But that seems unnecessary.
But there is a nuance: The numbers are from 1 to n, each exactly once, but the input provides three lists; we need to map each number to its original owner. So we need to fill owner array.
Time: O(n) reading and filling.
Potential issues: The indices are 1-based. Ensure to handle large input quickly.
Now, is there any alternative interpretation? Could there be a scenario where a problem moves twice to reduce total moves? For example, if we have a cycle: problem a belongs to participant 1 but should be with 2, problem b belongs to participant 2 but should be with 1. If we just move each directly, that's 2 moves. Could we do better by exchanging? Since a move is giving a problem from one to another, you could give a from 1 to 2, and b from 2 to 1; that's still two moves. If you tried to move a from 1 to 2, and then later move something else, it's still two. There's no scenario where moving a problem twice reduces total moves because each misplaced problem must end up with correct owner; moving it multiple times would only increase moves. So minimal moves = number of problems that are not already in correct final position.
Thus the problem reduces to maximum number of correctly placed problems after choosing partition.
Hence algorithm correct.
Now implement.
Implementation Steps:
Read integers k1,k2,k3.
n = k1+k2+k3.
Create vector owner(n+1, 0); // index 1..n.
For i in 1..3: read a list of integers of appropriate length. For first list, each value v: owner[v] = 1; for second: owner[v] = 2; for third: owner[v] = 3. We'll read three lines separately.
After reading, we can verify each number appears once, but not needed.
Initialize cnt1=0, cnt2=0, cnt3=0.
best_A = 0; (since A[0] = 0)
max_sum = 0; (best_A + B[0] = 0)
Loop p from 1 to n:
if (owner[p] == 1) cnt1++;
else if (owner[p] == 2) cnt2++;
else cnt3++; // must be 3
int A_cur = cnt1 - cnt2;
int B_cur = cnt2 - cnt3;
// update best_A with A_cur
if (A_cur > best_A) best_A = A_cur;
int cur = best_A + B_cur;
if (cur > max_sum) max_sum = cur;
total3 = cnt3.
ans = n - (max_sum + total3);
Print ans.
Double-check the definitions: At step p, we have processed positions 1..p. So at s=p, A_cur = cnt1[p] - cnt2[p], B_cur = cnt2[p] - cnt3[p]. best_A = max_{i <= p} A[i]. So cur = best_A + B_cur is the maximum of A[i]+B[p] for i<=p. So max_sum is max over s of that. Good.
Now note: Could there be a scenario where the optimal partition has i > s? That is not allowed. In our method, for each s we only consider i <= s, so that's fine.
Also we need to ensure that the partition i,s yields a valid distribution: i <= s <= n. Yes.
One more nuance: The middle block may be empty (i = s). That's allowed because participant2 may have no problems. Similarly prefix empty i=0, suffix empty s=n. Our method includes those.
Now test edge cases:
All participants have random assignments, maybe optimal is to give all to participant1 (i=n, s=n). In that case, correct = cnt1[n] + (cnt2[n]-cnt2[n]) + (total3 - cnt3[n])? Actually i=n, s=n: correct = cnt1[n] + (cnt2[n]-cnt2[n]) + (total3 - cnt3[n]) = cnt1[n] + 0 + total3 - cnt3[n] = cnt1[n] + (total3 - cnt3[n]). Since total3 = cnt3[n], this is cnt1[n]. So correct = number of 1s. That is if we give all to participant1, only the 1s are correctly placed. But we may also consider giving all to participant2: i=0, s=n? Actually all to participant2: i=0, s=n -> correct = cnt1[0] + (cnt2[n] - cnt2[0]) + (total3 - cnt3[n]) = 0 + cnt2[n] + 0 = cnt2[n]. That's number of 2s. Or all to participant3: i=0, s=0? Wait all to participant3: i=0, s=0 (since prefix empty, middle empty, suffix all): correct = cnt1[0] + (cnt2[0]-cnt2[0]) + (total3 - cnt3[0]) = total3 = cnt3[n]. So correct = number of 3s. So we cover those.
Thus our algorithm covers all possibilities.
Now, let's think about the correctness of the derivation of A and B. There's also alternative approach: compute minimal moves = n - max over x,y of correct. Our method yields that.
Thus solution is simple.
But we need to be careful about integer types: n up to 200k, all counts fit in int, but some subtractions may yield negative values, but that's okay. best_A might be negative? Starting from 0, A_cur may become negative, but best_A remains non-negative (since we only update if larger). Actually best_A might stay 0 even if all A_cur are negative because we start at 0 and we only update when A_cur > best_A. So best_A never drops below initial 0. But is it possible that the max sum A[i]+B[s] is achieved with i=0 (A[0]=0) even if all A[i] negative? Then best_A=0 works. However, is it possible that the optimal partition uses i such that A[i] is negative? It could be that adding a negative A[i] might be beneficial if B[s] is large positive? But then if all A[i] are negative, the maximum sum might be achieved with i=0 (A=0) because negative would reduce sum. So best_A should be the maximum A[i] over i<=s, which is at least 0 because A[0]=0. So best_A is always >=0. Therefore our initialization best_A=0 works. However, if we update best_A = max(best_A, A_cur), and A_cur can be negative, best_A remains 0. Good.
But what about s=0? B[0]=0, sum=0. So max_sum >= 0.
Thus no issues.
Now, we must verify that our calculation of max_sum indeed captures the maximum over i<=s of A[i] + B[s] including the case i=0 and s=0. The loop p from 1..n covers s from 1..n. s=0 we initialized with max_sum=0 (i=0). So fine.
Potential bug: When we update best_A before cur, we include A_cur for the same s. That's correct because i can equal s. So our max_sum includes the case i=s.
Now check with an example where i=0 is not optimal but i>0 is, we covered.
Now implement.
But we need to read three lines: after reading k1,k2,k3, there are three lines each containing the lists. However, input format: the lines are provided exactly as described. We need to read them accordingly. Use typical input reading.
C++ code:
code
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int k1, k2, k3;
cin >> k1 >> k2 >> k3;
int n = k1 + k2 + k3;
vector<int> owner(n+1, 0); // 1-indexed
// read first list
for (int i = 0; i < k1; i++) {
int x;
cin >> x;
owner[x] = 1;
}
// read second list
for (int i = 0; i < k2; i++) {
int x;
cin >> x;
owner[x] = 2;
}
// read third list
for (int i = 0; i < k3; i++) {
int x;
cin >> x;
owner[x] = 3;
}
int cnt1 = 0, cnt2 = 0, cnt3 = 0;
int best_A = 0; // A[0] = 0
int max_sum = 0; // for s=0, A[0]+B[0] = 0
for (int p = 1; p <= n; p++) {
if (owner[p] == 1) cnt1++;
else if (owner[p] == 2) cnt2++;
else cnt3++;
int A_cur = cnt1 - cnt2;
int B_cur = cnt2 - cnt3;
if (A_cur > best_A) best_A = A_cur;
int cur = best_A + B_cur;
if (cur > max_sum) max_sum = cur;
}
int total3 = cnt3;
int ans = n - (max_sum + total3);
cout << ans << endl;
return 0;
}
Check with given examples. Should output correct.
But wait, we didn't handle the case where owner[p] might not be set? But input guarantee ensures each number 1..n appears exactly once, so after reading three lists, owner array will be fully set. So else condition works.
Now complexity O(n) time, O(n) space for owner. Could we avoid owner array and compute directly? For example, we could read all numbers into a single array and sort? But we need to know which participant each number belongs to. Maybe we can store all numbers in three separate vectors and then later combine? But we need to compute owner for each p sequentially. We could create an array of size n+1 and set values by reading. That's fine.
However, we can also combine reading to fill owner directly: read first list, assign 1; read second list, assign 2; read third list, assign 3. That's exactly we are doing.
Now, could there be any pitfalls with large input? Use fast I/O.
Now, let's double-check logic with some random tests, maybe small n to ensure no off-by-one.
Write small brute-force for n up to 10 and compare.
But from reasoning and examples, it's correct.
Now, let's think about potential alternative scenarios: Suppose the optimal partition uses i > s? Not allowed because then middle would be negative size. So we are fine.
Now, final answer.
But wait: The problem statement says "the first programmer took some prefix of the statements (some number of first paper sheets), the third contestant took some suffix of the statements (some number of last paper sheets), and the second contestant took all remaining problems." This implies that the first took a prefix, third took a suffix, second took the middle. In our model, we allowed the first to take empty prefix, third empty suffix, second empty middle. That's fine.
Now, about moves: "During one move, some contestant may give one of their problems to other contestant." That's exactly what we assumed.
Thus solution done.
Now, the problem may also be solved by considering DP on three states for each position? But our method is simpler.
Let's check if there is any possibility that the optimal partition does not correspond to integer x and y exactly at indices? Since problems are numbered consecutively, the partition is based on the numbers, so yes.
Thus final.
Now, implement and submit.
But before finalizing, we should also test edge cases like:
Case: All numbers are owned by participant2: So owner array all 2. Then best partition should be give all to participant2: i=0,s=n, correct = cnt2[n] = n. So moves = n - n = 0. Our algorithm: Process:
init best_A=0,max_sum=0.
For each p: cnt2 increments; A_cur = 0 - cnt2 (negative), B_cur = cnt2 - 0 = cnt2. best_A stays 0, cur = 0 + cnt2. So after p=n, max_sum = cnt2[n] = n? Actually let's simulate n=3: p=1: cnt2=1, A=-1, B=1, best_A=0, cur=1, max_sum=1. p=2: cnt2=2, A=-2, B=2, cur=2, max_sum=2. p=3: cnt2=3, A=-3, B=3, cur=3, max_sum=3. So max_sum = n, total3=0, ans = n - (n+0)=0. Good.
Case: All numbers owned by participant1: Then optimal partition is give all to participant1: i=n,s=n, correct = cnt1[n] = n. Our algorithm: Process: each p: cnt1 increments; A_cur = cnt1 - 0 = cnt1; B_cur = 0 - 0 = 0. best_A updates to cnt1 gradually; cur = best_A + 0 = cnt1. So after p=n, max_sum = cnt1[n] = n. total3=0, ans=n - n = 0. Good.
Case: All numbers owned by participant3: Then optimal: all to participant3 (i=0,s=0): correct = total3 = n. Process: p=1: cnt3=1; A=0-0=0? Actually cnt1=0,cnt2=0,cnt3=1: A=0, B=0-1=-1; best_A=0, cur=-1, max_sum stays 0. p=2: cnt3=2; A=0, B=-2, cur=-2, max_sum 0. p=n: max_sum=0. total3=n, ans = n - (0+n) = 0. Good.
Case: Mixed but with negative contributions: Suppose n=4, owners: [2,1,1,2] (p1=2,p2=1,p3=1,p4=2). What is optimal? Let's brute: n=4. All partitions (i,s) with i<=s<=4. We'll compute correct:
i=4,s=4: cnt1[4]=2 (p2,p3) =2? Actually p4=2, so cnt1[4]=2; correct = 2.
So maximum correct = 3 at (i=3,s=4). That corresponds to prefix size 3 (problems 1,2,3) to participant1? Actually i=3 means first block contains 1..3, and s=4 means middle ends at 4 (so suffix empty). That gives: participant1 gets 1..3, participant2 gets 4, participant3 gets none. Check owners: p1=2 (should be 1) mismatch; p2=1 (should be 1) correct; p3=1 (should be 1) correct; p4=2 (should be 2) correct. So only p1 wrong, correct 3. So moves=1. Our algorithm: compute:
owner: [2,1,1,2]
Process:
p=1: cnt2=1; A=-1; B=1; best_A=0, cur=1, max_sum=1.
p=2: cnt1=1; A=1-1=0; B=1-0=1; best_A=0? Actually best_A = max(0,0)=0; cur=0+1=1; max_sum stays 1.
p=3: cnt1=2; A=2-1=1; B=1-0=1; best_A = max(0,1)=1; cur=1+1=2; max_sum=2.
p=4: cnt2=2; A=2-2=0; B=2-0=2; best_A=1; cur=1+2=3; max_sum=3.
total3=0, ans=4-3=1. Good.
Thus works.
Now, we should also consider the possibility that the maximum sum A[i]+B[s] is achieved for some s that is not n. Already covered.
One more nuance: In our loop we update best_A before computing cur, but the best_A after updating includes A_cur. That's correct for i <= s. So fine.
Now, what about i=0? best_A includes A[0] initially, but as we go, best_A may increase above 0. At s>0, best_A includes max over i=0..s, so includes i=0. Good.
Thus the code is correct.
Now, we should also consider alternative approach: dynamic programming where we compute the minimal moves directly. But this is fine.
Now, is there any hidden constraint like the final distribution must have at least one problem for each? The problem statement says "It is possible that after redistribution some participant (or even two of them) will not have any problems." So empty sets allowed.
Thus answer is as computed.
Now, let's test with some random data by brute force for small n to ensure no bugs.
Now, test a random n=5, owners = [2,2,2,2,2] we already did.
Now, test a case where the optimal partition includes a middle block that is not simply the set of 2's, but some 1's may be placed in the middle incorrectly? But we are maximizing correct; it's fine.
Now, think about potential pitfalls: the computation of B_cur = cnt2 - cnt3. But earlier we derived B[s] = cnt2[s] - cnt3[s]. Yes.
Now, let's re-express: The number of correct problems in the suffix is total3 - cnt3[s]. That's correct because suffix starts at s+1.
But wait, our formula for correct used s = n - y, where y is suffix length. So suffix = positions s+1..n. So number of 3's in suffix = total3 - cnt3[s]. That's correct.
Thus the formula stands.
Now, one more check: In the iterative process, at p (which is s), we compute B_cur = cnt2 - cnt3. But for s, we want B[s] = cnt2[s] - cnt3[s]. That's exactly what we compute. So it's consistent.
Now, note that B_cur may be negative. That's okay.
Now, final answer.
Time complexity: O(n).
Now, let's reflect if there is any simpler interpretation: The problem can be thought as: We have three segments we can assign, and we want to maximize the sum of matches. This is essentially a classic "three-way partition" problem that can be solved with prefix-suffix DP.
Now, we can finalize.
But hold on: The problem description: "the first programmer took some prefix of the statements (some number of first paper sheets), the third contestant took some suffix of the statements (some number of last paper sheets), and the second contestant took all remaining problems." This implies that the first took a prefix (contiguous from 1), third took a suffix (contiguous from the end), and second took the middle (contiguous). That's exactly our model.
Thus solution is correct.
Now, we'll write the code accordingly.
We should be careful about reading input: after reading k1,k2,k3, we might need to read the next lines which contain exactly k1, k2, k3 numbers. We'll just use standard cin.
Potential improvement: Since we only need owner array, we can avoid storing the three lists separately. We'll just fill owner as we read.
Now, note: The sum k1+k2+k3 may be up to 2e5, but each ki separately up to 2e5, but sum up to 2e5. So vector of size n+1 is fine.
Now, produce final solution.
But before finishing, I want to check that the initial distribution might have duplicate numbers? Input guarantee: "It is guaranteed that no problem has been taken by two (or three) participants". So no duplicates. Also "each integer a_{i, j} meets the condition 1 ≤ a_{i, j} ≤ n" and sum = n, so they form a permutation of 1..n. So our owner array will have each number exactly once.
Now, edge case: k1, k2, k3 could be large and maybe reading with cin can be slow, but with ios::sync_with_stdio(false) and cin.tie(0) it's fine.
Now, finalize.
Now, write explanation for the solution: We'll describe the transformation into maximizing correct placements, then show derivation of prefix-suffix optimization.
Now, output answer as integer.
Thus final.
But let's double-check the formula for ans: ans = n - (max_sum + total3). Because max_correct = max_sum + total3. Since total3 is constant, we computed max_sum as max_{i<=s} A[i]+B[s]. So ans = n - (max_sum + total3). That's correct.
Now, could there be a scenario where max_sum is computed incorrectly because we used best_A that includes A_cur for the same s? Yes, that's correct.
Now, test a scenario: Suppose the best partition is with i > s? Not allowed.
Now, consider scenario: Suppose we have a large number of 3's, and the optimal partition may have s small (so suffix large). In that case B[s] is cnt2[s] - cnt3[s]; if cnt3[s] is large, B[s] may be negative; but the plus with A[i] may still yield high sum? Actually, if suffix is large, then suffix includes many 3's, correct count from suffix = total3 - cnt3[s]. This is high if cnt3[s] is low (i.e., s small). That term appears as total3 - cnt3[s] = total3 + (-cnt3[s]). But in our expression, we separated total3 constant; the variable part is A[i] + B[s] = cnt1[i] - cnt2[i] + cnt2[s] - cnt3[s]. So if s is small, -cnt3[s] is a large negative? Actually if s is small, cnt3[s] may be small too? Let's think: If s is small, cnt3[s] is count of 3s in early positions. That could be small. But -cnt3[s] is negative or zero. Meanwhile cnt2[s] may be moderate. But the suffix correct term is total3 - cnt3[s] which is large when cnt3[s] is small. That term is included as total3 + (-cnt3[s]) but we are adding total3 later. So it's fine.
Now, one more test to ensure best_A update order is correct: At each s, we want max_{i<=s} A[i]. That includes i=s. So we must update best_A with A_cur before computing cur. In code we did:
if (A_cur > best_A) best_A = A_cur;
cur = best_A + B_cur;
That's correct. However, there is a nuance: What about i that are less than s but not yet considered? best_A holds the max over all i seen so far (including i=0..s-1). After we update with A_cur, it's i=0..s. So cur is max over i=0..s of A[i] + B_cur. Good.
But is there a scenario where best_A from previous steps is not the maximum over i<=s because A_cur might be lower but a previous i yields larger sum with B_cur? That's fine: best_A + B_cur with best_A being the max A[i] among i<=s will give the max sum because B_cur is constant for given s. So we are taking best_A which maximizes A[i]. But wait, we want max_{i<=s} (A[i] + B_cur) = B_cur + max_{i<=s} A[i]. That's exactly what we do. So it's correct.
Now, ensure that we consider i=0 as well. At start, best_A = 0 from initialization (A[0]=0). But what if A[0] is not considered? Actually before loop we set best_A=0 (which is A[0]) and max_sum=0 (A[0]+B[0]=0). But we never explicitly computed B[0] as part of loop? But we set max_sum=0. That's fine. In loop starting from p=1, best_A includes A[0] because we set best_A=0 initially. So it's included.
Now, what about s=0? Already accounted.
Now, maybe we should also consider s from 0 to n inclusive, and our loop only goes from 1 to n for s; s=0 we set max_sum=0. So all good.
Now, one more verification: Suppose n=1. Then k1,k2,k3 are such that one is 1 and others 0? But constraints say each ki >=1, so n>=3. So fine.
Now, I think solution is solid.
But is there any trick with reading? The input lines may have trailing spaces; but reading integers handles that.
Now, final code.
But I recall that sometimes people use a different approach: they compute the minimum number of moves = (k1 - count1_in_prefix) + (k2 - count2_in_middle) + (k3 - count3_in_suffix). This is equivalent.
Now, this problem appears to be Codeforces Round #... (maybe problem "Contest for Robots"? Actually it's Codeforces problem 1251C? No, it's "Minimum moves" perhaps. But anyway.)
Now, I'm comfortable.
Before final answer, maybe we should also consider whether the optimal solution could be to not have the blocks in order 1,2,3? The problem statement says first gets prefix, third gets suffix, second gets remaining. So order is fixed.
Now, final.
Edge Cases:
All numbers are owned by participant2 except one owned by participant1 at position n. What's optimal? For n=5: owners: [2,2,2,2,1]. Let's compute manually: maybe best to assign all to participant2? That would give correct count = 4 (since 4 twos). Moves=1. But perhaps assign prefix to include the 1? If we set i=5 (all to participant1), correct count = 1 (only the 1 at p5). So not better. If we set i=0,s=4: participant2 gets 1..4 (all twos) correct=4, participant3 gets 5 (should be 3) incorrect. So correct=4. If we set i=0,s=5: participant2 gets all, correct=4. So moves=1. Our algorithm should output 1. Let's test mentally:
owners: p1=2,p2=2,p3=2,p4=2,p5=1.
Process:
p1: cnt2=1; A=-1; B=1; best_A=0; cur=1; max_sum=1.
p2: cnt2=2; A=-2; B=2; best_A=0; cur=2; max_sum=2.
p3: cnt2=3; A=-3; B=3; best_A=0; cur=3; max_sum=3.
p4: cnt2=4; A=-4; B=4; best_A=0; cur=4; max_sum=4.
p5: cnt1=1; A=1-4=-3; B=4-0? cnt3=0 -> B=4; best_A = max(0,-3)=0; cur=0+4=4; max_sum stays 4.
total3=0; ans=5-(4+0)=1. Good.
All numbers owned by participant3 except one owned by participant2 at position 1. n=5: owners: [2,3,3,3,3]. Best? Possibly assign all to participant3 gives correct count = 4 (since four 3's). Moves=1. Let's compute:
p1: cnt2=1; A=-1; B=1-0? cnt3=0 -> B=1; best_A=0; cur=1; max_sum=1.
p2: cnt3=1; A=0-1=-1; B=1-1=0; best_A=0; cur=0; max_sum stays1.
p3: cnt3=2; A=-1; B=1-2=-1; cur=-1; max_sum1.
p4: cnt3=3; A=-1; B=1-3=-2; cur=-2; max_sum1.
p5: cnt3=4; A=-1; B=1-4=-3; cur=-3; max_sum1.
total3=4; ans=5-(1+4)=0? Wait 5 - (1+4)=0, but we expected moves=1? Let's recalc: total3=4, max_sum=1, so max_correct = max_sum + total3 = 5? That would mean all problems correctly placed? But we have only four 3's and one 2; is there a partition where all 5 are correct? Let's think: We need a prefix of 1's, middle of 2's, suffix of 3's. We have no 1's. So we could set prefix empty. Then we need to assign the 2 to the middle, and the 3's to the suffix. That would require that the 2 is in the middle and the 3's in the suffix. The order of problems is by number: positions 1..5. The 2 is at position 1. To have all correct: we want participant2 to get the middle, participant3 to get suffix. The middle must be contiguous after prefix. If prefix empty, middle starts at position 1. So position 1 should be desired 2, which is true because owner at position 1 is 2. Then the suffix starts at some position >1, say from position 2 to 5. Those should be 3, and indeed owners are 3. So we can set i=0 (prefix empty) and s=1? Wait s = end of middle. If i=0 and s=1, then middle = positions 1..1 (desired 2), suffix = positions 2..5 (desired 3). That matches all: p1=2 correct, p2=3 correct, p3=3 correct, p4=3 correct, p5=3 correct. So correct=5! So moves=0. But initially, the first participant has none? Actually the first participant initially has no problems? Wait initial owners: first participant? The input: we said first participant owns none? In this example we said all owned by participant3 except one by participant2. But the problem statement says the three participants initially have some problems; so it's possible that participant1 has 0? Actually constraints say k1,k2,k3 >=1. So each participant initially has at least one problem. So my example violates constraints because k1 would be 0. So it's not allowed. But the algorithm gave ans=0 which is correct for that case. However, since constraints ensure each ki >= 1, we have at least one problem for each participant initially. So in a valid test case, there will be at least one 1, one 2, one 3 in the owner array. So we don't need to worry about all zeros.
But is it possible that the optimal solution yields max_correct = n? That would mean initial distribution already satisfies some partition (like example2). That's fine.
Now, confirm that our algorithm yields max_correct = n in such cases.
Now, any potential overflow? n up to 200k, counts up to 200k, differences within [-200k,200k], sums within [-200k,400k]. So int (32-bit) is sufficient.
Now, final code.
We'll produce final answer.
But before we finalize, we should consider if there is any alternative interpretation: The moves are counted as each time a contestant gives a problem to another. Could we possibly reduce moves by having a contestant give a problem to someone who then gives it to the final owner? That would be two moves for that problem, which is worse. So one move per problem that changes owner is optimal.
Thus solution is correct.
Now, time to produce final answer with explanation.
We'll craft the answer accordingly.
Now, final. ```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
code
int k1, k2, k3;
cin >> k1 >> k2 >> k3;
int n = k1 + k2 + k3;
vector<int> owner(n + 1, 0); // 1‑based, owner[p] = 1,2 or 3
for (int i = 0; i < k1; ++i) { int x; cin >> x; owner[x] = 1; }
for (int i = 0; i < k2; ++i) { int x; cin >> x; owner[x] = 2; }
for (int i = 0; i < k3; ++i) { int x; cin >> x; owner[x] = 3; }
int cnt1 = 0, cnt2 = 0, cnt3 = 0;
int bestA = 0; // max A[i] for i ≤ current s, A[0] = 0
int maxSum = 0; // best value of A[i] + B[s] seen so far
for (int p = 1; p <= n; ++p) {
if (owner[p] == 1) cnt1++;
else if (owner[p] == 2) cnt2++;
else cnt3++;
int A_cur = cnt1 - cnt2; // A[p]
int B_cur = cnt2 - cnt3; // B[p]
if (A_cur > bestA) bestA = A_cur;
int cur = bestA + B_cur;
if (cur > maxSum) maxSum = cur;
}
int total3 = cnt3;
int answer = n - (maxSum + total3);
cout << answer << "\n";
return 0;