We need to solve problem: given c_i (max number of radiators) and sum_i (minimum total sections needed), find minimal sum of squares of numbers (positive? are radiators allowed to have zero sections? Possibly we can have fewer than c_i radiators, so we can assign numbers k1,...,km where m ≤ c_i, each ki ≥ 1? The problem: "install at most c_i radiators" – usually radiators could have any positive number of sections? Could a radiator have zero sections? Typically a radiator with zero sections is nonsense; but we could not install it. So we consider we can use up to c_i radiators, each having a positive integer number of sections (at least 1). However, the note for the third room: they list variants: [6,0], [5,1], ... [0,6]. That includes zeros, meaning they consider that they can use fewer than c_i=2 radiators, but they list [6,0] meaning they install one radiator with 6 sections and the second radiator with 0 sections? Or they list all possibilities of distributing sum=6 sections across at most 2 radiators, with radiators possibly having 0? They treat zeros as not installing a radiator, but still count as a radiator? The note says "there 7 variants to install radiators: [6, 0], [5, 1], [4, 2], [3, 3], [2, 4], [1, 5], [0, 6]." They consider two radiators, but each can have 0 sections (meaning not installed). So effectively, we can have any number of radiators up to c_i, each with positive integer sections; zeros are allowed to indicate not using that radiator. So we can think we can choose m (1 ≤ m ≤ c_i) radiators each with positive integer sections that sum to at least sum_i (but obviously we want exactly sum_i? Because extra sections increase cost? But cost is sum of squares; if we have extra sections beyond required, that increases cost unnecessarily, so optimal solution will have total sections exactly sum_i. So we need to partition sum_i into up to c_i positive integers (each at least 1) to minimize sum of squares.
Thus problem reduces to: given total S = sum_i, and maximum number of parts K = c_i, find nonnegative integers x1,...,xK (or variable number m ≤ K) such that each xi ≥ 0, sum xi = S, and minimize sum xi^2. But zeros are allowed; zeros essentially reduce number of radiators used. Since xi=0 contributes 0 to cost and also doesn't count as radiator? But they are included in the list of K? The note includes zeros; they have two "radiators" but one with zero sections. However, if we treat zeros as radiators with zero sections, cost is zero and they are still counted? The problem states "install at most c_i radiators". If we treat zeros as not installing, then we can have fewer than c_i radiators. So we can assume we choose m ≤ K positive integers that sum to S, and we can always pad with zeros to make K numbers, which doesn't affect cost. So the minimization is over number of positive parts p (1 ≤ p ≤ K) and distribution of S into p positive integers.
We need to minimize sum of squares given fixed sum S, with constraint that number of summands p is at most K.
We recall that for a fixed sum S, the sum of squares is minimized when the numbers are as equal as possible (by convexity). More precisely, given p parts (each ≥ 1), the minimal sum of squares is achieved when the numbers are either floor(S/p) or ceil(S/p), with the number of larger ones = S mod p, and smaller ones = p - (S mod p). Since each part must be at least 1, p ≤ S, because if p > S, some would be zero, but zeros are allowed? Actually if p > S, we could have zeros? But note: we need exactly S sections; if we have more radiators than S, we can assign some radiators 0 sections (i.e., not installed), effectively using only some radiators. So the effective number of radiators used is at most min(K, S) because each radiator must have at least 1 section if installed. But if K > S, we can just use S radiators with 1 section each, achieving cost S. But is that always optimal? Possibly not, because squares: 1^2 = 1 each, sum = S. But if we use fewer radiators, we might get larger numbers and larger squares; e.g., S=6, K=4: we could use 6 radiators? Actually K=4, so maximum radiators is 4, but S=6 >4, so we can't use 6 radiators because K=4. So p must be ≤ K and also ≤ S (since each positive part at least 1). So p ∈ [1, min(K, S)].
Thus we need to compute min over p = 1..min(K, S) of f(p) = minimal sum of squares when S is split into p positive integers.
We know that f(p) = (S mod p) * (ceil(S/p))^2 + (p - (S mod p)) * (floor(S/p))^2, where ceil(S/p) = floor((S+p-1)/p) = q+1 if remainder >0. Actually if S = pq + r, with 0 ≤ r < p, then q = floor(S/p), and r = S mod p. Then the minimal sum of squares is r(q+1)^2 + (p-r)q^2. (Note: if r=0 then all q, sum = pq^2).
We need to find the p that minimizes this expression.
But we have to answer many queries: n up to 1000, each with c_i, sum_i up to 10^4. So direct loop over p up to min(c_i, sum_i) per query (max 10^4) is fine: 1000 * 10^4 = 1e7, which is okay in C++ but we are to produce answer possibly in Python? The problem might be from Codeforces (likely problem "Heating" or something). Python with 1e7 operations may be borderline but still okay if optimized? But we can also compute directly formula. Let's see: f(p) = p * q^2 + 2qr + r, where q = floor(S/p), r = S mod p? Let's derive:
If S = pq + r, 0 ≤ r < p. Then numbers: r of them are q+1, and p-r are q. So sum squares = r(q+1)^2 + (p-r)q^2 = r(q^2+2q+1) + (p-r)q^2 = pq^2 + 2q*r + r.
So f(p) = p * floor(S/p)^2 + 2floor(S/p)(S mod p) + (S mod p). Good.
We need min_{p=1..min(K,S)} f(p).
We could try to find optimal p analytically. Because the function f(p) with integer p has some pattern: splitting into more parts reduces squares? Actually sum of squares for fixed sum S is convex in the vector and is minimized when numbers are equal, but as p increases, the numbers become smaller, thus sum of squares decreases? Let's check: S=6: p=1 -> 36; p=2 -> optimal [3,3] = 18; p=3 -> [2,2,2] = 12; p=4 -> possible? p=4 ≤ min(?,S) -> S=6, p=4 allowed? Each part at least 1, so we need to distribute 6 across 4: numbers: three 2's? Actually 6 = 2+2+1+1: squares = 4+4+1+1=10; [2,2,2,0]? But zero not allowed if we require exactly p positive parts. But we can have p=4 parts, some might be 0? But we said each radiator must be positive if installed; but if we allow zeros, we effectively use fewer radiators. So if we consider p as actual number of radiators used, it must be ≤ K and each used radiator has ≥ 1. So for p=4, we need to assign at least 1 to each, so min sum is 4, S=6 is possible. So we consider p=4: distribution: 2,2,1,1 gives sum squares=10. For p=5: each at least 1, need sum 6: distribution: 2,1,1,1,1: squares=4+1+1+1+1=8. p=6: all ones: 6. p=7 would exceed S? p=7 >6, cannot have all positive because min sum would be 7 >6, so p max = S =6. So min cost among p=1..6 is: 36,18,12,10,8,6 => minimum is 6 at p=6. So indeed increasing p reduces sum of squares, because 1^2=1 is small. So the minimum cost would be achieved by using as many radiators as possible (i.e., p = min(K, S))? But wait, is that always true? Let's test with S=4, K=3: p=1:16; p=2: (2,2)=8; p=3: need distribution 2,1,1 => squares=4+1+1=6. So p=3 gives 6 which is lower than p=2=8. p=4? S=4, K=3 => min(K,S)=3 => cannot do p=4. So min at max p.
But consider S=5, K=3: p=1:25; p=2: (3,2)=9+4=13; p=3: distribution (2,2,1)=4+4+1=9. So p=3 gives 9 <13. So still max p.
Consider S=10, K=3: p=1:100; p=2: (5,5)=25+25=50; p=3: distribution (4,3,3) or (4,4,2) etc. Let's compute: 10 = 4+3+3 => squares=16+9+9=34; 4+4+2 =>16+16+4=36; 5+3+2 =>25+9+4=38; optimal is 4,3,3 = 34. So p=3 gives 34, lower than p=2=50. So max p seems better.
But is there any case where using more radiators increases cost? Because sum of squares is convex: for a fixed sum, distributing into more parts reduces the sum of squares, because the variance reduces? Actually, given sum S, the sum of squares is minimized when numbers are as equal as possible, but the minimal value for a given p is non-increasing as p increases? Let's check: Compare f(p) and f(p+1) for given S. Intuitively, if you have more parts, you can make them more equal, but also each part at least 1; if you add a part, you have to split the total more, which tends to reduce the squares. However, is it possible that adding a part might force some parts to become 1 and others larger, increasing sum? Let's test with S=4: p=2 -> f(2)=8, p=3 -> f(3)=6, p=4-> f(4)=4. So decreasing.
S=5: p=2 ->13, p=3->9, p=4-> (2,1,1,1) = 4+1+1+1=7? Actually p=4: need 5 across 4, so distribution: 2,1,1,1 => squares=4+1+1+1=7; p=5 -> all 1:5. So f(2)=13, f(3)=9, f(4)=7, f(5)=5. So decreasing.
S=6: we saw values: 36,18,12,10,8,6. So decreasing.
S=7: p=1:49; p=2: (4,3)=16+9=25; p=3: (3,2,2)=9+4+4=17; p=4: (2,2,2,1)=4+4+4+1=13; p=5: (2,1,1,1,2?) Actually need 7 across 5: distribution: 2,2,1,1,1 = 4+4+1+1+1=11; p=6: (2,1,1,1,1,1)=4+1+1+1+1+1=9; p=7: all ones=7. So decreasing.
S=8: p=1:64; p=2: (4,4)=16+16=32; p=3: (3,3,2)=9+9+4=22; p=4: (2,2,2,2)=44=16; p=5: (2,2,2,1,1)=4+4+4+1+1=14; p=6: (2,2,1,1,1,1)=4+4+1+1+1+1=12; p=7: (2,1,1,1,1,1,1)=4+16=10? Actually 2+6*1=8, squares=4+6=10; p=8: all ones=8. So decreasing.
So it seems f(p) is strictly decreasing with p (or at least non-increasing). Let's attempt to prove: For a given S, consider optimal distribution for p parts: numbers are q = floor(S/p) and q+1. For p+1 parts, we can consider taking the optimal for p and splitting one part into two. But we need to show f(p+1) ≤ f(p). Because we can always take an optimal p-partition and split one of its numbers (say a) into two numbers 1 and a-1, which yields p+1 numbers summing to S, but the sum of squares changes from a^2 to 1^2 + (a-1)^2 = a^2 - 2a +2. This is less than a^2 if a>1, since 1^2+(a-1)^2 = a^2 -2a +2. Compare to a^2: difference = -2a+2, which is <0 when a≥2. So if the p-partition has any part ≥2, then by splitting it into 1 and a-1, we get a new partition with p+1 positive parts, whose cost is f(p) - (2a-2). This is less than f(p) provided a≥2. But is this new partition necessarily the optimal for p+1 parts? Not exactly, but it gives an upper bound: f(p+1) ≤ f(p) - (2a-2) < f(p) provided there exists a part ≥2 in the optimal p-partition. However, what if the optimal p-partition is all ones? That would mean S = p, because each part at least 1, sum of ones = p. If S = p, then f(p) = p*1^2 = p. For p+1, if we try to use p+1 parts, sum must be S = p, but min sum with p+1 positive parts is p+1, so not possible. So p cannot exceed S. So for p < S, there is at least one part ≥2? Actually if p < S, sum of p positive integers is S, by pigeonhole there must be at least one part >= ceil(S/p) which is at least 2 if S > p. So yes, if p < S, there is at least one part >=2. Therefore for any p < S, we can construct a (p+1)-partition with lower cost, implying f(p+1) ≤ f(p) - something < f(p). Thus f(p) is strictly decreasing for p = 1,2,...,S-1. At p=S, f(S)=S (all ones). So indeed, the minimal cost for given S and max radiators K is achieved by using as many radiators as possible, i.e., p = min(K, S). Because f(p) is decreasing with p.
But careful: The above argument uses that we can always split a part to get a valid (p+1)-partition, but it requires that the part being split is at least 2, which holds for p < S. So for p+1 ≤ S, we can always do that. Therefore f(p+1) ≤ f(p) - positive, so f(p) strictly decreasing until p = S. Therefore the minimal cost given maximum number of radiators K is achieved at p = min(K, S). However, we must consider the condition that p ≤ K; if K ≥ S, then we can use p=S, cost = S. If K < S, then p=K, cost = f(K) = K * floor(S/K)^2 + 2floor(S/K)(S mod K) + (S mod K). So answer is simply:
If c_i >= sum_i: answer = sum_i.
Else: answer = c_i * floor(sum_i / c_i)^2 + 2floor(sum_i / c_i)(sum_i % c_i) + (sum_i % c_i).
But wait, check sample:
First: c=1, sum=10000 => c < sum, so answer = 1 * (10000/1)^2 + 2100000 + 0 = 10000^2 = 10^8, matches.
Second: c=10000, sum=1 => c >= sum, answer = sum =1, matches.
Third: c=2, sum=6 => c < sum? 2<6 => answer = 2 * floor(6/2)^2 + 23(0)+0? Wait: floor(6/2)=3, S mod 2 = 0 => answer = 29 = 18, matches.
Fourth: c=4, sum=6 => c > sum? Actually c=4, sum=6 => c=4 <6? 4 < 6, so c < sum, so answer = 4 * floor(6/4)^2 + 21*(2) + 2? floor(6/4)=1, mod=2 => answer = 41 + 21*2 + 2 = 4+4+2=10, matches sample output 10. So that works.
Thus the problem simplifies drastically: the answer is sum_i if c_i >= sum_i, else compute as above.
But we must double-check if any nuance: can we have radiators with zero sections? The above analysis used p = number of radiators actually used, which we set to min(c_i, sum_i) because using more radiators reduces cost, but we must ensure each radiator has at least 1 section. For c_i >= sum_i, we can use exactly sum_i radiators each with 1 section, costing sum_i. Could there be a cheaper arrangement with fewer radiators? No, because using fewer radiators would mean some radiators have more than 1 section, increasing squares sum beyond sum_i? Let's test sum=3, c=5. Using p=3 gives cost 3. Using p=2 gives optimal [2,1] cost=5. So indeed p=3 (i.e., sum_i radiators) is better. Using p=4? But p can't be 4 because we need at least 4 positive parts sum to 3 is impossible; you could have one zero? But if we allow zeros, we could have p=4 with numbers 1,1,1,0, which sum to 3, cost = 3. That's same as p=3. But if we count zeros as not installed, effectively p is number of non-zero radiators, which we can choose up to c_i. So if c_i >= sum_i, we can achieve cost = sum_i by using sum_i radiators each with 1 section, and the rest not installed. That's valid because we install at most c_i radiators. So answer sum_i is correct.
Thus problem reduces to simple calculation.
But hold on: The constraints: c_i, sum_i up to 10^4. So answer may be up to (10^4)^2 = 10^8 for the case c=1, sum=10000. But also maybe larger? if c=1, sum=10000 -> 10^8, which fits in 32-bit int? 10^8 fits in 32-bit signed (2.147e9). But what if sum_i is up to 10^4 and c_i=1, then answer is (10^4)^2 = 10^8. If sum_i up to 10^4, the maximal answer is when c_i=1, sum_i=10000 => 10^8. That's fine. But wait, the first sample includes 100000000 = 10^8 indeed.
Thus solution is trivial.
But is there any trap? Maybe we need to consider that radiators must have integer number of sections, but we already considered.
Now, confirm reasoning: The sum of squares is minimized when the numbers are as equal as possible, and for a fixed number of parts p, the minimal is as given. And we argued that f(p) is decreasing in p. Let's prove formally: For p from 1 to S-1, f(p+1) < f(p). Consider the optimal partition for p parts. Since p < S, there exists at least one part >= 2. Take one such part a (>=2). Replace it with two parts: 1 and a-1. This yields a partition of S into p+1 positive parts. The new sum of squares = f(p) - a^2 + 1^2 + (a-1)^2 = f(p) - 2a + 2. Since a >=2, -2a+2 <= -2, so f(p+1) ≤ f(p) - 2 < f(p). Since the optimal partition for p+1 cannot have cost larger than this constructed partition, we have f(p+1) ≤ f(p) - 2 < f(p). So indeed decreasing. Therefore, for K ≤ S, the minimum is achieved at p = K. For K ≥ S, we can achieve p = S, which yields cost S. And p cannot exceed S because each part must be at least 1.
Hence solution.
Now for implementation: read n, loop, read c, s. If c >= s: print(s). Else: q = s // c, r = s % c; ans = c * q * q + 2 * q * r + r. Or more clearly: ans = (c - r) * q * q + r * (q+1) * (q+1). But careful: (c - r) * q^2 + r * (q+1)^2. Compute and print.
Check with sample: 4,6: q=1, r=2 => (4-2)1 + 24 = 2 + 8 = 10. Good.
Thus solution.
But the problem statement: "Each radiator can have several sections, but the cost of the radiator with k sections is equal to k^2 burles." So yes.
Now, we need to output answer per room.
Complexity O(n).
Potential pitfalls: integer overflow? In C++ with int, 10000*10000 = 100 million, within 2e9. But if we use 64-bit for safety, that's fine.
Now, we need to provide final answer.
But wait, I'm ChatGPT. The user expects a solution with explanation? The user posted "Several days ago you bought a new house...". It's a problem statement and they likely want a solution code. However, the instruction says: "We need to solve problem: ...". In the conversation, it's me (the assistant) analyzing. The final output should be something. Usually, in these tasks, the user expects a solution description and code. So I'll produce a final answer: explanation and code in Python or C++. I'll include reasoning.
But is there any nuance about zeros? Let's double-check: Could it be optimal to use fewer radiators than min(K,S) if K < S? Because f(p) is decreasing, so p=K gives minimum among p≤K. So yes.
Now, also consider that K may be 0? No, constraints say c_i ≥ 1.
Thus answer is as above.
Let's also test some random cases to be sure: c=3, s=10: q=3, r=1? Actually 10//3=3, r=1. ans = (3-1)9 + 116 = 2*9+16=18+16=34. That matches earlier optimal (4,3,3) gave 34. Good.
c=5, s=10: since c > s? Actually c=5 < s? 5<10, so q=2, r=0? 10//5=2, r=0 -> ans = 5*4=20. But is that minimal? Could we use 5 radiators? Yes, 2,2,2,2,2 -> cost=20. Using 4 radiators would be 3,3,2,2? Actually optimal for 4 parts: 3,3,2,2 = 9+9+4+4=26. So 20 is lower, consistent with decreasing property: p=5 gives lower than p=4. But can we use 6 radiators? max is 5, so p=5 is max. So answer 20.
c=10, s=10: c>=s -> ans =10. Using 10 radiators each with 1 gives 10.
c=10, s=12: q=1, r=2? 12//10=1, r=2. ans = (10-2)1 + 24 = 8+8=16. Check: using 10 radiators: two radiators with 2 sections, eight with 1: squares: 42? Actually two radiators with 2 -> 4 each, total 8; eight with 1 ->8; sum=16. Could we do better with fewer radiators? If we used 9 radiators: sum=12 across 9: at least eight 1's and one 4? Actually 9 positive numbers sum to 12, average ~1.33, so numbers are mostly 1 and some 2s and maybe 3. Optimal: we want as equal as possible: floor(12/9)=1, r=3, so distribution: three 2's, six 1's => cost: 34 + 6*1 = 12+6=18. So indeed 16 is lower.
c=2, s=3: q=1, r=1 -> ans = (2-1)1 + 14 = 1+4=5. That's [2,1] cost=5. Using one radiator would be 9, so indeed 5 is minimal. Good.
Thus formula works.
Now about zeros: In our approach, when c < s, we used exactly c radiators. Could it ever be beneficial to use fewer than c radiators? Since f(p) decreases with p, using more radiators always reduces cost (as long as p ≤ s). So with p ≤ min(c,s), the best is p = min(c,s). So for c < s, that is p = c; for c ≥ s, p = s (but we could also use more than s? Cannot because each radiator must have at least 1 section, so you can't have more than s radiators with positive sections. But you could have exactly c radiators with some zeros, effectively using only s of them? If c > s, you could use exactly s radiators (each with 1) and the rest zero. That's allowed because you install at most c radiators; you could install fewer than c. That yields cost = s. Using c radiators would require c positive numbers summing to s, but that's impossible because min sum would be c (since each ≥1), which > s if c > s. So you cannot install c radiators if c > s because you would need at least c sections. However, note: "install at most c_i radiators" means you can choose to install any number up to c_i. So if c > s, you are allowed to install s radiators, but you cannot install more than s because each radiator needs at least one section to meet total sum, but you could install c radiators and assign some zero sections? But the problem probably means that each installed radiator must have at least one section (since having a radiator with zero sections is meaningless). The note in third room includes [0,6] variant, which implies they consider installing a radiator with 0 sections as an option (maybe as not installing it). But they also list [6,0] meaning one radiator with 6 sections and another not installed. So effectively, you can have up to c_i radiators, but if you choose to have fewer, you simply leave some out. In the case c > s, you can install exactly s radiators, each with 1 section. You could also install more than s radiators, but then some would have 0 sections (i.e., not installed). But does that count as installed? If you consider a radiator with 0 sections as not installed, then the effective number of installed radiators is the count of those with positive sections. So the maximum number of installed radiators with positive sections is at most s, because total sections sum is s. So you cannot exceed s in terms of radiators that actually contribute. So the constraint of at most c_i radiators only limits the number of radiators you can "install", but if you install one with 0 sections, is that considered installing? Usually, no. So it's safe to think that we can have up to c_i radiators, but each installed radiator must have at least 1 section. The note includes zeros, but those zeros are likely representing not installing that radiator, but they are counted as positions? They list all variants of distributing sections across two radiators; they include [6,0] meaning one radiator gets 6 sections and the other gets 0 sections, i.e., only one radiator is installed. So it's consistent: you can decide to not use some of the allowed radiators.
Thus for c > s, you can use exactly s radiators, each with 1. That yields cost s. Could there be a configuration using more than s radiators with some zeros that yields lower cost? Suppose c=5, s=3. Using s=3 radiators each with 1 gives cost 3. Could we use 4 radiators? Then we need 4 positive integers summing to 3, impossible. So we must have at most s radiators with positive sections. So the best is s.
Therefore answer is as we derived.
Thus solution is trivial.
Now, we should produce final answer: a code snippet, perhaps in Python or C++.
I'll write:
n = int(input())
for _ in range(n):
c, s = map(int, input().split())
if c >= s:
print(s)
else:
q = s // c
r = s % c
# cost = (c - r) * q**2 + r * (q+1)**2
ans = (c - r) * q * q + r * (q+1) * (q+1)
print(ans)
This works.
But we need to consider integer sizes: q up to 10000, (q+1)^2 up to 10001^2 ~ 100 million, times c up to 10000, maximum product: (c - r) * q^2, with c up to 10000 and q up to 10000? But note: when c < s, q = s // c ≥ 1. The worst-case for value: c = 1, s = 10000, then ans = 1 * 10000^2 = 10^8, safe. If c = 5000, s = 10000, q = 2, r = 0, ans = 5000 * 4 = 20000, small. If c=5000, s=10000, actually c < s so q=2, ans=5000*4=20000. The largest ans seems to be when c is minimal relative to s, i.e., c=1, s=10000 -> 10^8. That's within 32-bit signed, but safe in Python anyway.
Thus code is fine.
But the original problem might require 64-bit? Some languages need long long. So for C++ we use long long.
Now, we should provide explanation. In the final answer, we can include reasoning and code.
Now, let's consider edge cases: c=0? Not allowed. s=0? Not allowed (1 ≤ sum_i). So fine.
Thus answer.
But wait: Are there any test cases where the greedy equal distribution isn't optimal? For a fixed number of parts, yes, equal distribution minimizes sum of squares. That's a standard result by convexity: For fixed sum, sum of squares is minimized when numbers are as equal as possible. So fine.
Now, we can finalize.
But maybe the problem originally from Codeforces: "1180A - Heating"? Actually there is Codeforces Round #1180 (Div. 2) problem A: "Heating" with exactly this statement. I recall that the solution is indeed as we derived: if c >= sum, answer = sum; else answer = (c - r)(q^2) + r(q+1)^2. So that's correct.
Thus final answer.
Now, the user likely expects an explanation and code. I'll provide that.
Given that the instruction says "We need to solve problem: ..." it's ambiguous. But the assistant's final response should contain the solution.
I'll write:
Solution Explanation:
Given we need to distribute total sections S across at most C radiators, each radiator must have integer number of sections (≥1 if installed). The cost of a radiator with k sections is k^2, and we want to minimize total cost while total sections ≥ S. It is never beneficial to exceed S, so we aim to have exactly S sections.
For a fixed number p of radiators actually used (1 ≤ p ≤ C, and also p ≤ S because each must have at least 1), the minimal cost is achieved when the sections are as equal as possible: let q = ⌊S/p⌋, r = S mod p. Then r radiators get q+1 sections and p−r get q sections, giving cost:
[
\text{cost}(p) = (p - r) q^2 + r (q+1)^2 = p q^2 + 2 q r + r.
]
It can be proved that cost(p) strictly decreases as p increases (as long as p < S). Therefore, to minimize cost we should use as many radiators as possible, i.e., p = min(C, S).
- If C ≥ S, we can use exactly S radiators each with 1 section, costing S.
- Otherwise (C < S), we set p = C and compute the cost using the formula.
Implementation is straightforward.
Complexity: O(1) per test case, overall O(n).
I'll provide code in Python and C++.
Now, provide final output. The minimum cost for each room can be obtained by using as many radiators as possible (up to the limit c), because splitting the sections more evenly always reduces the sum of squares.
If c ≥ sum, we can install exactly sum radiators each with one section, total cost = sum.
Otherwise, let q = sum // c and r = sum % c. The optimal distribution is to have r radiators with q+1 sections and c - r radiators with q sections. The total cost is
[
(c - r) \cdot q^2 + r \cdot (q+1)^2.
]
The solution runs in O(1) per room, overall O(n).
Code (Python)
n = int(input())
for _ in range(n):
c, s = map(int, input().split())
if c >= s:
print(s)
else:
q = s // c
r = s % c
ans = (c - r) * q * q + r * (q + 1) * (q + 1)
print(ans)
Code (C++)
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
while (n--) {
long long c, s;
cin >> c >> s;
if (c >= s) {
cout << s << '\n';
} else {
long long q = s / c;
long long r = s % c;
long long ans = (c - r) * q * q + r * (q + 1) * (q + 1);
cout << ans << '\n';
}
}
return 0;
}