We need to solve: Given n numbers a_i (1<=a_i<=100, n up to 5e4). We can perform at most one operation: pick i, j (i != j, presumably? It says "choose one machine and reduce the power of its machine by x times, and at the same time increase the power of one another machine by x times". So i and j must be distinct? It says "choose two such indices i and j" and then perform changes: a_i = a_i / x, a_j = a_j * x. They don't explicitly say i != j, but it would be pointless if same. So assume i != j. Also x must be a divisor of a_i (and presumably integer >0). After operation, all powers remain positive integers. We want to minimize total sum S = sum a_i. We can also choose not to perform any operation.
We need to compute minimal possible sum after at most one such operation.
We can think of the operation as transferring a factor x from a_i to a_j. Since we divide a_i by x and multiply a_j by x, the product of all a_k stays the same? Actually product becomes (a_i/x)(a_jx)*others = product. So product is invariant. Sum is what we want to minimize.
We have a multiset of numbers, we can move a factor (which divides a_i) from one number to another. Equivalent to: pick two numbers a, b. Pick a divisor d of a (d > 0 integer). Replace a with a/d, b with bd. The sum changes by delta = (a/d + bd) - (a + b) = a/d - a + bd - b = a(1/d - 1) + b*(d-1) = -a*(1 - 1/d) + b*(d-1). Actually better: delta = a/d + bd - a - b = a(1/d - 1) + b*(d - 1) = -a*( (d-1)/d ) + b*(d-1) = (d-1)*(b - a/d). Hmm not simpler.
Alternatively, note that product invariant, sum minimization under product fixed for a set of positive numbers? Under fixed product, sum is minimized when numbers are as "unequal" as possible? Actually, given fixed product and positive numbers, the sum is minimized when the numbers are as imbalanced as possible (since for two numbers, given fixed product, sum is minimized when they are as far apart? Let's check: for two positive numbers a, b with fixed product P, a+b minimized when a=b=sqrt(P) gives sum 2 sqrt(P). Actually that's the minimum, not maximum. For fixed product, sum is minimized when numbers are equal? For two numbers, by AM-GM, a+b >= 2 sqrt(ab), so sum is at least 2 sqrt(P). So sum is minimized when they are equal? That contradicts? Actually AM-GM: (a+b)/2 >= sqrt(ab) -> a+b >= 2 sqrt(P). So the minimal sum is when a=b, not when they are imbalanced. Wait, but we have more than two numbers and we can change two numbers while keeping product constant. The sum change from moving a factor from a to b: starting with a, b, after: a' = a/d, b' = bd. So product remains ab. The new sum S' = S - a - b + a/d + bd. The change: Δ = a/d + bd - a - b = (a/d - a) + (bd - b) = a(1/d - 1) + b(d - 1). Since d is a divisor of a, d >= 1 integer. For d=1, Δ=0. For d>1, we want Δ negative to reduce sum. So we want a(1/d - 1) + b(d - 1) < 0. That is -a(d-1)/d + b*(d-1) < 0 => (d-1)(b - a/d) < 0 => since d>1, d-1>0, so we need b - a/d < 0 => b < a/d. So the condition for sum reduction is that b < a/d. In other words, we want to take a factor d from a (making a smaller) and give it to b (making b larger), but we need that the increased b (after multiplication) is not too large: Actually condition b < a/d means that the new a (a/d) becomes larger than b? Wait: new a = a/d, new b = bd. The condition for sum reduction is b < a/d. That is original b is less than the new a. Equivalent to original a > bd? Let's see: b < a/d => bd < a. So original a is larger than bd. That condition seems plausible. But note d divides a. So we need to find a pair (i,j) and divisor d>1 of a_i such that b = a_j satisfies a_j < a_i / d. Then sum decreases by (d-1)(a_i/d - a_j) (since Δ = (d-1)(a_j - a_i/d) ? Actually we derived Δ = (d-1)(b - a/d). So if b < a/d, Δ negative, reduction magnitude = (d-1)(a/d - b). So we want to maximize reduction.
But we need minimal sum, so we want as large reduction as possible (or zero if none possible). Since we can only do one operation, we need to find the max possible reduction.
Constraints: n up to 5e4, a_i up to 100. So numbers are small, but n is large. We need an efficient algorithm, perhaps O(100^2 * something) per test? Actually we can precompute something. Since a_i are small, we can count frequencies of each value from 1 to 100. Since we only care about values, we can treat each possible value v, and consider divisors d of v. Then we need to know if there exists another number w such that w < v/d. And we also want to maximize reduction.
But careful: The operation uses exactly one divisor d of a_i, and we can pick any j (including possibly same value as i but different index). Since frequencies may be >1, we can pick i and j possibly with same value? But they must be distinct machines. If there are at least two machines, we can pick any distinct indices. So as long as we have at least one occurrence of the source value v and at least one occurrence of the target value w (or possibly same value but different indices if we need a source and a different target? Could we pick i and j with same value? If we pick same value, but then we would require d divisor of a_i and then a_i = v, a_j = v, after: a_i' = v/d, a_j' = v*d. That changes two numbers. It's allowed as long as i != j. So that's permissible. But is there any restriction that i and j must be distinct? Yes, they are two different machines. So we can pick both from the same value if there are at least 2 occurrences. So we must consider cases where source and target values may be equal or different, and we need to check availability (counts) to ensure we can pick distinct indices. Since n up to 5e4, but numbers are small, we can handle counts.
We need to compute the maximum reduction possible over all choices of i (value v), j (value w) distinct indices, and divisor d>1 of v such that the operation is allowed (x = d). However, note: x can be any divisor of a_i, not necessarily prime; x integer >0 such that a_i divisible by x. Also x must be integer; but operation says "reduce the power of its machine by x times", meaning multiply by 1/x? Actually they wrote "reduce the power of its machine by x times" meaning divide by x. So x is integer divisor. So yes.
So we have to consider all possible d (divisor >1) of each possible v (1..100). For each such pair (v, d), we get new source value v' = v/d, and new target value w' = w * d, where w is original value of target. But we care about reduction: Δ = (d-1)(v/d - w) if w < v/d. So reduction = (d-1)(v/d - w). Actually Δ negative: -reduction.
We want maximum reduction R = (d-1)*(v/d - w) over all v, d (divisor>1 of v), and w (original value of target) such that w < v/d, and we have at least one index with value v and at least one index with value w (distinct from the source index; if v = w, need at least two occurrences).
But also note: could we consider using a divisor d that is not >1? d=1 yields no change.
So we need to compute max reduction possible. Since a_i <= 100, v and w are in [1,100]. So we can brute force over v and d and w, where v/d is integer <= 100 (since v <=100, v/d <=100). And w is from 1 to 100. So at most 100* (number of divisors) * 100. Number of divisors for numbers up to 100 is at most 12 (like 96 has many divisors, but average small). So total combos maybe around 10010100 = 100k, trivial.
But we must also consider availability: we need at least one machine with value v (source) and at least one machine with value w (target) such that they are distinct. If v != w, we need count[v] >=1 and count[w] >=1. If v == w, we need count[v] >=2 (since we need two distinct machines). Also, we need to ensure that after operation, the values become v/d and wd. Those will be positive integers. Since v/d <=100 and wd could be >100? Actually wd could exceed 100. But operation is allowed even if wd > 100? There's no explicit upper bound after operation; a_i can become >100? The input bound is only for initial a_i. The problem statement doesn't restrict resulting values. So they can become >100. That's fine. So we don't restrict wd. But note that we only care about sum; the reduction formula derived does not depend on wd being bounded. So we can have wd >100. But our brute force over w from 1 to 100 may miss if w is >100? Initially w <=100, so w is within 1..100. So wd could exceed 100. That's okay; we don't need to index beyond 100 for w.
So algorithm: read n, array a. Compute total sum S = sum a_i. Compute count[1..100] = frequency of each value.
Initialize max_reduction = 0.
Loop over v from 1 to 100:
if count[v] == 0: continue.
For each divisor d of v where d > 1:
v_div = v // d.
Then we need to find w (original value of target) such that w < v_div and reduction = (d-1)(v_div - w). For fixed v and d, reduction is decreasing as w increases? Actually for fixed v_div and d, reduction = (d-1)(v_div - w). So larger reduction when w is as small as possible. So to maximize reduction for given (v,d), we would want the smallest possible w that is available and satisfies w < v_div, and also we need to ensure we can pick distinct indices. So we need the minimal w (value) that has count>=1 (and distinct from source if w == v? careful). However if we pick the smallest w, we need to check if it's feasible: if w == v, then we need count[v] >=2 because we need two distinct machines from same value v. If count[v] >=2, we can use w = v as target and source both from v. But w = v may or may not satisfy w < v_div. v_div = v/d. Since d>1, v/d < v, so v < v_div? Actually if w = v, then w = v, but v_div = v/d < v, so w = v is not < v_div, it's greater. So w=v cannot satisfy condition because we need w < v_div. Since v_div < v, v is > v_div, so condition fails. So if w=v, w < v_div is false. So source and target cannot be same value if we want reduction because then w would be equal to v, but v_div < v, so w < v_div would be false. So if we want reduction, we need w strictly less than v_div. Since v_div < v, any w that is >= v cannot be < v_div (since v_div < v). So w must be less than v_div, which is less than v. So w is at most v_div-1. So w <= v_div-1. Therefore w < v. Thus source and target values are distinct: source v > target w (since v_div <= v/2? Not necessarily: if d=2, v_div = v/2, so w < v/2, so w <= v/2 -1 if integer; so w < v. For d>2, v_div even smaller. So indeed source v > target w. So v and w are different. So we don't need to worry about using same index because values will be different. However, could there be a case where v = w but using two distinct machines? But condition w < v_div can't hold because v_div < v, so w=v cannot be < something less than v. So no reduction from same value. So we only consider w != v. Thus we just need count[v] >=1 and count[w] >=1.
Thus for each v,d, we need to find the smallest w in [1, v_div-1] that has count[w] >=1. Then compute reduction for that w. But also, maybe a larger w could yield a larger reduction if w is extremely small? Actually reduction = (d-1)*(v_div - w). So it's larger when w is smaller. So indeed the smallest possible w yields largest reduction. So for each (v,d) we only need the minimal w with count>0 and w < v_div. But wait, what if the smallest w is very small but that w is actually the only occurrence and maybe we need to consider that the source v and target w must be distinct machines. They are distinct because values different. So it's fine. So we can precompute the minimum value present (overall) but we need w < v_div. So for each v_div, we need the smallest w among values that exist and are < v_div. That's essentially the minimum existing value less than some threshold. Since values only up to 100, we can precompute an array min_val_lt[k] = smallest value that appears (count>0) that is less than k? Or more directly, we can just iterate w from 1 to v_div-1 and pick the smallest with count>0. Since v_div <=100, this is at most 100 per (v,d), overall at most 100k, which is fine. However we also need to consider the possibility that no w exists (i.e., all numbers are >= v_div). Then no reduction from that (v,d). So we can check.
But we also must consider that the target machine's original value w can be any, and reduction depends linearly on w. So the smallest w gives best reduction. But is it always optimal to take the smallest w? What about the constraint that we must have at least one machine with value w. That's fine.
But wait: Could there be a scenario where using a larger w yields a larger reduction because maybe the resulting product? No, reduction formula is (d-1)*(v_div - w). For fixed v,d, it's strictly decreasing as w increases. So smallest w yields max reduction.
But is it possible that the smallest w might be so small that w*d becomes huge, and that might affect something else? No, only sum matters. So indeed.
Thus algorithm: compute total sum. Compute max_reduction = 0. For each v (1..100) with count[v] >=1, list its divisors d > 1. For each d, compute v_div = v // d. Then for w from 1 to v_div-1, if count[w] >=1, compute reduction = (d-1)*(v_div - w). Keep max over all. Then answer = total_sum - max_reduction.
But wait: what about the case where we perform operation but not necessarily the optimal reduction? The minimal sum is total_sum - max_reduction (since reduction can't be negative). So answer = sum - max_reduction.
But is it always possible to achieve that max_reduction? Need to ensure that there is at least one source machine with value v and one target machine with value w, and they are distinct. Since v and w are different, we just need at least one of each. That's satisfied if count[v]>=1 and count[w]>=1. That's our check. So good.
Edge case: Could we get a larger reduction by not using the smallest w but using a w that is not the smallest because maybe using the smallest w would cause the target value w*d to exceed something? No constraint.
Thus answer computed.
But is there any nuance? Let's test with examples.
Example1: n=5, a=[1,2,3,4,5]
count:1,2,3,4,5 each 1.
Compute manually.
We need to consider each v,d,w.
We'll compute reductions.
We can also brute think: They gave operation: reduce 4 by 2 times, increase 1 by 2 times: v=4, d=2, v_div=2. w must be <2, so w=1. reduction = (2-1)(2-1)=11=1. So total sum originally 1+2+3+4+5=15, after reduction becomes 14. So max_reduction=1.
Check other possibilities: v=5, d=5? d=5 divides 5, v_div=1, then need w <1, none. d=5 only. v=5,d=5: v_div=1, no w<1. v=4,d=4? 4/4=1, need w<1 none. v=4,d=2 we did. v=4,d=? divisors:2,4? also d=1 excluded. v=3,d=3 -> v_div=1 no w. d= none? 3 only divisor >1 is 3. v=2,d=2 -> v_div=1 no w. v=1 has no divisor >1 except maybe 1? Actually 1 has only divisor 1, so no. So max reduction=1. Answer 15-1=14. matches.
Example2: 4 4 2 4 4 -> a=[4,2,4,4] sum=14? Actually 4+2+4+4=14. They output 14, meaning no reduction possible (or reduction 0). Let's check. count:4 appears 3, 2 appears 1.
We need v,d,w such that w < v/d and both values present.
v=4 divisors >1:2,4.
d=2 -> v_div=2, need w<2, so w=1? but 1 not present. So none.
d=4 -> v_div=1, need w<1 none.
v=2 divisors >1:2 -> v_div=1, need w<1 none.
Thus max_reduction=0, answer=14. matches.
Example3: 2 4 2 3 7 -> sum=2+4+2+3+7=18? Actually 2+4=6+2=8+3=11+7=18. Output 18, so no reduction. Check.
count:2 appears 2, 3 appears1,4 appears1,7 appears1.
Consider v=4 divisors:2,4. d=2 v_div=2, need w<2 -> w=1? not present. w none. d=4 v_div=1 none.
v=2 divisors:2 -> v_div=1 none.
v=3 d=3 -> v_div=1 none.
v=7 divisors:7 -> v_div=1 none.
Thus no reduction. But is there any operation that reduces sum? Maybe v=4 and w=2? But condition w < v/d: with d=2, v_div=2, w=2 not <2, so fails. So no reduction. Good.
So our simple approach seems correct.
But wait, could there be a scenario where using a divisor d that is not the full factor? Already considered.
Double-check: The operation also allows x to be any divisor of a_i, including maybe d= a_i itself? Then a_i becomes 1, and a_j becomes a_j * a_i. That is allowed because x divides a_i. Then v_div = 1. Need w < 1, i.e., w=0 but w cannot be 0. So no reduction. So not helpful.
But what about cases where we could reduce sum even if condition b < a/d doesn't hold? Our derived condition was for Δ<0. If Δ>=0, sum doesn't decrease. So we only care about negative Δ.
But is there any scenario where moving a factor could reduce sum even if b >= a/d? Let's test: Suppose a=6, b=2, d=3: a/d=2, b*d=6, new sum =2+6=8, old sum=6+2=8, unchanged. If b>a/d, new sum larger. So indeed only b < a/d reduces sum.
Thus our condition is necessary and sufficient.
Thus solution: For each v with count>0, for each divisor d>1 of v, let v_div = v/d. Find minimal w (1 <= w <= 100) such that count[w] >0 and w < v_div. Compute reduction. Keep max.
But careful: If there are multiple machines, we might use the same machine for both? No, they must be distinct. If the minimal w is also the only occurrence of v? But v and w are different values, so they are distinct machines. If count[v]>=1 and count[w]>=1, we can choose one source from v and one target from w. They are different indices. So fine.
One nuance: Could there be a case where the minimal w is present, but the only machine with that w is also the only machine with v? That's impossible because v != w. So they are distinct machines. So no conflict.
Thus answer is total_sum - max_reduction.
But wait, what about using the same value but different indices? As argued, not beneficial for reduction because condition fails. However, could there be a reduction if we use d such that a/d > b? If v = w, then b = v. Condition b < v/d => v < v/d => multiply both sides by d (positive) => v*d < v => d < 1, impossible for d>1. So indeed no reduction possible with same value. So we ignore.
Now, we must consider if there is any possibility to perform two separate operations? No, at most one operation.
Thus algorithm seems straightforward.
But we need to consider that d must be a divisor of a_i. We'll generate divisors for each v from 2 to 100: precompute list of divisors >1.
Implementation steps:
- read n
- read array a
- total = sum(a)
- freq[101] = {0}
- for each a_i: freq[a_i]++
- max_reduction = 0
- for v in 1..100:
if freq[v] == 0: continue
for each divisor d in divisors_of_v where d > 1:
v_div = v // d
# Need smallest w < v_div with freq[w] > 0
# If v_div <= 1, then no w because w>=1, so skip
if v_div <= 1: continue # since w <1 impossible
# iterate w from 1 to v_div-1
found = False
for w in range(1, v_div):
if freq[w] > 0:
reduction = (d - 1) * (v_div - w)
if reduction > max_reduction:
max_reduction = reduction
# Since we are looking for smallest w, we can break after first? Actually reduction larger for smaller w, so first found (smallest) gives max reduction for this (v,d). So we can break inner loop after computing.
break
- print(total - max_reduction)
But careful: For a given (v,d), the smallest w gives the maximum reduction for that (v,d). However, there might be other (v,d) where a larger w still gives a reduction larger than the one from smallest w of other (v,d). But since we are iterating all (v,d) and checking the smallest w, we get the max possible. But we need to confirm that it's always optimal to pick the smallest w for each (v,d). Because for fixed (v,d), reduction is monotonic decreasing with w. So indeed the best for that (v,d) is with the smallest w available. So we only need to consider that.
But is it possible that the smallest w leads to a situation where the source and target are the same machine? No, because w is a value distinct from v.
Thus we can break once we find smallest w.
Complexity: For each v (max 100), for each divisor d (max about 12), inner loop scans from 1 to v_div-1 (max 99). So at most 10012100=120k, trivial.
But we need to consider that there may be multiple values v, and we need to consider that the source and target machines could be the same? Not needed. So fine.
But wait, there is a nuance: The divisor x must be an integer divisor of a_i. That's fine.
Potential edge: What if the smallest w is present, but we need to ensure that after operation, the new values remain positive integers. They will, because a_i divided by d yields integer since d divides a_i, and a_j multiplied by d yields integer. So okay.
Now, is it possible that we could reduce sum by more than this? Let's double-check the derivation. The operation is: pick i, j, and x divisor of a_i. Then a_i' = a_i/x, a_j' = a_j * x. The sum change: Δ = a_i/x + a_j * x - a_i - a_j = a_i*(1/x -1) + a_j*(x-1). Since x >= 1 integer divisor. For x>1, 1/x -1 negative. To have Δ<0, we need a_i*(1 - 1/x) > a_j*(x-1)? Actually Δ = -(x-1)(a_i/x - a_j). Because (1/x -1) = -(x-1)/x. So Δ = a_i * (-(x-1)/x) + a_j(x-1) = (x-1)(-a_i/x + a_j) = (x-1)(a_j - a_i/x). So Δ = (x-1)(a_j - a_i/x). For Δ<0, we need a_j - a_i/x < 0 => a_j < a_i/x. That's our condition. And reduction = -Δ = (x-1)(a_i/x - a_j). So we maximize (x-1)(a_i/x - a_j) given a_i divisible by x, a_j available, and a_j < a_i/x.
Our approach picks a_i = v, x = d, a_j = w. So correct.
Now is there any scenario where picking a non-minimal w could be beneficial because maybe the smallest w is not available due to being the only occurrence and we need to use a different source? But we only need one source and one target. If the smallest w exists, we can use it. The source is from v, which exists. So it's available. There's no restriction linking them other than they are distinct. So we can use any w present.
But what if the smallest w is the only occurrence of w and also the only occurrence of v? That can't happen because v and w are different values. So there is no conflict. If v and w are different, we can pick one machine from each group.
But there is a subtle case: Could it be that for a given (v,d), the smallest w is present, but using it would cause the target's new value w*d to be extremely large, but that does not affect anything else. So fine.
Thus our computed max_reduction should be achievable.
But we might have missed cases where we could achieve reduction by using the same machine? Not allowed as i and j must be distinct. But could we use two machines with same value but different? Already not beneficial.
Now, before finalizing, let's test with some custom examples.
Example: n=2, a=[100,1]. total=101. Can we reduce? v=100, divisors >1: many. d=100 -> v_div=1, no w<1. d=50 -> v_div=2, need w<2, w=1 present. reduction = (50-1)(2-1)=491=49. So new sum =101-49=52. Check: operation: choose i=1 (100), x=50, then a_i becomes 2, j=2 (1) becomes 50. So new values: 2 and 50 sum=52. Good. Could we do better? d=20 -> v_div=5, need w<5, smallest w=1, reduction=(20-1)(5-1)=194=76, sum=25. That seems better: new values: 100/20=5, 120=20, sum=25. reduction 76. d=10: v_div=10, need w<10, w=1, reduction=9(10-1)=81, sum=20. (100/10=10, 110=10, sum=20). Wait, 10+10=20, reduction 81? Actually original sum 101, new sum 20, reduction 81. That's even better. d=5: v_div=20, need w<20, w=1, reduction=4(20-1)=76, sum=25? Actually 100/5=20, 15=5, sum=25, reduction 76. d=4: v_div=25, reduction=3(25-1)=72, sum=29. d=2: v_div=50, reduction=1*(50-1)=49, sum=52. So best is d=10 giving reduction 81. But could we do d=25? 100/25=4, need w<4, w=1, reduction=24*(4-1)=72, sum=29. So d=10 best among these. Actually d=10 gives reduction 9*(10-1)=81. d=8? 100/8=12.5 not integer, so not divisor. So the maximum reduction seems at d=10? Let's check d=20 gave 76, d=25 gave 72, d=50 gave 49, d=100 gave 0. What about d=9? Not divisor. Divisors of 100 >1: 2,4,5,10,20,25,50,100. Among these, compute v_div and reduction with w=1:
d=2: v_div=50 red=149=49
d=4: v_div=25 red=324=72
d=5: v_div=20 red=419=76
d=10: v_div=10 red=99=81
d=20: v_div=5 red=194=76
d=25: v_div=4 red=243=72
d=50: v_div=2 red=49*1=49
So best is d=10. So maximum reduction 81, sum 20. Is it possible to get sum lower? Let's see if we can do better with other combos? Suppose we choose source as 1? v=1 has no divisors>1, so no. So answer 20. Our algorithm: freq[100]=1, freq[1]=1. For v=100, iterate divisors d. For each d, find smallest w < v_div with freq[w]>0. Since only w=1 is present, that works for all d where v_div >1. So we compute reductions as above and pick max 81. So answer=101-81=20. Good.
Now test another: n=3, a=[6,3,2] sum=11. Can we reduce? We have values 6,3,2. Let's examine manually:
Possible source v=6. Divisors:2,3,6.
d=2: v_div=3, need w<3: w could be 2 (or 1 but no 1). w=2 exists. reduction = (2-1)(3-2)=11=1. New values: 6/2=3, 2*2=4 -> [3,3,4] sum=10.
d=3: v_div=2, need w<2: w=1? none, but maybe w=??? 1 not present. So no.
d=6: v_div=1, none.
Source v=3: divisors:3 only. v_div=1 none.
Source v=2: divisors:2. v_div=1 none.
Thus reduction 1. Could there be a better reduction using source=6 and target=3? That would be w=3, but condition w < v_div? d=2 gives v_div=3, w=3 not <3. So no.
What about source=6, d=3, target=2? v_div=2, w=2 not <2. So no.
What about source=3? none.
So max reduction=1, sum=10. But maybe we could choose source=6, d=2, target=3? That yields same as above? Actually target=3 gives w=3, not allowed. So only w=2 works. So answer 10.
Our algorithm: freq[6]=1, freq[3]=1, freq[2]=1. For v=6, d=2, v_div=3, iterate w from 1 to 2: w=1 not present, w=2 present. reduction=1. d=3: v_div=2, w from 1 to1: none. d=6: skip. So max_reduction=1, sum=10.
But is there a better operation: maybe source=6, d=6? no. source=3, d=3? no. So fine.
Now consider a scenario where the smallest w is not the best overall? Actually for a fixed (v,d), yes smallest w yields max reduction. However, what if the smallest w is present, but the reduction from that (v,d) is not as high as using a larger w with a different (v,d) that yields even larger reduction. But we are taking max over all (v,d) and w (smallest for each (v,d) yields the best for that (v,d). So we need to consider all (v,d). But could there be a case where for a given (v,d), the smallest w yields a reduction, but maybe using a larger w with the same (v,d) could produce a larger reduction? No, because reduction decreases as w increases. So the smallest gives the maximum for that (v,d). So we only need to check that.
Thus algorithm correct.
But wait: what about the scenario where we have multiple copies of the same value? For example, v appears multiple times, w appears multiple times. That's fine.
One more nuance: Could we get a reduction by using the same value for source and target if we have at least two copies? We argued no because condition fails. But check if d is not an integer? It must be divisor. So no.
Thus solution is simple.
However, we must consider if we can use a divisor x that is not necessarily the whole d? Actually x is the factor by which we divide. So d = x. So it's exactly what we consider.
Now, is there any scenario where we might want to use a factor x that is not a divisor? The problem says x is a divisor of a_i. So we must ensure a_i divisible by x. So yes.
Now confirm with some edge cases: n=2, a=[2,2]. total=4. count[2]=2. v=2, divisors >1: d=2. v_div=1, need w<1 none. So no reduction. Could we do operation? Choose i and j both 2, x=2, then a_i=1, a_j=4 sum=5, increases. Not beneficial. So answer 4.
Another case: n=2, a=[4,2] total=6. v=4,d=2 v_div=2, need w<2 -> w=1 none. d=4 v_div=1 none. v=2,d=2 v_div=1 none. So no reduction. Could we try something else? x=2 with i=4, j=2 => 2 and 4 sum 6, same? Actually 4/2=2, 22=4 sum=6. So no change. x=4 not allowed for a_i=4? 4 divisor? Yes 4 divides 4, but then 4/4=1, 24=8 sum=9, increase. So answer 6.
But is there a possibility to reduce sum using a factor that is not a divisor? Not allowed.
Thus algorithm stands.
But hold on: Could we consider using a divisor that is 1? That yields no change, so ignore.
Now, is there any possibility to achieve a reduction by choosing x such that x is not a divisor of a_i? The problem says x is an integer such that x is a divisor of a_i. So must be divisor.
Thus solution is straightforward.
But we need to ensure we correctly handle the case where there is no possible reduction: then max_reduction remains 0, answer = total sum.
Now confirm with a more complex scenario: suppose we have many numbers. Could there be a reduction by using a divisor that is not the best for a given v because maybe using a different target w that is not the smallest yields a larger reduction when considering that the same w might be used with a different source? But we only do one operation, so we only pick one pair and one divisor. So for each possible operation, we compute reduction. We want the max reduction over all operations. So we need to consider all combinations of (i,j,d) that satisfy conditions. In our brute, we considered each v (source value) and each divisor d, then we considered the smallest w (target value) because that gives the best reduction for that specific (v,d). However, what if the smallest w is present, but the source v appears only once, and the target w also appears only once, and they are the only machines? That's fine, we can use them. But what if the smallest w is the same as v? We argued it's impossible because w<v_div<v so w<v, so w != v. So no conflict.
Thus our method yields the maximum reduction across all possibilities.
But is it possible that for a given v, using a larger w yields a larger reduction than using the smallest w with a different v? No, we are comparing across all (v,d,w). For each (v,d) we take the best w. Then we take max across (v,d). That's correct.
Now, we must also consider that we could also choose to not perform any operation, so max_reduction could be 0.
Thus answer = total - max_reduction.
Now, let's test some random cases manually to ensure no oversight.
Consider n=3, a=[8,4,1]. total=13. freq:8,4,1.
v=8 divisors:2,4,8.
d=2: v_div=4, need w<4: smallest w=1 present. reduction=(2-1)(4-1)=13=3 -> new sum=10? Actually compute: 8/2=4, 12=2, so numbers: 4,4,2? Wait original: 8,4,1. After op: source=8, target=1, x=2, new: 4 and 2, so values: 4,4,2 sum=10. reduction 3.
d=4: v_div=2, need w<2: w=1 present. reduction=(4-1)(2-1)=31=3 -> new: 8/4=2, 14=4 -> 2,4,4 sum=10 same reduction 3.
d=8: v_div=1 none.
v=4 divisors:2,4.
d=2: v_div=2, need w<2: w=1 present. reduction=(2-1)(2-1)=1 -> new: choose source=4, target=1: 2 and 4? Actually 4/2=2,12=2 -> 8,2,2? Wait careful: If source=4 (value 4), target=1 (value 1), after: source becomes 2, target becomes 2, so numbers:8,2,2 sum=12, reduction=1.
d=4: v_div=1 none.
v=1 none.
So max_reduction=3, answer=10. Could we do better? Perhaps source=8,d=2,target=4? That's w=4 not <4, no reduction. Source=8,d=4,target=4? w=4 not <2. So answer=10. Is there any operation yielding sum 9? Maybe source=8,d=2,target=1 gives sum 10. Source=8,d=4,target=1 gives sum 10. Could we do source=8,d=2,target=1 is best. So correct.
Now consider scenario where multiple w's are present but smallest w yields high reduction but maybe using a slightly larger w with a larger d yields even larger reduction? For fixed v, d larger reduces v_div and increases (d-1) factor. So trade-off. Our method enumerates all d, and for each picks smallest w. That's fine.
Now, is it always optimal to pick the smallest w available? Let's prove: For given v and d, reduction R(w) = (d-1)*(v/d - w). Since v/d is constant, R decreases linearly as w increases. So maximum is at smallest possible w that satisfies w < v/d. Therefore yes.
Thus algorithm is correct.
But wait, could there be a scenario where we have multiple source values v, and we might need to consider using a particular w that is the smallest for some (v,d) but that w is also the only occurrence, and that same w might be needed as source for a different operation that yields even larger reduction? But we only perform one operation, so we don't care about conflicts across operations. So for each candidate operation, we assume the two machines are available. So no conflict.
Thus answer = total - max_reduction.
However, there is one more nuance: The operation requires that we choose one machine to reduce and one to increase. Could we also choose the same machine? It says "choose one machine and reduce the power of its machine by x times, and at the same time increase the power of one another machine by x times". "Another machine" clearly means a different machine. So i != j.
Thus done.
Now, we must implement.
But note constraints: n up to 5e4, a_i up to 100, but total sum can be up to 5e4*100=5e6, which fits in int (32-bit signed up to 2e9). So Python int fine.
We'll precompute divisors for numbers 1..100. For each v, store list of divisors >1.
Implementation steps:
-
Read n.
-
Read list a.
-
freq = [0]*101
-
for val in a: freq[val] +=1
-
total = sum(a)
-
Compute divisors: for v in range(1,101): list = [d for d in range(2, v+1) if v % d == 0]
-
Alternatively, precompute all divisors for each v.
-
max_red = 0
-
For v in range(1,101):
if freq[v] == 0: continue
for d in divisors[v]:
v_div = v // d
if v_div <= 1: continue # because w must be at least 1 and <1 impossible
# find smallest w with freq[w] >0 and w < v_div
# Since v_div <=100, we can just loop w from 1 to v_div-1:
for w in range(1, v_div):
if freq[w] > 0:
reduction = (d - 1) * (v_div - w)
if reduction > max_red:
max_red = reduction
break # since we found smallest w, no need to check larger w for this (v,d)
-
print(total - max_red)
But we must also consider the possibility that we could use the same v but different target w that is not the smallest, but maybe the smallest w is not available because we need distinct machines and maybe the only machine with that w is the same as the only machine with v? However, as argued, v and w are different values, so they are distinct machines. But there is a corner case: if v and w are different values but there is only one machine of each, that's fine. However, what if the only machine with value w is also the only machine with value v? That can't happen because a machine has a single value. So distinct.
Thus, no problem.
But there is another nuance: We need to ensure that the source machine and target machine are distinct. If v and w are different, we have at least one machine for each (since freq[v]>0 and freq[w]>0). They are distinct because they have different values, so indices are different. So fine. If v and w are same? Not possible for reduction.
Thus algorithm complete.
Let's test some more scenarios.
Case: freq of some value may be high, but we only need at least one. So no problem.
Now, what about using a divisor d that is not a prime? Our enumeration covers all divisors.
Now, double-check the example where v=100, d=10, w=1 gave reduction 81. Could there be a reduction using w=2? w=2 is also present (if we had freq[2]>0). In our test we only had 1 and 100. So smallest w was 1, that gave max. If we had both 1 and 2 present, then using w=1 gives higher reduction than w=2. So we would pick 1.
Now consider a scenario where v=90, d=9, v_div=10. If smallest w is 2, reduction = (9-1)(10-2)=88=64. If w=1 is also present, reduction =8*9=72 better.
Thus picking smallest w is optimal.
Now, could there be a scenario where there is no w such that w < v_div? Then skip.
Now, let's test a case where we have many numbers and the best operation might involve not the smallest w because the smallest w may not be available for that source? Actually if the smallest w is present, it's available. But what if the source v is the same as the smallest w? That can't happen because w < v_div <= v/2 (since d>=2) so w < v/2, thus w cannot equal v. So distinct. So no conflict.
Thus algorithm stands.
But wait: Is there a scenario where for a given v and d, the smallest w is present, but using that w as target might cause the new value of the target to be w*d, which could be extremely large, but that doesn't affect anything else. So fine.
Now, one more nuance: The problem says "choose an arbitrary integer x, then choose one machine and reduce the power of its machine by x times, and at the same time increase the power of one another machine by x times (powers of all the machines must stay positive integers). Note that he may not do that if he wants. More formally, 2D can choose two such indices i and j, and one integer x such that x is a divisor of a_i, and change powers as following: a_i = (a_i)/(x), a_j = a_j ⋅ x". So x must be a divisor of a_i. That's what we used.
Now, check boundary: a_i up to 100, so v up to 100. So our loops are fine.
But could there be a scenario where x = 1 is allowed? That yields no change; we ignore.
Now, does the operation allow x to be any integer divisor, including possibly a_i itself? Yes.
Now, think about possible improvement: Could we also consider the reverse direction? That is, reducing a_j and increasing a_i? But operation is asymmetric: we choose one machine to reduce and another to increase. So we consider all i as source (the one we divide) and j as target (multiply). So we covered all possibilities by iterating over v (source) and w (target). So fine.
Now, confirm with the examples.
Example 1: We got 14.
Example 2: 14.
Example 3: 18.
Now, need to ensure we consider all v and all d. However, we also need to consider that the source and target could be the same index? Not allowed.
Now, what about the case where we have only one machine of a certain value v, and the only w available is also the only machine of some other value. That's fine; we have two distinct machines.
Now, consider scenario where all numbers are equal, e.g., [5,5,5]. freq[5]=3. v=5, divisors>1:5 only (since 5 is prime). v_div=1, no w. So no reduction. Could there be reduction using something else? v=5,d=5? no. So answer sum=15. Is that minimal? Could we do an operation? If we take i and j both 5, x=5, then 5/5=1, 5*5=25, sum=1+25+5=31, increased. If x=1, no change. So indeed no reduction. So answer 15.
Now, scenario: [2,2,1]. freq:2 x2, 1 x1. v=2,d=2: v_div=1 none. v=1 none. So no reduction. But could we do: source=2 (one of them), target=1, x=2? 2/2=1, 1*2=2 -> we get [1,2,2] sum=5, original sum=5, same. So no reduction. So answer 5.
Now, scenario: [4,1,1]. sum=6. v=4,d=2: v_div=2, need w<2: w=1 present (two copies). reduction=(2-1)(2-1)=1, new sum=5 (e.g., 4/2=2, 12=2 -> [2,2,1] sum=5). d=4: v_div=1 none. So answer 5. Could we get lower? v=4,d=2 with w=1 yields 5. What about using w=1? That's the only w. So answer 5.
Now, scenario: [6,2,1]. sum=9. v=6 divisors:2,3,6. d=2: v_div=3, w<3: w=1 or 2. smallest w=1. reduction=1*(3-1)=2? Actually (2-1)(3-1)=12=2 -> new sum=7. d=3: v_div=2, w<2: w=1. reduction=2*(2-1)=2 -> new sum=7. d=6: v_div=1 none. So answer 7. Could there be better using source=2? v=2,d=2: v_div=1 none. So answer 7.
Now, more complex: [12,3,1]. sum=16. v=12 divisors:2,3,4,6,12. compute each with w=1 (only w=1 present).
d=2: v_div=6, reduction=1*(6-1)=5 => sum=11
d=3: v_div=4, reduction=2*(4-1)=6 => sum=10
d=4: v_div=3, reduction=3*(3-1)=6 => sum=10
d=6: v_div=2, reduction=5*(2-1)=5 => sum=11
d=12: v_div=1 none.
So best reduction=6, sum=10. Check manually: if d=3: 12/3=4, 13=3 -> [4,3,3]? Wait original: 12,3,1. Choose source=12, target=1, x=3, get 4 and 3 => numbers: 4,3,3 sum=10. Yes. Could we get sum 9? Try source=12, target=3? w=3, condition w < v_div? For d=3, v_div=4, w=3<4 true! Actually w=3 is also present. reduction= (3-1)(4-3)=2*1=2 => sum=14, worse. Using w=3 with d=4: v_div=3, w=3 not <3. So no. So best is 6. So answer 10. Our method: for v=12,d=3, smallest w is 1, reduction=6; also we should check w=3? But smallest w is 1, so we would compute reduction=6. That's max. So good.
But note: In this case, the smallest w=1 yields reduction 6. If we used w=3, reduction is 2. So we automatically pick w=1.
Now, what if w=1 is not present? Then smallest w may be larger.
Thus algorithm stands.
Now, let's check for potential pitfalls: Since we only consider smallest w for each (v,d), we might miss a scenario where for a given (v,d), the smallest w exists, but we cannot use it because it's the only occurrence and that same machine might also be the only occurrence of v? But they are different values, so distinct machines. However, consider if v and w are different but both values appear only once, and those two machines are distinct. That's fine. So no conflict.
But wait: The source machine must have value v, the target machine value w. If there is only one machine with value v and only one with value w, we have two distinct machines. Good.
Now, what if the smallest w is also the only occurrence, and v also appears only once, they are distinct, fine.
Now, what if there is a case where the smallest w is the same as the source machine's value? Not possible as argued.
Thus algorithm is correct.
But before finalizing, let's consider if we should also consider the possibility of using the operation in the "reverse" direction: i.e., we might want to reduce a machine and increase another, but we treat i as the one being divided. Could there be a situation where reducing a machine with a smaller value and increasing a larger value leads to sum reduction? Let's check: condition for reduction is a_j < a_i / x. So the source a_i must be larger enough relative to target a_j. So source should be larger. So we only consider source values v that are larger than some threshold. So our iteration over v as source covers all possibilities.
Now, let's also think about if we could reduce sum by doing nothing? That's default.
Thus answer = total - max_reduction.
But hold on: There's also possibility that performing operation could increase sum, but we don't care.
Now, we need to ensure that after operation, all powers stay positive integers. Since x divides a_i, a_i/x is integer positive. a_j*x is integer positive. So fine.
Now, complexity: O(100 * d_max * 100) = O(1e5) per test, trivial.
Now, the problem has n up to 5e4, a_i up to 100, but we could also have numbers up to 100. So fine.
Now, let's test a more complex scenario where there are many numbers and the best reduction might involve not using the absolute smallest w because maybe for that (v,d) the smallest w is very small but the reduction is not as good as using a larger w with a different (v,d). But we are considering all (v,d) and for each we take the smallest w. So we compare across all.
Now, we also need to consider that if there are multiple copies of the smallest w, it's fine.
Now, one more nuance: The problem states "2D can at most once choose an arbitrary integer x, then choose one machine and reduce the power of its machine by x times, and at the same time increase the power of one another machine by x times". It says "choose an arbitrary integer x". But then says "x is a divisor of a_i". So x must be divisor. So that's covered.
Now, we need to output minimum total power.
Now, what about the possibility of using x=1? That yields no change, same as doing nothing.
Now, consider if there is a scenario where we can reduce sum by more than one operation? No, at most one.
Now, verify with some random brute force for small n to ensure algorithm matches exhaustive search. We could mentally test a few.
Take n=3, a=[6,3,2] we did, answer 10 (since sum 11? Actually sum 6+3+2=11, reduction 1 => 10). But could we get sum 9? Let's try all possibilities:
i=1 (6), j=2 (3), x must divide 6: possible x=2,3,6.
x=2: new 3 and 6 => [3,6,2] sum=11.
x=3: new 2 and 9 => [2,9,2] sum=13.
x=6: new 1 and 18 => [1,18,2] sum=21.
i=1