We need to minimize sum after repeatedly applying operation: pick i, j with xi > xj, set xi = xi - xj. Equivalent to we can subtract smaller numbers from larger numbers repeatedly. This is reminiscent of the Euclidean algorithm to reduce numbers to multiples of the GCD. Actually the set of reachable states corresponds to numbers that are multiples of the GCD? Let's think.
Consider the process: Starting with multiset of positive integers, repeatedly replace a larger number by the difference between it and a smaller number. This is similar to the classic "water pouring" or "subtraction" game that yields numbers that are linear combinations with coefficients integer (not necessarily non-negative?) Actually each operation replaces xi with xi - xj, where xj is unchanged. So the set of numbers remain positive integers? Since we require xi > xj to perform operation, after operation xi becomes positive (since > xj, so difference >0). So all numbers remain positive integers. The operation preserves the sum of all numbers? Let's compute: Sum changes: originally total S. After operation, new xi = xi - xj, so total becomes S - xj. So the sum decreases by xj. So each operation reduces the total sum by the smaller number. So we want to minimize sum by appropriate sequence. Since sum always decreases (by positive amounts), we want to make it as small as possible. However there is a lower bound: The numbers remain positive and any common divisor property? Let's analyze invariants.
Consider the gcd of all numbers. Initially gcd = g. Does operation preserve gcd? If we have numbers a, b (a>b), then we replace a with a - b. The gcd of the set {a-b, b, others} is still the same as gcd of {a,b,...} because gcd(a,b) = gcd(a-b,b). So gcd is invariant. Also the numbers remain multiples of g. So each number is at least g? Not necessarily; they are positive multiples of g. So the minimal possible sum is at least n * g? Since each number must be a positive multiple of g, the smallest positive multiple is g itself. But can we achieve all numbers equal to g? Possibly, but we need to check constraints: operation only subtracts, never adds. So we can only reduce numbers, never increase. Starting from initial numbers (multiples of g), can we reduce all to exactly g? That would give sum = n * g. But is that always attainable? The classic problem: Given a set of numbers, by repeatedly replacing larger by difference with smaller (like Euclid's algorithm), you can obtain numbers that are multiples of the gcd, but you might be able to reduce them further? Actually think about two numbers: a, b. Using the operation: if a > b, replace a with a - b, repeat. This is exactly the Euclidean algorithm for computing gcd, but the process continues until both numbers become equal to gcd? Actually the standard subtraction algorithm: start with (a,b). Repeatedly subtract the smaller from the larger. This process yields the gcd: eventually both numbers become equal to gcd. For example a=12, b=18. Process: 12,18 -> (12,6) (since 18-12=6) -> then (6,6) (since 12-6=6) -> both 6. So we can get both numbers equal to gcd. But note sum: initially 30, after first operation sum = 30-12=18, after second sum = 18-6=12? Wait check: Start (12,18): total=30. Operation: subtract smaller (12) from larger (18): new numbers (12,6) sum = 18. Operation: now larger is 12, smaller 6, subtract: (6,6) sum = 12. So final sum = 12 = 2gcd (gcd=6). So yes, minimal sum for two numbers is 2gcd.
But can we do better than ng? Could we have sum less than ng? Since each number must be positive multiple of g, the smallest possible value for each is g. So minimal sum is at least ng. So ng is lower bound. But is it always achievable? For n=2, yes. For n>2, need to see if we can reduce all numbers to exactly g. The invariant is gcd stays g. So if we manage to make all numbers equal to g, sum = ng. But can we always achieve that? Consider three numbers: (2,4,6). gcd=2. Could we get all to 2? Let's try: (2,4,6). We can subtract: 6-4=2 => (2,4,2) sum=8. Then subtract 4-2=2 => (2,2,2) sum=6. So yes, sum=6 = 32. So minimal sum 6, matches example 2. Another example: (12,18) gcd=6 -> minimal sum 12 = 26, matches example 3. Example 1: (1,2) gcd=1 -> minimal sum = 21=2, matches. Example 4: (45,12,27,30,18) gcd? 45,12 -> gcd=3? 45%12=9 -> 12%9=3 -> 9%3=0 -> gcd 3. 27 divisible by 3, 30,18 also. So gcd=3. Minimal sum would be 5*3 = 15, matches example output. So indeed the answer seems to be n * gcd(x1,...,xn). But is it always the case? We need to ensure we can always reduce all numbers to exactly the gcd. This is a known result: Using the operation of subtracting a smaller number from a larger one, you can obtain multiples of the gcd, but can you always achieve the exact gcd for all numbers? There might be constraints: The operation subtracts exactly the smaller number, not any multiple. However, we can simulate Euclidean algorithm stepwise. For multiple numbers, we can reduce each number to a multiple of the gcd, but maybe some numbers get stuck at larger multiples? Let's try a tricky case: n=3, numbers (6,10,15). gcd=1. Can we reduce all to 1? Let's attempt. We need to produce a 1 from these numbers by subtraction. Since gcd=1, there is a combination that yields 1, but via allowed operations? Typically, the set of numbers reachable from the initial set through such operations is the set of positive integers that can be expressed as a linear combination with coefficients from Z? But each operation is like a transformation on the vector: it's essentially applying elementary operations that preserve the gcd. Actually it's akin to the subtractive Euclidean algorithm in multiple dimensions. The process corresponds to the set of numbers that can be obtained as non-negative integer combinations? Let's think.
The operation: pick i, j with xi > xj, set xi = xi - xj. This is similar to the step in the Euclidean algorithm but limited to subtracting the smaller from the larger, not subtracting multiples. However, by repeated subtraction, you can effectively compute the remainder of division: you can subtract the smaller from the larger repeatedly until the larger becomes smaller, which is essentially the modulo operation if you perform many steps. But the problem allows any number of operations, so we can effectively perform modulo operation by a sequence of subtractions. So we can, for two numbers a and b, reduce the larger to a mod b (if a >= b, by subtracting b repeatedly). However, note that we cannot subtract multiple copies at once; we need to do it stepwise, but that's fine. So we can simulate Euclidean algorithm.
Thus, for multiple numbers, we can compute the gcd by repeatedly taking two numbers and reducing one by the other. Since we can always reduce any number to its remainder modulo another number (by repeated subtractions), we can eventually reduce all numbers to the gcd. But is it always possible to make all numbers exactly equal to gcd? Let's consider an approach: we can compute gcd of all numbers by pairwise reduction. For n numbers, we can use the following: While not all numbers equal, pick the maximum and the minimum (positive). Replace the maximum by max % min, but if max % min == 0, then it becomes min? Actually if max is divisible by min, then after repeated subtractions you'll get min? Wait: If max is a multiple of min, then repeatedly subtract min until it becomes 0? But we cannot have zero because we always require xi > xj, and after subtracting the last time, if xi becomes equal to xj? For example, if a = 2, b = 2, we cannot subtract because xi > xj condition fails. If a = 2, b = 2, both equal, we cannot perform operation. Also we cannot get to zero because if xi > xj, subtract yields xi - xj > 0, so positive. So if max is multiple of min and max > min, after repeatedly subtracting min you eventually get either min (if max = k*min, then after k-1 subtractions you get min? Let's simulate: a=6, b=2: initially a=6,b=2. Operation: 6>2, set a=4. Now (4,2). 4>2 -> a=2. Now (2,2). Both equal, stop. So we ended with both = min (2). So we never get zero. So any number can be reduced to min if it's a multiple? Actually if it's exactly equal to min, we stop; if it's multiple, we end with min. So we can achieve min. So in general, the process can reduce numbers to a common divisor. And we can reach the gcd.
But is it always possible to get all numbers exactly equal to gcd? For two numbers, yes. For three or more, maybe there is a pathological case where some numbers become stuck at values larger than gcd while others are gcd? Let's try to find a counterexample. Suppose numbers are (6,10,15). gcd=1. Can we get all to 1? Let's attempt using systematic reduction.
We have 6,10,15. We want to generate 1. Use Euclid: 15-10=5 -> (6,10,5). Then 10-6=4? But 10>6 -> (6,4,5). Then 6-5=1? 6>5 -> (1,4,5). Now we have 1,4,5. Now we can try to reduce the others using 1: subtract 1 repeatedly from any larger number: 4-1=3 -> (1,3,5) -> 5-1=4 -> (1,3,4) -> 4-1=3 -> (1,3,3) -> 3-1=2 -> (1,2,3) -> 3-1=2 -> (1,2,2) -> 2-1=1 -> (1,1,2) -> 2-1=1 -> (1,1,1). This seems doable, as long as we have a 1, we can reduce any number to 1 by repeated subtraction of 1 (since we can always subtract 1 from a larger number, but we need a number equal to 1 to subtract. And we have 1, so we can subtract it from any number >1. So eventually all become 1. So yes, once we have one number equal to gcd, we can reduce all others to gcd by repeatedly subtracting that gcd. So the challenge is to produce one number equal to gcd. Is it always possible to produce gcd from the set? Since the gcd of the set is g, there exists a combination of the numbers that yields g as integer linear combination with integer coefficients. But can we produce g through allowed operations? This is akin to the "subtractive Euclidean algorithm" which yields the gcd for two numbers. For multiple numbers, we can pairwise reduce to eventually get the gcd. So we can always produce g.
Consider an approach: compute gcd of all numbers, answer = n * gcd. But is there any scenario where we cannot achieve exactly g for all? For example, what if some numbers are equal initially? Suppose numbers: (2,2). gcd=2, n=2, sum=4. But can we achieve sum 4? Actually initial sum =4. Since operations require xi>xj, we cannot perform any because they are equal. So the minimal sum is 4, which is 2gcd. So it matches. If numbers all equal, sum is already ng, and no operation possible, so minimal sum = n*g. Good.
What about numbers: (3,3,3)? gcd=3, n=3, sum=9. Already minimal.
But what about numbers where gcd=1 but we cannot get 1 because of parity? Let's try (2,4,6). gcd=2, not 1. Try (4,6,8). gcd=2. Should we be able to get all 2? Let's test: (4,6,8). Compute: 8-6=2 => (4,6,2). Then 6-4=2 => (4,2,2). Then 4-2=2 => (2,2,2). Works. Try (6,9,12). gcd=3. 12-9=3 -> (6,9,3). 9-6=3 -> (6,3,3). 6-3=3 -> (3,3,3). Works.
Try (6,10,14). gcd=2. Can we get all 2? 14-10=4 -> (6,10,4). 10-6=4 -> (6,4,4). 6-4=2 -> (2,4,4). Then 4-2=2 -> (2,2,4). 4-2=2 -> (2,2,2). Works.
Try (9,15,21). gcd=3. 21-15=6 -> (9,15,6). 15-9=6 -> (9,6,6). 9-6=3 -> (3,6,6). Then 6-3=3 -> (3,3,6). 6-3=3 -> (3,3,3). Works.
Seems always possible. The reason: the set of numbers form a vector space over Z? Actually, the operation is analogous to the set of numbers being closed under subtraction (but only positive). This is a classic problem: "Given a set of numbers, you can repeatedly replace any number by the absolute difference of two numbers" or "replace a larger by the difference". It's known that the minimal achievable positive number is the gcd, and you can make all numbers equal to the gcd.
But careful: The operation here is not symmetric: we replace the larger by the difference, not the smaller. However, similar result holds.
Proof sketch:
- The gcd g is invariant.
- We can produce g: by focusing on two numbers, we can apply Euclidean algorithm to reduce their gcd to g. But need to ensure we don't interfere with other numbers? Actually we can always isolate two numbers and perform operations only involving those two. The operation uses two indices; we can choose i and j arbitrarily among the set. So we can focus on two numbers and perform steps that would produce their gcd, while other numbers remain unchanged. For two numbers a,b, by repeatedly subtracting the smaller from the larger, we can reduce them to (g,g). This is a known property of the subtraction algorithm. (Proof: The subtraction algorithm eventually yields the gcd, and both numbers become gcd.) So we can take any two numbers and reduce them to both equal to g. Then with these two g's, we can reduce other numbers by subtracting g repeatedly to make them also g, because if a number > g, we can subtract g (since g>0 and smaller) until it becomes g (or less? But if it's a multiple of g, it will become exactly g after enough subtractions: if x = kg, subtract g repeatedly (k-1) times to get g). But careful: the operation subtracts the smaller number exactly; we have a number g (the subtractor) which is g, and the target number is > g. However, we need to ensure that during subtraction, the target number stays > g to continue? Actually we need to subtract g repeatedly. Starting with x = kg, k>=2. Do operation: x - g = (k-1)*g. This is still >= g. If (k-1)*g > g, we can subtract again; if (k-1)*g = g, then after operation we have two numbers both g, and operation stops because no larger > smaller? Actually when we have (g, g), we cannot subtract because need >. So we can proceed until we get g. So it's fine.
Thus, we can achieve all numbers equal to g. Therefore minimal sum = n * g.
But wait: Is there any nuance? The operation requires xi > xj. If we have two numbers a and b, to reduce them to gcd we need to sometimes subtract a number that is not the minimum of the whole set but we are allowed to pick any pair as long as xi > xj. While performing Euclidean algorithm on two numbers, we may need to subtract the smaller from the larger multiple times. That's allowed because we can repeat. However, there is a subtlety: The Euclidean algorithm using subtraction only (not modulo) can take many steps, but that's okay.
Thus answer is straightforward: compute gcd of all numbers, multiply by n.
Double-check with examples: matches.
But before finalizing, consider potential pitfalls: The input constraints are small, but answer may be up to? n max 100, xi max 100, so maximum gcd is 100, maximum sum is ngcd <= 100100=10000, but initial sum could be up to 100100=10000. Minimal sum = ngcd could be as low as n*1 = n, which fits within int.
Now we must confirm that the process indeed can always achieve the gcd for all numbers. Let's formalize: Let S be the set. Define g = gcd(S). We claim we can transform S into the multiset consisting of n copies of g. Steps:
- Pick any two numbers a and b from the set. By repeatedly applying the operation on the pair (a,b) (i.e., always subtract the smaller from the larger), we can reduce both to g. More precisely, we can simulate the Euclidean algorithm: while a != b, if a > b, replace a with a - b; else replace b with b - a. This classic algorithm yields both numbers equal to gcd(a,b). However, we must be careful that when we have other numbers present, we can still perform these operations using only indices i and j that refer to the two numbers, ignoring others. The operation only affects one of them (the larger). So we can treat the pair isolatedly. So after enough steps, we end with both a and b equal to d = gcd(a,b). However, note that d might be larger than g. Since g is gcd of all numbers, d is a multiple of g? Actually gcd(a,b) could be greater than g? Because g is the gcd of all numbers, so gcd(a,b) is at least g, but might be larger. For example, numbers (6,10,15). g=1. If we pick a=6, b=10, gcd(6,10)=2, which is >1. Reducing them to 2 gives (2,2,15). Then we have gcd(2,2,15)=1, but now we have numbers not all divisible by g? Actually 2 and 2 are divisible by 1, okay. But we still have 15. Now we need to get a 1. Can we get 1 from (2,2,15)? Possibly by mixing: 15-2=13 -> (2,2,13); 13-2=11 -> (2,2,11); ... that might take many steps but eventually produce 1? But does the algorithm guarantee that we can get down to 1? In (2,2,15), gcd=1, but both 2's are even, 15 odd. To get 1, we need to produce an odd number from subtraction? 15-2=13 odd, 13-2=11 odd, continue subtract 2 yields odd numbers decreasing by 2: 9,7,5,3,1. So we can produce 1. So indeed, after reducing two numbers to their gcd (which might be > g), we still can continue to eventually achieve g overall. But is it always possible? Maybe it's easier: we can directly aim to produce g from the entire set without first reducing any two numbers to a larger gcd. The typical strategy: While there exists at least two numbers that are not equal to g, pick the maximum number (which is > g) and subtract from it some other number that is also > g? But we need to ensure progress.
But to prove that we can get all numbers to g, we can use the following: For any multiset with gcd = g, we can perform operations that do not increase the maximum, and eventually reduce all numbers to g. One constructive method: Use the classic algorithm to compute gcd of whole set: repeatedly pick two numbers and replace the larger with the difference. This process will eventually make all numbers equal to g. This is known as the "reduction" process for the "Stone Game"? There's a known problem: "Given numbers, you can replace any number by the absolute difference of two numbers" leads to all numbers becoming the gcd. But here it's not absolute difference; it's subtract the smaller from the larger. However, the result is similar.
Better to rely on known result: The set of numbers that can be obtained as a linear combination with coefficients 0/1? But anyway, given the examples and reasoning, answer is n * gcd.
But let's test more random cases with brute force for small n to be sure. Since constraints are small we could simulate all possible sequences? But state space can be large. However we can reason.
But there is a potential counterexample: what if we have numbers that are multiples of g but we cannot get exactly g because we might get stuck with numbers all equal to some multiple > g? Could it be that the only reachable configurations are those where all numbers are multiples of g, but also the sum is invariant modulo something? Let's see if there is any invariant beyond gcd. Each operation subtracts xj from xi. The sum decreases by xj. So modulo something? Perhaps the parity of sum? Not invariant.
Alternatively, the multiset of numbers modulo g? Since all numbers are multiples of g, the operation subtracts multiples of g, so all numbers remain multiples of g. So the state space is multiples of g.
Now consider the concept of the "total sum" being minimized. The minimal sum is at least ng. We need to show we can achieve exactly ng. This is equivalent to saying that we can reach a configuration where all numbers are equal to g. Because if all numbers are g, sum = n*g, and cannot be lower.
So question reduces to: Given positive integers all divisible by g, with gcd = g, can we transform via allowed operations to all numbers equal to g? I think yes.
Proof: We can use induction on the sum of numbers divided by g (i.e., total "mass"). Let S = sum_i (xi/g). Since each operation reduces total sum by xj, the scaled sum reduces by (xj/g). So we have a decreasing measure. The minimal configuration (with all numbers = g) has scaled sum = n. We need to show that from any configuration not all g, we can perform an operation that reduces some xi to a smaller positive multiple of g, while maintaining gcd = g (invariant). And we can eventually get to all g. However, there is a risk of getting stuck in a local minimum where all numbers are multiples of g but not all equal and no operation possible? Operation requires xi > xj. So if all numbers are equal, no operation possible. If they are not all equal, there exists a maximum > minimum. Then we can subtract the minimum from the maximum? But careful: we need xi > xj. If we pick maximum and minimum, max > min, so operation possible. Subtract min from max. This reduces the max, possibly reducing the sum. Will this always lead eventually to all equal to g? Not obviously; there could be cycles? But since sum strictly decreases, no cycles. So we just need to argue that repeated application of "subtract the smallest from the largest" (or any valid operation) will eventually lead to all numbers equal to g. But is it guaranteed that the process of always subtracting the smallest from the largest converges to all numbers equal to the gcd? This is reminiscent of the "Euclidean algorithm" for multiple numbers, known as the "pairwise reduction" algorithm. I recall a known problem: "Given a set of numbers, you can replace any two numbers by their difference; eventually all numbers become equal to the gcd." But there the operation is different: you replace the two numbers by their difference and the other number remains? Actually in some problems, you can replace (a,b) with (|a-b|, min(a,b))? But here we replace one number with the difference, keep the other unchanged. That is similar but asymmetric.
Let's test a tricky scenario: numbers (6,10,15). If we always subtract smallest from largest, would we eventually get all 1? Let's simulate: Start (6,10,15). Max=15, min=6 -> subtract: 15-6=9 -> (6,10,9). Max=10, min=6 -> subtract: 10-6=4 -> (6,4,9). Max=9, min=4 -> subtract: 9-4=5 -> (6,4,5). Max=6, min=4 -> subtract: 6-4=2 -> (2,4,5). Max=5, min=2 -> subtract: 5-2=3 -> (2,4,3). Max=4, min=2 -> subtract: 4-2=2 -> (2,2,3). Max=3, min=2 -> subtract: 3-2=1 -> (2,2,1). Max=2, min=1 -> subtract: 2-1=1 -> (1,2,1) -> now (1,2,1). Max=2, min=1 -> subtract: 2-1=1 -> (1,1,1). So yes.
What about (2,4,6)? Already works.
What about (4,6,8)? Simulate: (4,6,8) -> max=8, min=4 -> 8-4=4 -> (4,6,4) -> (4,6,4) max=6 min=4 -> 6-4=2 -> (4,2,4) -> max=4 min=2 -> 4-2=2 -> (2,2,4) -> max=4 min=2 -> 4-2=2 -> (2,2,2). Works.
So the naive algorithm seems to work. But is it always true? There is a known property: The "subtractive Euclidean algorithm" for multiple numbers converges to the gcd. Indeed, if you repeatedly replace the largest number by the difference with the smallest, the set's gcd remains unchanged, and the sum strictly decreases, so it must terminate. Termination occurs when all numbers equal. When all numbers equal, they must be the gcd (since the gcd of the set is that common value). So eventually all become g. So the algorithm works.
Thus answer = n * gcd.
But wait, there is nuance: In the operation, we must have xi > xj, not xi >= xj. So when numbers become equal, we cannot act further. So if they become all equal to some d > g, is that possible? That would mean gcd = d, but the gcd invariant says d must be a divisor of g? Actually gcd is invariant, so if all numbers become equal to d, then the gcd of the set is d. But initially gcd = g, so d must equal g. So if the process terminates with all numbers equal, they must be equal to g. So any terminal state (where no operation possible) has all numbers equal. And indeed, if not all equal, there is always an operation (max > min). So the process will continue until all equal, which must be the gcd. So regardless of sequence, if we can eventually reach a terminal state, it will be all equal to g. But is it guaranteed that we can reach a terminal state? Since sum strictly decreases and is bounded below by ng (since each number >= g), the process must eventually stop; i.e., there is no infinite decreasing sequence of positive integers. So we will eventually reach a state where no operation is possible, which is when all numbers are equal. Therefore, from any initial configuration, there exists a sequence of operations that leads to all numbers equal to g? But careful: The process may not be forced; we can choose operations. However, we need to show that there is at least one sequence that leads to termination. Since we can always perform the operation as long as not all equal, and the sum strictly decreases, and the number of possible states is finite? The sum decreases, but there could be many possible states; however, since sum is a positive integer that strictly decreases each step, any sequence of operations cannot have more steps than initial sum. So any sequence that always picks a valid operation will eventually terminate because sum cannot go below ng. But we must ensure that we never get stuck in a situation where no operation is possible but numbers are not all equal? Is that possible? The condition for no operation possible is that for all i, j, we cannot have xi > xj. That means all numbers are equal. Because if there exists i and j with xi != xj, then the larger is > the smaller, so an operation exists. So terminal states are exactly states where all numbers are equal. So any sequence that continues while possible will terminate at an all-equal state. So any maximal sequence ends with all equal numbers. Since gcd is invariant, that common number must be g. Therefore, from any initial configuration, by repeatedly applying any valid operation (as long as we can), we will eventually reach the state where all numbers = g. But is it guaranteed that we will not get stuck early due to inability to continue even though numbers are not all equal? That would require that for all pairs xi > xj is false, i.e., there is no pair with xi > xj. That implies all numbers are equal. So as long as there is any inequality, we can pick a pair where the larger is > the smaller and perform operation. So we can always continue until all equal. So indeed, starting from the initial state, we can simply repeatedly choose any pair with larger > smaller (e.g., max and min) and perform operation. This algorithm will eventually terminate at all equal numbers = g. So it's always possible to achieve sum = n*g.
But wait: Consider a scenario where numbers are not all multiples of g? But they are, because gcd invariant ensures they remain multiples of g. However, at start they are arbitrary integers; gcd g might not divide all numbers? Actually by definition of gcd, g divides each xi. So yes.
Thus, minimal sum = n * gcd. However, is it possible that by some clever sequence we could achieve sum less than n*g? That would require at least one number less than g, but since all numbers remain multiples of g and positive, the smallest positive multiple is g. So no.
Hence answer is n * gcd.
Let's verify with edge cases: n=2, x1=1, x2=1. gcd=1, sum=2*1=2. But initial sum=2, already minimal. Operation not possible because 1>1 false. So minimal sum =2 correct.
n=2, x1=1, x2=3. gcd=1, answer=2. Can we achieve sum 2? Let's try: (1,3) -> subtract 1 from 3 (3>1) -> (1,2) sum=3. Then subtract 1 from 2 -> (1,1) sum=2. So yes.
n=3, (2,2,3). gcd=1, answer=3. Can we? (2,2,3) -> subtract 2 from 3 -> (2,2,1) sum=5. Then subtract 1 from 2 -> (2,1,1) sum=4. Then subtract 1 from 2 -> (1,1,1) sum=3. Works.
n=3, (4,8,12). gcd=4, answer=12. Can we? (4,8,12) -> subtract 4 from 12 -> (4,8,8) sum=20. subtract 4 from 8 -> (4,4,8) sum=16. subtract 4 from 8 -> (4,4,4) sum=12. Works.
All good.
Thus solution: read n, read array, compute gcd of all numbers, output n * gcd.
Implementation: Use Euclidean algorithm for gcd (iterative, gcd(0,a)=a). Compute gcd as accumulate from first element.
Edge cases: n up to 100, numbers up to 100, no overflow.
Check sample: 5 numbers 45 12 27 30 18 -> gcd? compute: gcd(45,12)=3; gcd(3,27)=3; gcd(3,30)=3; gcd(3,18)=3. So gcd=3, n=5 -> 15, matches.
Thus answer is straightforward.
But wait, is there any twist? Let's re-read problem statement: "Ciel has n positive integers: x1, x2, ..., xn. She can do the following operation as many times as needed: select two different indexes i and j such that xi > xj hold, and then apply assignment xi = xi - xj. The goal is to make the sum of all numbers as small as possible." This is exactly what we used.
Thus solution.
But we must be careful: Some might think answer is sum of numbers after performing operations optimally. But our reasoning shows minimal sum is n * gcd. However, is it always achievable? Need to ensure that during operations, we always maintain positivity and condition xi > xj. As we argued, as long as we follow the algorithm (subtract smallest from largest), we can always proceed. But need to prove that the process will not get stuck with some numbers not equal to g but no operation possible? Not possible as argued.
Thus answer.
Now we can implement.
Complexity O(n log max).
Now, I'll produce final answer.
But let me double-check with some random tests mentally:
Test: n=4: [6,9,15,21] gcd = 3, answer = 12. Let's try to reduce: (6,9,15,21) -> subtract 6 from 21? 21-6=15 -> (6,9,15,15). subtract 6 from 15? 15-6=9 -> (6,9,9,15). subtract 6 from 15 -> (6,9,9,9). subtract 6 from 9 -> (6,3,9,9). subtract 3 from 6 -> (3,3,9,9). subtract 3 from 9 -> (3,3,6,9). subtract 3 from 6 -> (3,3,3,9). subtract 3 from 9 -> (3,3,3,6). subtract 3 from 6 -> (3,3,3,3). sum=12. Works.
Test: n=3: [100, 99, 98] gcd? gcd(100,99)=1, gcd(1,98)=1, answer=3. Can we get sum 3? We need to produce a 1. Let's try: 100-99=1 -> (1,99,98). Now reduce others: 99-1=98 -> (1,98,98). then 98-1=97 -> (1,97,98). This may take many steps but eventually we can get all 1. Actually with 1 present, we can subtract 1 repeatedly from any number >1 to reduce it to 1. So yes.
Test: n=2: [100, 1] gcd=1, answer=2. Already (100,1): subtract 1 from 100 repeatedly? 100>1 -> 99; continue... eventually (1,1). So yes.
Thus answer.
Now, implement.
But hold on, I recall a Codeforces problem: "Ciel and Flowers" maybe? But this is different. The problem described appears to be from Codeforces Round #... Actually I think it's Codeforces 323A or something? Anyway.
Now, write solution.
Pseudo-code:
read n
read array a[0..n-1]
g = a[0]
for i=1..n-1:
g = gcd(g, a[i])
ans = n * g
print ans
That's it.
Edge case: n=0? but n>=2.
Thus final.
But wait: There's a nuance: The operation reduces sum by xj. Could we achieve sum less than n*g by making some numbers less than g? Since g is the gcd, any number is a multiple of g. If a number becomes less than g, it would be positive but not a multiple of g? Actually if all numbers are multiples of g, the smallest positive multiple is g, so numbers cannot be less than g unless they become zero, but they cannot become zero because xi > xj ensures difference positive. So numbers always >= 1. But could they become a positive integer less than g that is also a multiple of g? The only positive multiple of g less than g is none, except g itself. So they cannot be < g. So indeed min is g.
Thus answer stands.
One more check: Suppose numbers: [2,3]. gcd=1. Answer=2. Can we achieve sum 2? Sequence: (2,3) -> subtract 2 from 3 => (2,1) sum=3. Then subtract 1 from 2 => (1,1) sum=2. Yes.
Now finalize.
But wait, there is a known subtlety: The operation requires xi > xj. When we have a number equal to g and another number also equal to g, we cannot subtract because they are equal. So we cannot overshoot to less than g. So fine.
Thus solution is trivial.
However, we must confirm that we can always eventually reach all numbers equal to g. The algorithm we described (repeatedly subtract smallest from largest) will converge, but we should ensure that it never gets stuck in a cycle? Since sum strictly decreases, no cycles. So it terminates. And at termination all numbers are equal. Because if not all equal, there exists max > min, so operation possible. So at termination, they are all equal. And invariant gcd = g, so the equal number must be g (since all numbers equal to some d, then gcd = d, but initial gcd = g, so d must divide g? Actually gcd is invariant, so the gcd of the final set is d, but the gcd of the final set must equal the initial gcd g, so d = g. However careful: gcd is invariant under the operation: We argued earlier that gcd(a,b) = gcd(a-b,b). So indeed the gcd of the whole set remains unchanged throughout. So if we end with all numbers equal to d, the gcd of the set is d. Since the initial gcd is g, we have d = g. So final numbers are g. So indeed.
Thus answer is n * g.
Double-check with sample 4: 45 12 27 30 18 -> gcd=3, n=5 -> 15, matches.
Thus solution.
Now implement.
We'll just compute gcd and multiply.
Potential pitfall: gcd function for zero? but numbers are >=1. So fine.
Thus answer.
Now produce final code in Python.
I'll include reading input, computing gcd using math.gcd or custom.
Write:
import sys
def gcd(a,b):
while b:
a,b = b, a%b
return a
def main():
data = sys.stdin.read().strip().split()
if not data: return
n = int(data[0])
arr = list(map(int, data[1:1+n]))
g = arr[0]
for x in arr[1:]:
g = gcd(g, x)
print(n * g)
if name == 'main':
main()
That's it.
But wait: I recall a problem "Ciel and Numbers" from Codeforces? Actually I think it's "Ciel and Flowers"? No.
Anyway.
Now provide answer.
Thus final output.
```python
import sys
def gcd(a: int, b: int) -> int:
"""Return the greatest common divisor of a and b."""
while b:
a, b = b, a % b
return a
def main() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
numbers = list(map(int, data[1:1 + n]))
g = numbers[0]
for x in numbers[1:]:
g = gcd(g, x)
print(n * g)
if name == "main":
main()