Pasha decided to invite his friends to a tea party. For that occasion, he has a large teapot with the capacity of w milliliters and 2n tea cups, each cup is for one of Pasha's friends. The i-th cup can hold at most ai milliliters of water.
It turned out that among Pasha's friends there are exactly n boys and exactly n girls and all of them are going to come to the tea party. To please everyone, Pasha decided to pour the water for the tea as follows:
Pasha can boil the teapot exactly once by pouring there at most w milliliters of water;
Pasha pours the same amount of water to each girl;
Pasha pours the same amount of water to each boy;
if each girl gets x milliliters of water, then each boy gets 2x milliliters of water.
In the other words, each boy should get two times more water than each girl does.
Pasha is very kind and polite, so he wants to maximize the total amount of the water that he pours to his friends. Your task is to help him and determine the optimum distribution of cups between Pasha's friends.
Input
The first line of the input contains two integers, n and w (1 ≤ n ≤ 105, 1 ≤ w ≤ 109) — the number of Pasha's friends that are boys (equal to the number of Pasha's friends that are girls) and the capacity of Pasha's teapot in milliliters.
The second line of the input contains the sequence of integers ai (1 ≤ ai ≤ 109, 1 ≤ i ≤ 2n) — the capacities of Pasha's tea cups in milliliters.
Output
Print a single real number — the maximum total amount of water in milliliters that Pasha can pour to his friends without violating the given conditions. Your answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6.
Examples
Input
2 4
1 1 1 1
Output
3
Input
3 18
4 4 4 2 2 2
Output
18
Input
1 5
2 3
Output
4.5
Note
Pasha also has candies that he is going to give to girls but that is another task...
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We are given: n boys and n girls, total 2n cups, each cup i has capacity a_i. The constraints: Pasha can boil the teapot exactly once by pouring at most w milliliters of water. Then he distributes water to cups. Each girl gets same amount x, each boy gets same amount 2x. So total water used = nx + n2x = 3n*x. Also each cup's capacity must be at least the amount poured into it. So we need to assign which cups go to girls and which to boys. Since cups have capacities, we need to choose n cups for girls (they need at least x each) and n cups for boys (need at least 2x each). Also total water <= w.
Goal: maximize total water = 3n*x, subject to existence of assignment such that the chosen cups can hold the required amounts, and x >=0. We can choose assignment to maximize possible x.
We need to find maximum x such that: There exist n cups with capacity >= x (for girls) and n cups with capacity >= 2x (for boys). But note: The cups are distinct; we need a partition of the 2n cups into two groups of size n each, with group G having all capacities >= x, group B having all capacities >= 2x. Since we can assign any cup to either gender, the condition is that at least n cups have capacity >= 2x (to be possible for boys) and at least n cups (which might overlap with the previous) have capacity >= x for girls. However careful: The assignment must be disjoint: we need exactly n cups for boys that each have capacity >= 2x, and exactly n cups for girls that each have capacity >= x, and they cannot share cups. So we need to check if we can pick n cups for boys with capacity >= 2x, and the remaining n cups (for girls) have capacity >= x. Equivalent to: After reserving n cups for boys (the ones with highest capacities that can satisfy >=2x?), the rest must all be >= x. We need to see if such assignment exists.
Observation: Since boys require higher amount (2x) than girls (x), we should assign the larger capacity cups to boys, smaller to girls. So a natural strategy: Sort cups by capacity. For a given x, check: The largest n cups should be candidates for boys; they need to be >= 2x. And the smallest n cups will be for girls; they need to be >= x. Is this sufficient? If the largest n are all >= 2x, then we can assign those to boys; and the smallest n must be >= x for girls. But could there be a scenario where some of the largest n are < 2x but if we assign some cups from the lower half to boys and some higher to girls, we might still satisfy? Since boys require double, it's best to give them the largest cups. So the necessary and sufficient condition for existence is: After sorting capacities ascending a[1] <= a[2] <= ... <= a[2n], we must have a[n] >= x (since the n-th smallest is the minimum among the larger half? Actually careful: There are 2n cups. If we sort ascending, the smallest n are the first n (indices 1..n). The largest n are indices n+1..2n. For assignment: We can assign any n to boys, any other n to girls. To minimize stress on capacities, we should assign the n largest cups to boys, and the n smallest to girls. Because boys require more water. If even that assignment fails (i.e., if the smallest among the n largest is < 2x or the largest among the n smallest is < x), then there is no assignment that works. Let's prove: If we have any assignment that works, then the set of n cups assigned to boys must all be >= 2x; thus there exist at least n cups with capacity >= 2x. Among all cups, let the number of cups with capacity >= 2x be count_big. For assignment to exist, we need count_big >= n. Additionally, after taking n cups for boys, the remaining n cups must all be >= x. So we need that the smallest n cups among those not taken for boys are >= x. But if we take the n largest for boys, the remaining are the n smallest. If the n smallest all >= x, then any other selection of n cups for boys (which would leave a superset of the n smallest? Actually if we choose a different set for boys, the leftover set might include some larger cups and some smaller, but the minimal capacity among leftovers could be even smaller than the smallest n. So the worst-case leftover is exactly the n smallest if we take the n largest for boys. So if even with the most favorable assignment (largest to boys) the leftover n (smallest) are all >= x, then it's okay. But also we need that the n largest are all >= 2x. Could there be a scenario where the n largest are not all >= 2x, but we can still choose n boys cups by taking some of the larger cups that are >=2x and also some from the smaller half that happen to be >=2x? But if the n largest are not all >=2x, that means that the (n+1)-th smallest (i.e., the smallest of the top half) is < 2x. That implies that at most n-1 cups are >= 2x? Let's think: Sorted ascending a_1..a_2n. If a_{n+1} < 2x, then since a_{n+1} is the smallest among top n, then all a_i for i <= n+1 are < 2x, so number of cups with capacity >= 2x is at most (2n - (n+1) + 1?) Actually if a_{n+1} < 2x, then all cups up to index n+1 are < 2x, because sorted ascending, so the first n+1 cups are < 2x, leaving at most 2n - (n+1) = n-1 cups that could be >= 2x. So count_big <= n-1 < n. So impossible to have n cups >= 2x. Thus necessary condition: a_{n+1} >= 2x? Wait, the condition is: there must be at least n cups with capacity >= 2x. Since sorted, the largest n are indices n+1...2n. So we need the smallest of these, a_{n+1}, to be >= 2x. Because if a_{n+1} < 2x, then all a_i for i <= n+1 are < 2x, so only indices > n+1 might be >=2x, but there are at most n-1 of them. So indeed necessary: a_{n+1} >= 2x. Similarly, for the leftover after taking the n largest, we need all n smallest (a_1..a_n) to be >= x. That is necessary because if any a_i < x for i <= n, then that cup would have to be assigned to girls, but it cannot because it's too small; but could we assign a small cup to boys? No, boys need 2x > x, so if a_i < x, it's definitely less than 2x, so it can't be used for boys either. So that cup would be unusable altogether, but we must assign every cup? Wait, we have exactly 2n cups. We must pour water into each cup? The problem says: "Pasha decided to invite his friends to a tea party. He has 2n tea cups, each cup is for one of Pasha's friends." So each friend gets a cup, and we pour water into each cup. So all cups are used. So indeed we need to assign each cup to either a girl or a boy, and every cup gets used. Therefore, for a given x, we must be able to label each cup as girl or boy such that: if girl, capacity >= x; if boy, capacity >= 2x. And exactly n of each.
Thus we need to find if there exists a labeling. Equivalent to: There is a bipartition with exactly n boys. The condition we derived: sort a, if we take the n largest as boys, then we need a_{n+1} >= 2x and a_n >= x? Actually if we assign the n largest to boys, the girls get the n smallest. Then we need: For all i in [1..n], a_i >= x, and for all i in [n+1..2n], a_i >= 2x. That's sufficient. Is it necessary? Suppose there exists some assignment that satisfies. Consider the set of boys B (size n). Let b_min = min_{i in B} a_i, which must be >= 2x. Since there are n boys, the n largest cups must all be >= b_min? Not necessarily: If the n largest cups include some that are < b_min? But b_min is at least 2x. So any cup with capacity < 2x cannot be a boy. So all boys must be among cups with capacity >= 2x. Since there are n boys, we need at least n cups with capacity >= 2x. So count_big >= n. And among those, we select n for boys. The leftover cups, which are girls, consist of the other n cups: some may be from the >= 2x set (if count_big > n) and some from the < 2x set. But girls only need >= x, so any cup with capacity >= x can be a girl. However, if there is a cup with capacity < x, it cannot be assigned to anyone (since boys need >= 2x > x). So we must have no cup with capacity < x. But is that necessary? Because if a cup has capacity < x, it cannot be used for either gender, so it's impossible. So indeed, all cups must have capacity >= x. Wait, but we could assign a cup with capacity between x and 2x to a girl, but if a cup is less than x, it's impossible. So necessary condition: min_i a_i >= x. But we have exactly 2n cups, all must be assigned, so the smallest capacity must be >= x. That is a stronger condition than a_n >= x? Since a_1 is the smallest, we need a_1 >= x. But a_n >= x is weaker. However, is a_1 >= x necessary? Consider if some cups are < x but we assign them to boys? But boys require 2x, so they would be even more insufficient. So yes, if any cup has capacity less than x, we cannot pour x into it for a girl, nor 2x for a boy. So that cup would not receive enough water, but we have to pour water into it because each friend gets a cup. The constraints say each cup can hold at most a_i milliliters, but we pour water into it. The amount poured must be <= capacity. So if we pour x (or 2x) into a cup, we need capacity >= that amount. So if a cup capacity < x, we cannot pour x or 2x into it. So it's impossible. So indeed, all cups must have capacity >= x. That is necessary. But is it sufficient? Not exactly, because we also need that at least n cups have capacity >= 2x to assign to boys. But note: if all cups are >= x, and at least n cups are >= 2x, then we can assign those n as boys, the rest as girls (since they are >= x but possibly < 2x). So condition: min_i a_i >= x AND count of cups with capacity >= 2x >= n. Since we have 2n cups, the condition min_i a_i >= x is equivalent to a_1 >= x. And count_big >= n is equivalent to a_{n+1} >= 2x if sorted? Actually if sorted ascending, count_big >= n means that the n-th largest cup (i.e., the (2n - n + 1) = (n+1)-th smallest) is >= 2x. Because there are 2n cups, and if the (n+1)-th smallest is >= 2x, then at least n cups (from index n+1 to 2n) are >= 2x. If it is less than 2x, then at most n-1 cups are >= 2x. So indeed condition is a_{n+1} >= 2x. And we also need a_1 >= x. But note: if a_1 >= x, automatically a_n >= x, but a_1 >= x is stronger. So the necessary and sufficient conditions for existence are: (1) a_1 >= x; (2) a_{n+1} >= 2x.
But is (1) necessary? Suppose a_1 < x but there are enough cups >= 2x to assign n boys, and the girls need n cups. Could we assign the small cup (with capacity < x) to a boy? No, because boy needs 2x > x > capacity, so not possible. So we must assign it to a girl, but girl needs x > capacity, so not possible. So indeed impossible. So (1) is necessary.
Thus the feasible x must satisfy: x <= min( a_1, a_{n+1}/2 ). And also the total water 3n x <= w. So we have an upper bound on x: x_max = min( w/(3n), a_1, a_{n+1}/2 ). Actually careful: The condition a_{n+1} >= 2x means x <= a_{n+1}/2. So we have x <= min( a_1, a_{n+1}/2, w/(3n) ). Since we want to maximize total water = 3n x, we want the maximum x that satisfies these three constraints. That's simply x_opt = min( a_1, a_{n+1}/2, w/(3n) ). Then answer = 3n * x_opt.
But let's test with examples.
Example 1:
n=2, w=4, a = [1,1,1,1].
Sorted: 1,1,1,1. a_1=1, a_{n+1}=a_3=1. So a_1=1, a_{n+1}/2=0.5. w/(3n)=4/6≈0.6667. So min = 0.5. Then total = 320.5=3. That matches output 3.
Example 2:
n=3, w=18, a = [4,4,4,2,2,2]. Sorted: [2,2,2,4,4,4]? Actually given: 4 4 4 2 2 2. Sorting ascending yields [2,2,2,4,4,4]. a_1=2, a_{n+1}=a_4=4 => a_{n+1}/2=2. w/(3n)=18/9=2. So min = 2. Then total = 9*2=18. Output 18.
Example 3:
n=1, w=5, a=[2,3]. Sorted: [2,3]. a_1=2, a_{n+1}=a_2=3 => /2 =1.5. w/(3n)=5/3≈1.6667. min = 1.5. Total = 3*1.5=4.5. Output 4.5.
Matches.
Thus solution: sort the 2n capacities, compute x_max = min( a[0], a[n] / 2, w/(3.0*n) ). But careful indexing: a_1 is smallest, index 0 in 0-based. a_{n+1} is the (n+1)-th smallest; since indices 0..2n-1, the n-th index (0-based) corresponds to the (n+1)-th smallest? Let's derive: if sorted a[0] <= a[1] <= ... <= a[2n-1]. Then the smallest is a[0]. The (n+1)-th smallest is a[n] because 0-index: 0->1st, 1->2nd, ..., n->(n+1)th. So a_{n+1} = a[n] (0-index). So condition: a[n] >= 2x => x <= a[n]/2. So compute:
double x = min( (double)a[0], a[n] / 2.0, w / (3.0 * n) );
But wait: Could there be scenario where a[0] is large enough, but a[n]/2 is larger? Then x limited by w/(3n) or a[0]? Actually x can't exceed the smallest of these three. So ok.
But is the condition a[0] >= x always necessary? Let's reexamine the logic: All cups must be used. So each cup must receive either x (girl) or 2x (boy). Therefore, each cup's capacity must be at least x, because even the girls get x. However, could a cup be assigned to a boy and still have capacity less than x? That's impossible because boy gets 2x > x, so if capacity < x, it's definitely < 2x. So indeed all cups must have capacity >= x. So condition a[0] >= x is necessary.
But wait, could we possibly pour less than x to some cup? No, because each girl gets exactly x, each boy gets exactly 2x, and all cups are used for either gender. So every cup gets either x or 2x. So yes.
Thus the problem reduces to that simple bound.
But is there any nuance about the assignment when count_big > n? That's okay, we assign exactly n of those to boys, others to girls. Then some girls may have capacities > 2x, but that's fine. The only constraints are as above.
Check edge cases: n up to 1e5, w up to 1e9, a_i up to 1e9, so multiplications within 64-bit integer, but we use double for division. But careful: w/(3n) could be computed as w/(3.0n). a[n] might be up to 1e9, dividing by 2 yields up to 5e8. So x up to ~1e9. total water = 3nx up to about 3e5 * 1e9 = 3e14, fits in double with 53-bit mantissa? 3e14 < 2^53 (~9e15), so exact integer representation for integers up to 9e15 is safe. So double is fine.
But we need to output real number. Even if answer is integer, we can output as double with appropriate precision.
Thus algorithm: read n, w; read list a of size 2n; sort a; compute x = min( a[0], a[n]/2.0, w/(3.0n) ); output 3n*x.
But wait, there might be a catch: The condition "Pasha can boil the teapot exactly once by pouring there at most w milliliters of water;". That means total water poured into teapot is at most w. But we are also constrained by cup capacities. So yes.
Thus solution seems trivial. But is there any scenario where the assignment can be better by not giving each girl exactly x and each boy exactly 2x, but maybe some cups are not used? No, all cups are for friends, so must be used.
Thus answer is as above.
Let's double-check with some custom tests.
Test: n=1, w=10, cups [5,100]. Sorted: [5,100]. a[0]=5, a[1]/2=50, w/(3)=3.333..., min=3.333..., total=3*3.333=10. So maximum total water = 10, but can we actually pour 10? Total water = 3 * 1 * x = 3x <=10 => x <=3.333, then we need assign: need a boy with capacity >= 2x=6.667, and a girl with capacity >= x=3.333. Cups: 5 and 100. We can assign the 100 cup to boy (100 >=6.667) and the 5 cup to girl (5>=3.333). Yes works, total water=10. So good.
Test: n=2, w=100, cups: [1,100,100,100]. Sorted: [1,100,100,100]. a[0]=1, a[2]/2=100/2=50, w/(6)=16.6667, min=1? Actually min(1, 50, 16.6667)=1. So x=1, total=6. But could we do better? Suppose we set x=16.6667? But then need a[0]>=x: a[0]=1 <16.666, so impossible. So indeed maximum x=1. That yields total=6. But could we assign cups differently? For x=1, all cups must hold at least 1, they do. Need two cups with capacity >=2 for boys: cups with capacities 100,100,100 satisfy, so we assign two of them to boys, and the remaining two cups (including the 1) to girls. But the girl cup with capacity 1 gets x=1, ok. So total water 6. Could we try to give the 1 cup to a boy? Boy needs 2x=2, but capacity 1 <2, not possible. So indeed max total=6.
Now consider if the smallest cup is relatively small but there are enough large cups for boys. The limiting factor is the smallest cup, because it must hold at least x. So x can't exceed smallest capacity.
Thus the derived condition stands.
But wait, could we assign the smallest cup to a boy and give him less than 2x? No, because condition says each boy gets exactly 2x, and each girl gets exactly x, and they are same for all boys and all girls. So no.
Therefore solution is correct.
Thus problem is trivial.
But I recall some similar problems (Codeforces problem "Tea Party" maybe?) might have a twist: The teapot capacity w, but also we might not be able to pour all water because cups limit. Actually we derived x <= min(a1, a_{n+1}/2, w/(3n)). However, could we sometimes use less than w and still max total? That's what we do.
Now confirm with more nuance: The requirement that each boy gets twice each girl is strict. So we must pour exactly x for each girl and 2x for each boy. So total is fixed as 3n x. We can choose x to maximize total subject to constraints. That's all.
Thus answer is straightforward.
Implementation: read n and w; read array of 2n ints; sort; compute double ans = 3.0 * n * min( (double)a[0], a[n]/2.0, w/(3.0*n) ); print with sufficient precision.
But careful: w and a are ints up to 1e9, and n up to 1e5, so w/(3.0*n) yields double. Use min of three numbers: double v1 = a[0]; double v2 = a[n] / 2.0; double v3 = w / (3.0 * n); double x = min(v1, min(v2, v3)); ans = 3.0 * n * x; print.
But wait: a[0] is int, but x should be real, not necessarily integer. So ok.
But is there any case where v2 might be smaller than v1 and v3, but assignment could be done with larger x if we assign some cups differently, perhaps using some cups with capacity between x and 2x for boys? No, because boys need 2x, so any cup assigned to boys must have capacity >= 2x. So the limiting factor for x from boys side is the nth largest cup among those that can be assigned to boys. That is exactly the nth largest overall capacity? Actually if we assign the n largest cups to boys, the smallest among those must be >=2x. So x <= smallest among top n / 2 = a[n]/2 (since sorted). Could there be an assignment where we don't assign the n largest to boys, but still have x > a[n]/2? Suppose a[n] is not that large, but we have many cups with capacities just above 2x but some of them might be in the lower half? Wait, if a[n] is the (n+1)-th smallest, and it is less than 2x, then there are at most n-1 cups with capacity >= 2x. So we cannot assign n cups to boys. So x cannot exceed a[n]/2. So condition is necessary.
Now consider the smallest cup a[0] condition: If a[0] is small, x cannot exceed it. That seems necessary as argued. But could we assign that small cup to a boy and give him less than 2x? No, because all boys must get same 2x. So not.
Thus answer stands.
Double-check with random tests: Let's do n=2, w=100, a=[10, 30, 40, 50]. Sorted: [10,30,40,50]. a0=10, a2=40 => a2/2=20. w/(6)=16.666. min = 10? Actually min(10,20,16.666)=10. So x=10, total=60. Could we get more? Suppose x=16.666, then need all cups >=16.666? smallest is 10 <16.666, fails. So can't. But what if we assign the 10 cup to a boy? Then boy needs 33.33 >10, fails. So indeed max total=60. Now if we had a = [15,30,40,50], then a0=15, a2=40 =>20, w/6=16.666 -> min=15? Actually min(15,20,16.666)=15? Wait 15<16.666, so x=15, total=90. But could we get x=16.666? then need smallest >=16.666, but a0=15 fails. So total=90. But if we increase to x=16, need smallest >=16, but 15 fails, so no.
Now test where a0 is large but a[n]/2 is limiting: a = [100,101,102,103] with n=2, w=1000. a0=100, a2=102=>51, w/6=166.66, min=51 => total=306. So x=51, total=306. Check: Boys need 102, girls need 51. Cups: assign two largest (102,103) to boys? They have capacities 102,103; 102>=102 ok, 103>=102 ok. Girls get (100,101) each >=51 ok. So total = 251+2102=102+204=306. Could we get higher total by assigning e.g., the 100 cup to a boy? Then boy needs 102, but 100<102 not possible. So no. So maximum is 306.
Now test where w is limiting: a = [1000,1000,1000,1000], n=2, w=10. Then a0=1000, a2=1000=>500, w/6≈1.6667, min=1.6667, total=10. So max total=10 (fill teapot capacity). That works.
Thus answer algorithm seems correct.
Edge Cases: n=1, w and cups small. Always compute as above.
Check scenario where a[n] is less than a[0]? That's impossible because sorted: a[n] >= a[0].
Now, can we ever have x limited by both a0 and a[n]/2? Possibly.
Now, I suspect this problem is from Codeforces Round... The solution is indeed simple.
But I recall there might be a nuance: The capacity of the teapot is w, but we are allowed to pour at most w. So we can pour less than w. So maximizing total water is essentially maximizing 3n x subject to constraints. So x can be at most w/(3n) if other constraints are looser. So answer = min( w, 3n * min(a0, a[n]/2) )? Actually careful: total = 3n * min( a0, a[n]/2, w/(3n) ). That's equivalent to min( 3n * a0, 3n * (a[n]/2), w ). Since 3n * (a[n]/2) = (3n/2) * a[n]. And w is the capacity. So answer = min( w, 3n * a0, (3n/2) * a[n] ). But note: a0 <= a[n]? Not necessarily? Actually sorted: a0 <= a[n]. So 3n * a0 <= 3n * a[n], but (3n/2)a[n] could be larger or smaller than 3n * a0 depending on values. But answer is min of the three. But we need to be careful about integer overflow when computing 3n * a0, as n up to 1e5 and a0 up to 1e9 => product up to 1e14, fits in 64-bit signed (9e18). So we could compute using 64-bit integers for these min comparisons, but since we need to output real number maybe fractional? But a0 and a[n] are integers, but total water must be a real number? Wait, x may be fractional because of w/(3n) could be fractional. But the answer could be fractional if the limiting factor is w/(3n) not yielding an integer x. But if the limiting factor is a0 or a[n]/2, then x is either integer or half-integer? a0 is integer, so x = a0 yields integer total = 3na0, which is integer. a[n]/2 could be integer or .5. So total could be .5 multiples. So answer can be fractional. But we can compute as double.
Alternatively, we could compute ans = min( w, 3.0 * n * min(a[0], a[n] / 2.0) ). That is simpler: x_max_cup = min(a[0], a[n]/2); Then total_max_cup = 3 * n * x_max_cup; Then ans = min(total_max_cup, (double)w). Because w is an upper bound on total. But careful: total_max_cup is maximum total water allowed by cups. But if w is lower, we take w. So ans = min( w, 3.0 * n * min(a[0], a[n] / 2.0) ). However, is it always that total_max_cup = 3n * min(a0, a[n]/2)? Let's derive: The constraints from cups are x <= a0 and x <= a[n]/2. So maximum x from cups is min(a0, a[n]/2). Then total from cups is 3n * that x. But then we also have w. So ans = min( w, 3n * min(a0, a[n]/2) ). But is it possible that w/(3n) is smaller than min(a0, a[n]/2) but total water limited by w? That's exactly the min with w. So answer = min( w, 3n * min(a0, a[n]/2) ). That is simpler: no need to compute w/(3n) separately. Actually check: if w < 3n * min(a0, a[n]/2), then answer = w, else answer = 3n * min(a0, a[n]/2). That's equivalent to min(w, 3n * min(a0, a[n]/2)). But careful: If w is not a multiple of 3n, but the cup-limited x is min(a0, a[n]/2). Then we cannot exceed w, so the total is limited by w, but we can choose x = w/(3n) as long as that doesn't exceed the cup limit. If w/(3n) <= min(a0, a[n]/2), then x = w/(3n) yields total = w. So answer = w. If w/(3n) > min(a0, a[n]/2), then x limited by cups, total = 3n * min(a0, a[n]/2). So answer = min( w, 3n * min(a0, a[n]/2) ). But note: if w is larger than 3n * min(a0, a[n]/2), the answer is 3n * min(a0, a[n]/2) (which is less than w). So yes, answer = min( w, 3n * min(a0, a[n]/2) ). But wait, is there any scenario where w is between those? That's captured.
Now consider example 2: w=18, n=3, min(a0, a[n]/2) = min(2,4/2=2)=2; 3n*2 = 18; min(w,18)=18. Output 18.
Example 1: min(1, 1/2=0.5)=0.5; 3n*0.5=3; w=4; min(4,3)=3.
Example 3: min(2,3/2=1.5)=1.5; 3*1.5=4.5; w=5; min=4.5.
Thus formula works.
But careful: In the case where min(a0, a[n]/2) is computed as double, 3n times that yields a double. We compare with w (integer). So ans = min( (double)w, 3.0nmin(a0, a[n]/2.0) ). Then print ans.
But there is a nuance: The condition a0 >= x is necessary. However, is it possible that a0 < x but we could assign that small cup to a boy and give the boy exactly 2x, but if the cup is too small, we cannot; but what if we pour less than the cup's capacity? No, we have to pour exactly 2x into the boy's cup. So the cup must hold at least 2x. So if a0 < x, it's also < 2x. So it cannot be used for either. So indeed it must be >= x. So condition stands.
Now, is there any scenario where the smallest cup is not used as a girl but as a boy? That would require it to hold at least 2x, which is even larger, so if it cannot hold x, it certainly cannot hold 2x. So it's impossible.
Thus the derived formula is correct.
But let me think about potential counterexample: Suppose we have capacities: [5, 9, 10, 12] for n=2. Sorted: 5,9,10,12. a0=5, a2=10 => a2/2=5. So min=5. Then total=325=30. But w may be large. Check: x=5. Boys need 10, girls need 5. Assign boys: need two cups >=10: we have 10 and 12, ok. Girls: remaining 5 and 9, both >=5, ok. Total water = 25 + 210 = 30. Could we do x=5.5? Then need all cups >=5.5? smallest is 5 <5.5, so impossible. So 30 is max. Ok.
Now what about [6,7,8,20] for n=2. Sorted:6,7,8,20. a0=6, a2=8 =>4. So min=4? Actually a2/2=4, a0=6, so min=4. So x=4, total=24. But can we do x=6? Then need all cups >=6? smallest is 6 ok. But boys need 12. Do we have two cups >=12? Cups: 6,7,8,20. Only 20 >=12, so only one cup, so not enough boys. So x limited by a2/2=4. So total=24. Could we try x=5? Then all cups >=5? 6,7,8,20 ok. Boys need 10: cups >=10: only 20, so only one. So no. So indeed max x=4.
Thus formula works.
Thus solution: O(2n log(2n)) sort, then compute.
But we can also do O(n) using nth_element to find a0 and a[n] (the n-th smallest? Actually we need smallest and the (n+1)-th smallest. But sorting is fine for 2e5 elements.
Now consider if n=0? But problem says 1 ≤ n, so n>=1.
Now, confirm that a[n] is the (n+1)-th smallest: There are 2n elements, indices 0..2n-1. The smallest is a[0], the n-th smallest is a[n-1]? Let's check: For n=2, 2n=4. Indices: 0 -> 1st, 1 -> 2nd, 2 -> 3rd, 3 -> 4th. So the (n+1)-th smallest = third smallest = index 2? Since n=2, n+1=3 -> index 2. That's a[2]. In our earlier we used a[n] as index n (2) which matches. So a[n] is correct: for n=2, a[2] is third smallest. For n=1, 2n=2, indices:0,1; a[n] = a[1] is second smallest, i.e., the larger one, which is indeed the (n+1)-th smallest. So good.
Thus compute: sort a, then let girl_limit = a[0], boy_limit = a[n] / 2.0. Then x = min(girl_limit, boy_limit). Then total_cup = 3.0 * n * x. Answer = min(total_cup, (double)w). Print with precision.
But we must be careful: The total_cup might be slightly larger than w due to floating point errors? Use min with w (as double) ensures we output correct.
But also consider that w is an integer up to 1e9, so double representation exact.
Thus final implementation.
Wait, check if there is any trick with the condition "Pasha can boil the teapot exactly once by pouring there at most w milliliters of water". That means the total water poured from teapot to cups cannot exceed w. But we might also consider that we cannot pour water from teapot partially? Actually we can pour exactly the amount we distribute, at most w. So we choose x accordingly. So if cup-limited total is less than w, we just pour that amount; if it's greater, we are limited by w, so we pour w? But we can't pour more than w, but we can pour less. So to maximize total, we would pour as much as possible, up to the cup limits and w. So if cup-limited total is >= w, we can pour w exactly? But careful: If cup-limited total > w, does that mean there exists x such that total = 3n x <= w and satisfying cup constraints? Actually cup-limited total is the maximum total water achievable under cup constraints only, i.e., with x_max_cup = min(a0, a[n]/2). If 3n * x_max_cup > w, then we cannot achieve that total because we are limited by w. Instead, we can only achieve total = w, provided that x = w/(3n) satisfies cup constraints, i.e., w/(3n) <= x_max_cup. Since w/(3n) is less than x_max_cup by assumption (since 3n * x_max_cup > w => x_max_cup > w/(3n)), then indeed x = w/(3n) is feasible (since w/(3n) <= x_max_cup). So answer = w. So min works.
Thus final answer.
But need to watch out for integer overflow in 3na0: if we compute 3na0 as integer, it might overflow 64-bit? n up to 1e5, a0 up to 1e9, product 3e14, which fits in 64-bit (2^63-1 ≈ 9.22e18). So safe. But we could compute directly as double.
Implementation in C++:
code
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, w;
cin >> n >> w;
vector<int> a(2*n);
for(int i=0; i<2*n; i++) cin >> a[i];
sort(a.begin(), a.end());
double x = min((double)a[0], a[n] / 2.0);
double total = 3.0 * n * x;
if(total > w) total = w;
cout << fixed << setprecision(10) << total << endl;
return 0;
}
But careful: The answer might be fractional, but if total is exactly w (an integer), it will print as integer. That's fine.
Alternatively, we could compute:
code
double ans = min(1.0 * w, 3.0 * n * min(1.0 * a[0], a[n] / 2.0));
But ensure to use long double for extra precision? Not needed.
Now, think about whether we must consider that we could pour less than x to some girls if cups are smaller? No, all must get same.
Thus solution is correct.
However, I recall a problem "Pasha and Tea" on Codeforces had similar but the condition was each boy gets exactly twice each girl, and there are n girls and n boys, total 2n cups. And solution is exactly min( w, 3nmin(a[0], a[n]/2) ). Yes that's classic.
Thus we can present solution.
Now, regarding input constraints: The array length is 2n, up to 2e5. Sorting O(2n log 2n) acceptable.
Thus answer.
I'll now produce final message with explanation. We sort the capacities and let the smallest be (a_1) and the ((n+1))-th smallest be (a_{n+1}).
Every girl receives the same amount (x), every boy receives (2x).
All cups must be used, so each cup must hold at least (x) (even the ones given to boys need (2x > x)).
Moreover, we need at least (n) cups that can hold (2x); the smallest among the largest (n) cups is (a_{n+1}), hence we require (a_{n+1} \ge 2x).
Thus (x) is bounded by
[
x \le \min\left(a_1,\ \frac{a_{n+1}}{2},\ \frac{w}{3n}\right).
]
The maximum total water is (3n x).
The three bounds can be combined as
[
\text{ans} = \min\left(w,\ 3n\cdot\min!\left(a_1,\ \frac{a_{n+1}}{2}\right)\right).
]
Implementation: read (n), (w) and the (2n) capacities, sort them, compute the value and output with sufficient precision.
cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, w;
cin >> n >> w;
vector<int> a(2 * n);
for (int &x : a) cin >> x;
sort(a.begin(), a.end());
double fromCups = 3.0 * n * min((double)a[0], a[n] / 2.0);
double ans = min((double)w, fromCups);
cout << fixed << setprecision(10) << ans << endl;
return 0;
}