Explanation:
Okay, so I have this function f(x) defined piecewise. It's ax² when x is greater than or equal to a, and ax + 2a when x is less than a. The question is asking for the largest value of a such that the graph of y = f(x) intersects every horizontal line at least once. Hmm, interesting. Let me try to figure this out step by step.

First, I need to understand what the question is really asking. It says the graph intersects every horizontal line at least once. That means, for any horizontal line y = k, there must be some x such that f(x) = k. So essentially, the function must be surjective onto its range? Or maybe not exactly surjective, but for every horizontal line, which would correspond to every real number if the range is all real numbers. Wait, maybe not. Wait, the problem says "every horizontal line," so maybe the function's range has to be all real numbers? Wait, but if the graph intersects every horizontal line, that would mean that for every real number k, there exists at least one x with f(x) = k. So the function must be surjective onto ℝ. But maybe not? Wait, no. Because horizontal lines can be y = k for any real number k. So if the function's range is all real numbers, then yes, every horizontal line would intersect the graph. However, if the range is not all real numbers, then there are some horizontal lines that don't intersect the graph. Therefore, the question is essentially asking for the largest value of a such that the range of f(x) is all of ℝ. Wait, but maybe not exactly. Wait, maybe not all horizontal lines? Wait, the problem says "intersects every horizontal line at least once," so for every horizontal line, meaning for every real number k, there is an x such that f(x) = k. Therefore, the range of f(x) must be all real numbers. Therefore, we need to find the largest a such that f(x) is surjective onto ℝ.

Alternatively, maybe there's a different interpretation? For example, maybe some horizontal lines might intersect the graph multiple times, but the question is just saying that each horizontal line must intersect at least once. So regardless, the function has to cover all real numbers. Therefore, the range must be ℝ. So we need to find the largest a such that the range of f(x) is ℝ.

Alternatively, perhaps even if the range isn't all real numbers, but for some reason every horizontal line still intersects the graph? Wait, that can't be. If the range is not all real numbers, then there must be some k not in the range, so the horizontal line y = k would not intersect the graph. Therefore, the problem reduces to finding the largest a such that f(x) is surjective onto ℝ. Therefore, we need to find the largest a such that the range of f(x) is all real numbers. So let's analyze the function f(x) and find for which a the range is ℝ.

First, let's analyze the function. It's piecewise defined. For x >= a, it's ax². For x < a, it's ax + 2a. So the function has two parts: a quadratic part for x >= a and a linear part for x < a. Let me try to understand the behavior of each part.

First, let's consider the linear part: ax + 2a for x < a. Let's rewrite that as a(x + 2). So it's a linear function with slope a. Depending on the value of a, this linear part can be increasing or decreasing. Similarly, the quadratic part: ax² for x >= a. The quadratic function ax² is a parabola opening upwards if a > 0, downwards if a < 0, and if a = 0, it's a constant function 0. But since the function is defined piecewise, the value of a could be positive or negative. However, the problem says "a is some number," so real number. But we need to find the largest value of a. So perhaps a is positive? Wait, maybe not necessarily. Let me think.

First, let me consider the case when a is positive. Then, for x >= a, the quadratic part is ax², which opens upwards, so it goes to infinity as x increases, and at x = a, it takes the value a*(a)^2 = a³. On the other hand, the linear part for x < a is ax + 2a, which is a line with slope a. Since a is positive, this line is increasing. So as x approaches a from the left, the linear part approaches a*a + 2a = a² + 2a. Therefore, the left side of the function, the linear part, approaches a² + 2a as x approaches a from the left, while the quadratic part at x = a is a³. Therefore, for the function to be continuous at x = a, we need the left and right limits to agree. But the problem didn't specify continuity. Wait, the problem didn't say anything about continuity. So maybe we need to check whether the function is continuous? Or maybe not. Because if the function isn't continuous, then there might be a jump discontinuity at x = a, but the problem is about intersecting every horizontal line. However, even if there is a jump discontinuity, the function could still intersect every horizontal line. However, perhaps the problem assumes continuity? Wait, but the problem didn't say that. So maybe we need to check if there is continuity or not. Wait, but maybe the function is not necessarily continuous. Let me check.

Wait, let me check the original problem again. The function is defined as:

f(x) = ax² if x ≥ a,

f(x) = ax + 2a if x < a.

So there's no mention of continuity. Therefore, the function may have a jump discontinuity at x = a. Let me check the left and right limits at x = a.

Left-hand limit as x approaches a from the left: ax + 2a evaluated at x = a is a*a + 2a = a² + 2a.

Right-hand limit as x approaches a from the right: ax² evaluated at x = a is a*(a)^2 = a³.

Therefore, unless a² + 2a = a³, the function is discontinuous at x = a. Therefore, for continuity, we have to have a³ = a² + 2a. Let's solve that equation:

a³ - a² - 2a = 0,

Factor out an a:

a(a² - a - 2) = 0.

So either a = 0, or a² - a - 2 = 0.

Solving a² - a - 2 = 0:

Discriminant D = 1 + 8 = 9,

Solutions are [1 ± 3]/2, so 4/2 = 2 or -2/2 = -1.

Therefore, the function is continuous at x = a if and only if a = 0, a = 2, or a = -1. But the problem didn't specify continuity, so maybe we don't have to assume that. However, the problem is about intersecting every horizontal line. So if the function is discontinuous at x = a, there might be a gap in the range. Therefore, maybe continuity is important here? Wait, but even if there is a jump discontinuity, maybe the range can still cover all real numbers? Or maybe not. Let me think.

But since the problem is asking for the largest value of a, maybe the answer is a positive number. Let me check for different cases of a.

But first, maybe it's better to first assume continuity, because maybe the problem expects that. Or maybe not. Let me try to think without assuming continuity.

First, let's consider the range of the function. Let's split the function into two parts: for x < a, which is ax + 2a, and for x ≥ a, which is ax².

So for the linear part, x < a. The linear function ax + 2a. Depending on the value of a, this can be increasing or decreasing. Similarly, the quadratic part is ax² for x ≥ a. Let's analyze both parts.

First, the linear part: ax + 2a for x < a.

Case 1: a > 0. Then the linear function is increasing. As x approaches negative infinity, the value of ax + 2a approaches negative infinity if a > 0. Wait, no: If a > 0, then as x approaches negative infinity, ax + 2a approaches negative infinity. However, since x is restricted to x < a, which for a > 0, includes all real numbers less than a. Therefore, the linear part, when a > 0, can take values from negative infinity up to the value at x approaching a from the left, which is a*a + 2a = a² + 2a. So the range of the linear part is (-∞, a² + 2a).

Then, for the quadratic part, ax² for x ≥ a. Since a > 0, the quadratic function ax² is a parabola opening upwards. For x ≥ a, the minimum value occurs at x = a, which is a*(a)^2 = a³. Then as x increases beyond a, the quadratic part increases to infinity. Therefore, the range of the quadratic part is [a³, ∞).

Therefore, combining both parts, the total range of f(x) is (-∞, a² + 2a) ∪ [a³, ∞). So for the function to intersect every horizontal line, this union must be all of ℝ. Therefore, we need that the upper bound of the linear part (a² + 2a) is greater than or equal to the lower bound of the quadratic part (a³). Because if the linear part goes up to a² + 2a and the quadratic part starts at a³, then for the two intervals to cover all of ℝ, we need that a² + 2a ≥ a³, so that there's no gap between (-∞, a² + 2a) and [a³, ∞). Otherwise, if a² + 2a < a³, then there's a gap between a² + 2a and a³, which would mean that horizontal lines between a² + 2a and a³ would not be intersected by the graph. Therefore, the condition is a² + 2a ≥ a³, which simplifies to a³ - a² - 2a ≤ 0, or a(a² - a - 2) ≤ 0.

Let me solve that inequality. Let's consider the equation a(a² - a - 2) = 0. As before, the roots are a = 0, a = 2, a = -1. Therefore, the critical points are -1, 0, 2. So to solve a(a² - a - 2) ≤ 0, we can test intervals.

First, factor the expression:

a(a - 2)(a + 1) ≤ 0.

So the critical points divide the real line into four intervals:

1. (-∞, -1)
2. (-1, 0)
3. (0, 2)
4. (2, ∞)

Let me test each interval.

1. For a < -1, let's pick a = -2:

(-2)(-2 - 2)(-2 + 1) = (-2)(-4)(-1) = (-2)(4) = -8 ≤ 0. So satisfies the inequality.

2. For -1 < a < 0, let's pick a = -0.5:

(-0.5)(-0.5 - 2)(-0.5 + 1) = (-0.5)(-2.5)(0.5) = (-0.5)(-1.25) = 0.625 > 0. So doesn't satisfy.

3. For 0 < a < 2, let's pick a = 1:

(1)(1 - 2)(1 + 1) = (1)(-1)(2) = -2 ≤ 0. So satisfies.

4. For a > 2, let's pick a = 3:

(3)(3 - 2)(3 + 1) = 3*1*4 = 12 > 0. Doesn't satisfy.

Therefore, the inequality a(a - 2)(a + 1) ≤ 0 holds when a ∈ (-∞, -1] ∪ [0, 2].

But remember, we were analyzing the case where a > 0. So in this case, for a > 0, the inequality holds when a is in [0, 2]. However, we need to consider the original problem's condition where the function intersects every horizontal line. Therefore, in the case where a > 0, the range is (-∞, a² + 2a) ∪ [a³, ∞). For this to cover all real numbers, we need that a² + 2a ≥ a³. Which, as we saw, gives a ∈ [0, 2]. But since a > 0 in this case, it's a ∈ (0, 2].

But wait, the problem says "the largest value of a". So if a can be up to 2 in this case, maybe a = 2 is the answer. But let me check.

But hold on, we need to check if when a = 2, the ranges indeed cover all real numbers. Let's check when a = 2:

Linear part: x < 2, f(x) = 2x + 4. As x approaches -infty, it approaches -infty, and at x approaching 2 from the left, it approaches 2*2 + 4 = 8. Therefore, the range of the linear part is (-infty, 8).

Quadratic part: x ≥ 2, f(x) = 2x². At x = 2, it is 2*(2)^2 = 8. For x ≥ 2, it goes from 8 upwards to infinity. Therefore, the range of the quadratic part is [8, ∞). Therefore, the total range of f(x) is (-infty, 8) ∪ [8, ∞) = ℝ. Perfect, so for a = 2, the range is all real numbers, so every horizontal line intersects the graph. Therefore, a = 2 is acceptable.

Now, if a is larger than 2, say a = 3. Then, the linear part is x < 3, f(x) = 3x + 6. As x approaches -infty, it goes to -infty, and at x approaching 3 from the left, it approaches 3*3 + 6 = 15. The quadratic part is x ≥ 3: f(x) = 3x². At x = 3, that is 3*(9) = 27. So the quadratic part starts at 27 and goes to infinity. Therefore, the range would be (-infty, 15) ∪ [27, ∞), which leaves a gap between 15 and 27. Therefore, horizontal lines between 15 and 27 would not be intersected by the graph. Therefore, a = 3 is invalid.

Therefore, for a > 2, the function doesn't cover all real numbers. For a = 2, it does. So for positive a, the maximum is 2.

But let me check the case for a < 0. Maybe there are larger values of a? Wait, the question is asking for the largest value of a. If a can be negative, but maybe there's a larger a? Wait, but if a is positive, 2 is positive, so if there are negative values of a, they are less than 2, so 2 would still be the largest. However, we need to check if for some negative a, the function could also cover all real numbers. Wait, but since the problem is asking for the largest value of a, even if negative a's are possible, but since 2 is positive, it's larger. However, I need to check if for some negative a, maybe there's a higher value? Wait, no, negative numbers are less than positive numbers. So if a can be 2, that's larger than any negative number. Therefore, the maximum is 2? But maybe not. Let me check the case when a is negative.

Case 2: a < 0.

First, analyze the linear part: ax + 2a for x < a. Since a is negative, the slope of the linear part is negative. Therefore, as x approaches -infty, ax + 2a approaches positive infinity (because a is negative, x approaches -infty, so ax approaches positive infinity). As x approaches a from the left, the value is a*a + 2a = a² + 2a. Therefore, since the linear function is decreasing (because slope a is negative), the range of the linear part is (a² + 2a, ∞). Because as x increases towards a, the value decreases to a² + 2a, so the range is from that value up to infinity.

For the quadratic part: ax² for x ≥ a. Since a is negative, and x ≥ a. Let's note that x ≥ a, but since a is negative, x could be anything greater than or equal to a. But since a is negative, and the quadratic function ax² is opening downward (because a is negative). The maximum of ax² occurs at x = 0, but x ≥ a. However, if a is negative, then x ≥ a includes x from a up to infinity. However, the quadratic ax² for x ≥ a. Let's check the behavior of this quadratic.

Since a is negative, ax² is a downward opening parabola. The vertex is at x = 0. But our domain for the quadratic part is x ≥ a (where a is negative). Therefore, for x ≥ a, the maximum occurs at x = 0 if 0 ≥ a, which it is, since a is negative. Therefore, the maximum value of the quadratic part is at x = 0: a*(0)^2 = 0. Wait, but x must be ≥ a. Since x = 0 ≥ a (because a is negative), so yes. Therefore, the quadratic function ax² on x ≥ a has maximum value 0 at x = 0, and as x approaches infinity, ax² approaches negative infinity. However, we need to check the minimum value of the quadratic part. Wait, since it's a downward opening parabola, the values go from the maximum at x = 0 (which is 0) down to negative infinity as x approaches infinity. However, on the interval x ≥ a, we need to find the range of ax². Let me check the value at x = a: a*(a)^2 = a³. As x increases from a to infinity, ax² goes from a³ down to negative infinity. Therefore, the range of the quadratic part is (-∞, 0].

Wait, hold on. Let me verify this. For a negative a, the quadratic function ax² is negative or zero. So when x ≥ a, the values of ax² depend on x. For example, if a = -1, and x ≥ -1. Then ax² = -x². At x = -1, it's -(-1)^2 = -1; as x increases from -1 to 0, -x² goes from -1 to 0; then from x = 0 to infinity, it goes from 0 to negative infinity. Therefore, the range of the quadratic part is (-∞, 0]. Therefore, if a is negative, the quadratic part's range is (-∞, 0].

Meanwhile, the linear part's range is (a² + 2a, ∞). Therefore, combining both parts, the total range of the function is (-∞, 0] ∪ (a² + 2a, ∞). For this to be all real numbers, we need that the upper part (a² + 2a, ∞) must cover the gap between the lower part's upper bound (0) and infinity. Wait, but the upper part starts at a² + 2a. For the entire range to be ℝ, we need that (-∞, 0] ∪ (a² + 2a, ∞) covers all real numbers. Therefore, we need that 0 ≤ a² + 2a. Because if 0 ≤ a² + 2a, then (a² + 2a, ∞) would start at or above 0, and since the lower part already covers up to 0, then there is no gap. However, if 0 > a² + 2a, then there's a gap between a² + 2a and 0. Therefore, the condition is that 0 ≤ a² + 2a. Let's solve that inequality:

a² + 2a ≥ 0 ⇒ a(a + 2) ≥ 0.

Which occurs when a ≤ -2 or a ≥ 0. But in this case, we are considering a < 0. Therefore, the condition becomes a ≤ -2.

Therefore, for a < 0, in order for the function to have range ℝ, we need a ≤ -2. Therefore, if a is ≤ -2, then the range is (-∞, 0] ∪ (a² + 2a, ∞). But since a ≤ -2, let's check what a² + 2a is. Let me compute a² + 2a for a ≤ -2. Let's take a = -2: a² + 2a = 4 + (-4) = 0. For a < -2, say a = -3: a² + 2a = 9 + (-6) = 3. Wait, that's positive. Wait, but for a < -2, let's see:

a² + 2a = a(a + 2). Since a ≤ -2, then a is negative, and a + 2 is negative when a < -2 (since a + 2 < 0). Therefore, a(a + 2) is positive (negative times negative). Therefore, for a ≤ -2, a² + 2a is positive or zero. Therefore, the upper part (a² + 2a, ∞) is (positive number, ∞). Therefore, combining with (-∞, 0], the total range is (-∞, 0] ∪ (something positive, ∞). Therefore, there is a gap between 0 and the something positive. Hence, the function does not cover all real numbers. Wait, that's a problem. Wait, but if a² + 2a is positive, then the upper part starts at a² + 2a, which is greater than 0, so the range would be (-∞, 0] ∪ (a² + 2a, ∞), which would leave a gap between 0 and a² + 2a. Therefore, the horizontal lines between 0 and a² + 2a are not covered. Therefore, even for a ≤ -2, the function does not cover all real numbers. Therefore, that suggests that for a < 0, there's no value of a where the function covers all real numbers?

Wait, but if a = -2, then a² + 2a = 4 + (-4) = 0. Therefore, the upper part is (0, ∞). Therefore, the total range is (-∞, 0] ∪ (0, ∞) = ℝ. Wait, so for a = -2, the range is ℝ. Wait, is that correct?

For a = -2, the linear part is x < -2, f(x) = -2x + 2*(-2) = -2x - 4. Let's check its range. Since the slope is -2 (negative), as x approaches -infty, -2x - 4 approaches positive infinity, and as x approaches -2 from the left, the value approaches -2*(-2) - 4 = 4 - 4 = 0. Therefore, the range of the linear part is (0, ∞). Wait, but earlier I thought it was (a² + 2a, ∞), which for a = -2 is 0, so (0, ∞). And the quadratic part is for x ≥ -2, f(x) = -2x². So for x ≥ -2, the quadratic function -2x². The maximum value occurs at x = 0, which is 0. As x increases beyond 0, -2x² becomes negative, and as x approaches -infty, but wait x is ≥ -2. So for x ≥ -2, the quadratic function -2x² has maximum value 0 at x = 0 and goes to -infty as x approaches ±infty? Wait, no. For x ≥ -2, when x approaches -infty? No, x is restricted to x ≥ -2. So x can go from -2 to infinity. For x between -2 and 0, the quadratic function -2x² is decreasing from -2*(-2)^2 = -8 to 0. For x ≥ 0, it's -2x², which decreases from 0 to negative infinity. Therefore, the range of the quadratic part is (-∞, 0]. Therefore, combining the linear part (0, ∞) and quadratic part (-∞, 0], the total range is ℝ. Therefore, for a = -2, the range is ℝ. However, when a < -2, for example, a = -3, then as I mentioned earlier, the linear part has range (a² + 2a, ∞). For a = -3, that's 9 + (-6) = 3, so (3, ∞). The quadratic part has range (-∞, 0]. Therefore, the total range is (-∞, 0] ∪ (3, ∞). So there is a gap between 0 and 3, hence not covering all real numbers. Therefore, only when a = -2 does the range become ℝ. Therefore, for a < 0, only a = -2 gives the entire real numbers. Therefore, if we consider the case for a < 0, the only value that works is a = -2.

Therefore, putting it together, for the function to have range ℝ, a must be in [0, 2] (for a > 0) or a = -2 (for a < 0). Therefore, the largest value of a is 2. However, let me check a = -2. Is -2 larger than 2? No, -2 < 2, so 2 is larger. Therefore, the maximum value is 2. However, let me check if for some a between 0 and 2, maybe there's a possibility that the function covers all real numbers? Wait, no. For a between 0 and 2, we saw that the condition a² + 2a ≥ a³ holds. Which ensures that the two ranges (-∞, a² + 2a) and [a³, ∞) overlap or meet, so that the total range is ℝ. For example, take a = 1. Then linear part range is (-∞, 1 + 2) = (-∞, 3). Quadratic part range is [1, ∞). Since 3 ≥ 1, the union is ℝ. So that works. Similarly, for a = 2, the linear part approaches 8, quadratic part starts at 8, so union is ℝ. Therefore, for all a between 0 and 2 inclusive, the range is ℝ. Therefore, the maximum value of a is 2. However, wait, the problem says "the largest value of a". But let me check if maybe for a = -2, which is smaller than 2, but maybe there is something else?

Wait, but no, because -2 is less than 2. So 2 is larger. Therefore, the answer is 2. Wait, but let me check for a = 0. If a = 0, the function becomes:

For x ≥ 0: f(x) = 0*x² = 0.

For x < 0: f(x) = 0*x + 2*0 = 0. Therefore, f(x) is identically 0. Therefore, the range is {0}, which is not all real numbers. Therefore, a = 0 is invalid. Therefore, in the earlier case, when a is between 0 and 2, we need to check if the ranges actually cover ℝ. Wait, when a is between 0 and 2, the linear part's upper bound is a² + 2a, and quadratic part starts at a³. Since a² + 2a ≥ a³, so the upper bound of the linear part is higher than or equal to the lower bound of the quadratic part, so there's no gap. Therefore, the union is (-∞, upper bound) ∪ [lower bound, ∞) which becomes ℝ if upper bound ≥ lower bound. Wait, no, the upper bound is a² + 2a and lower bound is a³. If upper bound ≥ lower bound, then (-∞, upper bound) ∪ [lower bound, ∞) is (-∞, upper bound) ∪ [lower bound, ∞). But since upper bound ≥ lower bound, the union is ℝ if upper bound is infinity? Wait, no. Wait, if upper bound is some finite number, say, for a between 0 and 2, say a = 1, upper bound is 3, lower bound is 1. Therefore, the union is (-∞, 3) ∪ [1, ∞) = ℝ. Because (-∞, 3) includes from -infty to 3, and [1, ∞) includes from 1 to infinity. Therefore, together they cover all real numbers. Similarly, for a = 2, upper bound is 8 and lower bound is 8, so the union is (-∞, 8) ∪ [8, ∞) = ℝ.

Therefore, yes, for all a in [0, 2], the function's range is ℝ. However, when a = 0, the function is constant 0, which only covers {0}, so a must be in (0, 2]. Therefore, the largest value of a is 2. Therefore, the answer is 2. However, let me check if maybe there's a case where a is greater than 2 but still somehow covers all real numbers? Earlier, we saw for a = 3, there's a gap. Let me think again.

For a > 2, the linear part has range (-∞, a² + 2a) and the quadratic part has range [a³, ∞). Since a > 2, let's check if a² + 2a ≥ a³? For a = 2, a² + 2a = 4 + 4 = 8, a³ = 8, so equality. For a > 2, let's see:

a² + 2a - a³ = -a³ + a² + 2a = -a(a² - a - 2). For a > 2, the quadratic a² - a - 2 is positive (since for a > 2, a² - a - 2 is positive). Therefore, -a times a positive number is negative. Therefore, a² + 2a - a³ < 0, so a² + 2a < a³. Therefore, there's a gap between a² + 2a and a³, so the function doesn't cover all real numbers. Therefore, a cannot be greater than 2.

Therefore, the maximum value of a is 2. So the answer is 2. Wait, but let me check a = -2 again. Since a = -2 is less than 2, so 2 is larger. So even though a = -2 works, the problem asks for the largest value of a, which is 2. Therefore, I think the answer is 2.

But let me check the case where a is between -2 and 0. Suppose a = -1. Then, the linear part is for x < -1, f(x) = -x + 2*(-1) = -x - 2. Wait no, original definition is ax + 2a. For a = -1, that's -1*x + 2*(-1) = -x - 2. The slope is -1, which is negative. As x approaches -infty, -x - 2 approaches +infty, and as x approaches -1 from the left, it approaches -(-1) - 2 = 1 - 2 = -1. Therefore, the range of the linear part is (-1, ∞). The quadratic part for x ≥ -1 is f(x) = -1x². Which is a downward opening parabola. For x ≥ -1, the maximum occurs at x = 0, which is 0. As x approaches infinity, it approaches -infty. At x = -1, the value is -(-1)^2 = -1. Therefore, the quadratic part has range (-infty, 0]. Therefore, combining both parts, the total range is (-infty, 0] ∪ (-1, ∞) = ℝ. Wait, because (-infty, 0] ∪ (-1, ∞) is ℝ? Wait, no. Because (-infty, 0] includes up to 0, and (-1, ∞) starts at -1. Therefore, the union would be (-infty, ∞). Wait, because everything less than or equal to 0 is covered by the first set, and everything greater than -1 is covered by the second set. But between -1 and 0, both sets cover it. Therefore, yes, the union is all real numbers. Wait, but this contradicts earlier reasoning. Wait, for a = -1, which is between -2 and 0, the function does have range ℝ?

Wait, earlier I thought that for a between -2 and 0, the function does not cover ℝ. But for a = -1, it seems like it does. Wait, let me check this again. For a = -1, the linear part is for x < -1, f(x) = -x - 2. The range of that is (-infty, -(-1) - 2) = (-infty, 1 - 2) = (-infty, -1). Wait, wait, no. Wait, when a = -1, the linear function is ax + 2a = -x - 2. Since the slope is -1 (negative), as x increases towards -1 from the left, the function value decreases to -(-1) -2 = 1 - 2 = -1. Wait, but if the slope is negative, increasing x leads to decreasing f(x). Therefore, as x approaches -infty, f(x) approaches +infty, and as x approaches -1 from the left, f(x) approaches -1. Therefore, the range of the linear part is (-1, ∞), not (-infty, -1). Wait, that's different from what I thought earlier. Wait, confusion here.

Let me be precise. For the linear function f(x) = -x - 2 for x < -1. Let me think about how it behaves. Let me pick a value of x approaching -infty. For example, x = -N where N is large positive. Then f(x) = -(-N) - 2 = N - 2, which approaches infinity as N approaches infinity. So f(x) approaches +infty as x approaches -infty. When x approaches -1 from the left, say x approaches -1⁻, then f(x) approaches -(-1) - 2 = 1 - 2 = -1. Therefore, since the function is decreasing (slope is -1), as x increases from -infty to -1, f(x) decreases from +infty to -1. Therefore, the range of the linear part is (-1, ∞). The quadratic part is for x ≥ -1: f(x) = -x². At x = -1, f(-1) = -(-1)^2 = -1. As x increases from -1 to infinity, f(x) decreases from -1 to -infty. Therefore, the range of the quadratic part is (-infty, -1]. Therefore, combining both parts, the total range is (-1, ∞) ∪ (-infty, -1] = ℝ. Therefore, for a = -1, the range is all real numbers. Wait, so this contradicts my earlier conclusion that for a between -2 and 0, the function doesn't have range ℝ. So my previous analysis was wrong. There's a mistake here.

Earlier, I thought that for a < 0, the linear part's range is (a² + 2a, ∞). But that seems incorrect. Let me re-examine. For a negative a, the linear part is ax + 2a for x < a. The slope is a < 0, so the function is decreasing. Therefore, as x approaches -infty, ax + 2a approaches +infty (since a is negative, x approaches -infty, so ax approaches +infty). As x approaches a from the left, it approaches a*a + 2a = a² + 2a. Therefore, since the function is decreasing, the range is (a² + 2a, ∞). However, in the case of a = -1, this would be ( (-1)^2 + 2*(-1), ∞ ) = (1 - 2, ∞) = (-1, ∞), which matches the actual calculation. Then the quadratic part for x ≥ a (which is x ≥ -1) is ax², which for a = -1 is -x². The range of that is (-infty, 0], but since the domain is x ≥ -1, the minimum value is at x approaching infinity, which approaches -infty, and the maximum is at x = 0, which is 0. Wait, but for x ≥ -1, the maximum value of -x² is at x = 0, which is 0. However, for x between -1 and 0, -x² is between 0 and -(-1)^2 = -1. Wait, no, -x² when x is between -1 and 0: x is negative, so x² is positive, so -x² is negative. The maximum occurs at x = 0, which is 0. The minimum is when x² is maximum, which occurs at x = -1, so -x² = -1. Therefore, the range of the quadratic part is [-1, 0]. Wait, but earlier I thought it was (-infty, -1]. That was a mistake. So, correcting that:

For a negative a, the quadratic part is ax² for x ≥ a. The function ax² is a downward opening parabola. The domain is x ≥ a. The maximum occurs at x = 0 (if 0 is within the domain, which it is when a ≤ 0). The value at x = 0 is a*(0)^2 = 0. The minimum value on the domain x ≥ a depends on the endpoints. At x = a, it's a*(a)^2 = a³. Then, as x increases beyond a, the function decreases from a³ to negative infinity. However, if the domain includes x = 0 (which it does since a ≤ 0), then the maximum is 0. So the range of the quadratic part is [a³, 0] ∪ something? Wait, no. Wait, for x ≥ a, which is a ≤ 0, the function ax² decreases from x = a to x approaching infinity. But since the function is continuous, the range is (-infty, max_value]. The max_value is at x = 0, which is 0. Therefore, the range is (-infty, 0]. But when x is restricted to x ≥ a, where a ≤ 0, what is the minimum value? At x = a, it is a³. Then as x increases from a to infinity, the value decreases from a³ to -infty. Therefore, the range is (-infty, max_value], where max_value is 0 (since x can go up to 0). Wait, but if the domain is x ≥ a, which includes x from a to ∞, then the maximum is 0 (at x = 0), and the minimum is -infty. Therefore, the range is (-infty, 0]. However, at x = a, the value is a³. Therefore, depending on a³, there might be an issue.

Wait, for example, if a = -1, then at x = -1, the value is a³ = -1. The rest of the values go from -1 to 0 (as x goes from -1 to 0) and then from 0 to -infty (as x goes from 0 to ∞). So the range is (-infty, 0]. But specifically, between -infty and 0. So when combined with the linear part's range of (-1, ∞), the total range is (-infty, 0] ∪ (-1, ∞) = ℝ. Therefore, even though for a = -1, which is between -2 and 0, the function does cover all real numbers. This suggests that my previous analysis was incorrect.

Therefore, I need to redo the analysis for a < 0. Let me try again.

For a < 0:

Linear part: ax + 2a for x < a. As discussed, range is (a² + 2a, ∞).

Quadratic part: ax² for x ≥ a. As discussed, range is (-∞, 0].

Therefore, combining the ranges: (-∞, 0] ∪ (a² + 2a, ∞). To cover ℝ, we need that the intervals overlap or fill in the gap. The gap would be between 0 and a² + 2a if a² + 2a > 0. If a² + 2a ≤ 0, then (a² + 2a, ∞) starts at ≤ 0, which would overlap with (-∞, 0], covering all real numbers. Therefore, the condition is a² + 2a ≤ 0. Solving a² + 2a ≤ 0 ⇒ a(a + 2) ≤ 0 ⇒ a ∈ [-2, 0].

Therefore, for a ∈ [-2, 0], the function f(x) has range ℝ. Therefore, for a between -2 and 0, the function's range is ℝ. Therefore, earlier mistake was due to incorrect calculation of the quadratic part's range. Therefore, in this case, for a between -2 and 0, the range is ℝ. Hence, possible values of a are [-2, 0] ∪ [0, 2], but since for a = 0, the function is constant 0, which doesn't cover ℝ. Therefore, a ∈ [-2, 0) ∪ (0, 2]. Therefore, the largest value of a is 2.

Therefore, even though there are negative values of a that work, the maximum a is still 2. Therefore, the answer is 2.

Wait, but let me verify with a = -1, which is in [-2, 0], the range is ℝ, as we saw. Similarly, for a = -2, we have the linear part's range is (a² + 2a, ∞) = (4 - 4, ∞) = (0, ∞), and quadratic part is (-∞, 0], so union is ℝ. Therefore, yes. Therefore, for a between -2 and 0, the range is ℝ. For a between 0 and 2, also range is ℝ. For a = 0, it's not. Therefore, the maximum value of a is 2. Therefore, the answer is 2.

But let me check if there is a case when a > 2, but somehow the function covers all real numbers. For example, suppose a = 3. Then linear part range is (-infty, a² + 2a) = (-infty, 15), quadratic part range is [a³, ∞) = [27, ∞). So there is a gap between 15 and 27, so not all real numbers. Therefore, no. So, the maximum a is 2.

But just to check another value in between, say a = 1. The linear part range is (-infty, 1 + 2) = (-infty, 3), quadratic part range is [1, ∞). So union is ℝ. Correct. For a = 2, linear part is (-infty, 8), quadratic part [8, ∞). Union is ℝ. For a = -1, linear part is (-1, ∞), quadratic part (-infty, 0]. Union is ℝ. For a = -2, linear part (0, ∞), quadratic part (-infty, 0]. Union is ℝ. So all these cases work. Therefore, the maximum a is 2.

Therefore, the answer is \boxed{2}.

**Final Answer**
\boxed{2}

To determine the **largest value of $ a $** such that the graph of the function
$$
f(x) =
\begin{cases}
ax^2 & \text{if } x \geq a,\\
ax + 2a & \text{if } x < a,
\end{cases}
$$
intersects **every horizontal line at least once**, we must ensure that the **range of $ f(x) $ is the entire set of real numbers $ \mathbb{R} $**.

This is because for every real number $ k $, there should be at least one $ x $ such that $ f(x) = k $, meaning that the range must be all real numbers.

---

### Step 1: Analyze the two pieces of the function

#### Case 1: $ x < a $

Define: $ f(x) = ax + 2a = a(x + 2) $

- This is a **linear** function with slope $ a $.
- The output values lie in the interval:
  $$
  (-\infty, a^2 + 2a) \quad \text{(if } a > 0 \text{ and the function is increasing)}
  $$
  or
  $$
  (a^2 + 2a, \infty) \quad \text{(if } a < 0 \text{ and the function is decreasing)}
  $$

#### Case 2: $ x \geq a $

Define: $ f(x) = ax^2 $

- This is a **quadratic function**.
- If $ a > 0 $, it opens **upward**, with **minimum value at $ x = a $**:
  $$
  f(a) = a(a)^2 = a^3
  $$
  So the range on $ x \geq a $ is:
  $$
  [a^3, \infty)
  $$
- If $ a < 0 $, it opens **downward**, with **maximum value at $ x = 0 $**:
  $$
  f(0) = a(0)^2 = 0
  $$
  So the range on $ x \geq a $ is:
  $$
  (-\infty, 0]
  $$
- If $ a = 0 $, then $ f(x) = 0 $ for all $ x $, and the range is just $ \{0\} $, which does **not** cover $ \mathbb{R} $.

---

### Step 2: Determine when the range equals $ \mathbb{R} $

To have the total range of $ f(x) $ as $ \mathbb{R} $, the ranges of the two parts must **cover all real numbers** without gaps.

#### Case: $ a > 0 $

- Linear part: $ (-\infty, a^2 + 2a) $
- Quadratic part: $ [a^3, \infty) $
- To cover all real numbers, we must have overlap (i.e., the upper bound of the linear part is **at least** the lower bound of the quadratic part):

$$
a^2 + 2a \geq a^3
\Rightarrow a^3 - a^2 - 2a \leq 0
\Rightarrow a(a^2 - a - 2) \leq 0
$$

Solving $ a(a - 2)(a + 1) \leq 0 $, we get:
$$
a \in (-\infty, -1] \cup [0, 2]
$$

But since $ a > 0 $, we only care about:
$$
a \in (0, 2]
$$

#### Case: $ a < 0 $

- Linear part: $ (a^2 + 2a, \infty) $
- Quadratic part: $ (-\infty, 0] $
- To cover all real numbers, the linear part must reach at most 0:

$$
a^2 + 2a \leq 0
\Rightarrow a(a + 2) \leq 0
\Rightarrow a \in [-2, 0]
$$

So for $ a \in [-2, 0] $, the range of $ f(x) $ is $ \mathbb{R} $.

#### Case: $ a = 0 $

- Entire function is constant zero (range = {0}), which **does not** cover $ \mathbb{R} $

---

### Step 3: Combine all valid ranges

So, the function will have full range $ \mathbb{R} $ if:
$$
a \in [-2, 0) \cup (0, 2]
$$

We are asked for the **largest** value of $ a $, so we choose the **maximum** value in this union:
$$
\boxed{2}
$$

---

### Final Answer

$$
\boxed{2}
$$

[Assistant]
Explanation:
Okay, so I have this function f(x) defined piecewise. It's ax² when x is greater than or equal to a, and ax + 2a when x is less than a. The question is asking for the largest value of a such that the graph of y = f(x) intersects every horizontal line at least once. Hmm, interesting. Let me try to figure this out step by step.

First, I need to understand what the question is really asking. It says the graph intersects every horizontal line at least once. That means, for any horizontal line y = k, there must be some x such that f(x) = k. So essentially, the function must be surjective onto its range? Or maybe not exactly surjective, but for every horizontal line, which would correspond to every real number if the range is all real numbers. Wait, maybe not. Wait, the problem says "every horizontal line," so maybe the function's range has to be all real numbers? Wait, but if the graph intersects every horizontal line, that would mean that for every real number k, there exists at least one x with f(x) = k. So the function must be surjective onto ℝ. But maybe not? Wait, no. Because horizontal lines can be y = k for any real number k. So if the function's range is all real numbers, then yes, every horizontal line would intersect the graph. However, if the range is not all real numbers, then there are some horizontal lines that don't intersect the graph. Therefore, the question is essentially asking for the largest value of a such that the range of f(x) is all of ℝ. Wait, but maybe not exactly. Wait, maybe not all horizontal lines? Wait, the problem says "intersects every horizontal line at least once," so for every horizontal line, meaning for every real number k, there is an x such that f(x) = k. Therefore, the range of f(x) must be all real numbers. Therefore, we need to find the largest a such that f(x) is surjective onto ℝ.

Alternatively, maybe there's a different interpretation? For example, maybe some horizontal lines might intersect the graph multiple times, but the question is just saying that each horizontal line must intersect at least once. So regardless, the function has to cover all real numbers. Therefore, the range must be ℝ. So we need to find the largest a such that the range of f(x) is ℝ.

Alternatively, perhaps even if the range isn't all real numbers, but for some reason every horizontal line still intersects the graph? Wait, that can't be. If the range is not all real numbers, then there must be some k not in the range, so the horizontal line y = k would not intersect the graph. Therefore, the problem reduces to finding the largest a such that f(x) is surjective onto ℝ. Therefore, we need to find the largest a such that the range of f(x) is all real numbers. So let's analyze the function f(x) and find for which a the range is ℝ.

First, let's analyze the function. It's piecewise defined. For x >= a, it's ax². For x < a, it's ax + 2a. So the function has two parts: a quadratic part for x >= a and a linear part for x < a. Let me try to understand the behavior of each part.

First, let's consider the linear part: ax + 2a for x < a. Let's rewrite that as a(x + 2). So it's a linear function with slope a. Depending on the value of a, this linear part can be increasing or decreasing. Similarly, the quadratic part: ax² for x >= a. The quadratic function ax² is a parabola opening upwards if a > 0, downwards if a < 0, and if a = 0, it's a constant function 0. But since the function is defined piecewise, the value of a could be positive or negative. However, the problem says "a is some number," so real number. But we need to find the largest value of a. So perhaps a is positive? Wait, maybe not necessarily. Let me think.

First, let me consider the case when a is positive. Then, for x >= a, the quadratic part is ax², which opens upwards, so it goes to infinity as x increases, and at x = a, it takes the value a*(a)^2 = a³. On the other hand, the linear part for x < a is ax + 2a, which is a line with slope a. Since a is positive, this line is increasing. So as x approaches a from the left, the linear part approaches a*a + 2a = a² + 2a. Therefore, the left side of the function, the linear part, approaches a² + 2a as x approaches a from the left, while the quadratic part at x = a is a³. Therefore, for the function to be continuous at x = a, we need the left and right limits to agree. But the problem didn't specify continuity. Wait, the problem didn't say anything about continuity. So maybe we need to check whether the function is continuous? Or maybe not. Because if the function isn't continuous, then there might be a jump discontinuity at x = a, but the problem is about intersecting every horizontal line. However, even if there is a jump discontinuity, the function could still intersect every horizontal line. However, perhaps the problem assumes continuity? Wait, but the problem didn't say that. So maybe we need to check if there is continuity or not. Wait, but maybe the function is not necessarily continuous. Let me check.

Wait, let me check the original problem again. The function is defined as:

f(x) = ax² if x ≥ a,

f(x) = ax + 2a if x < a.

So there's no mention of continuity. Therefore, the function may have a jump discontinuity at x = a. Let me check the left and right limits at x = a.

Left-hand limit as x approaches a from the left: ax + 2a evaluated at x = a is a*a + 2a = a² + 2a.

Right-hand limit as x approaches a from the right: ax² evaluated at x = a is a*(a)^2 = a³.

Therefore, unless a² + 2a = a³, the function is discontinuous at x = a. Therefore, for continuity, we have to have a³ = a² + 2a. Let's solve that equation:

a³ - a² - 2a = 0,

Factor out an a:

a(a² - a - 2) = 0.

So either a = 0, or a² - a - 2 = 0.

Solving a² - a - 2 = 0:

Discriminant D = 1 + 8 = 9,

Solutions are [1 ± 3]/2, so 4/2 = 2 or -2/2 = -1.

Therefore, the function is continuous at x = a if and only if a = 0, a = 2, or a = -1. But the problem didn't specify continuity, so maybe we don't have to assume that. However, the problem is about intersecting every horizontal line. So if the function is discontinuous at x = a, there might be a gap in the range. Therefore, maybe continuity is important here? Wait, but even if there is a jump discontinuity, maybe the range can still cover all real numbers? Or maybe not. Let me think.

But since the problem is asking for the largest value of a, maybe the answer is a positive number. Let me check for different cases of a.

But first, maybe it's better to first assume continuity, because maybe the problem expects that. Or maybe not. Let me try to think without assuming continuity.

First, let's consider the range of the function. Let's split the function into two parts: for x < a, which is ax + 2a, and for x ≥ a, which is ax².

So for the linear part, x < a. The linear function ax + 2a. Depending on the value of a, this can be increasing or decreasing. Similarly, the quadratic part is ax² for x ≥ a. Let's analyze both parts.

First, the linear part: ax + 2a for x < a.

Case 1: a > 0. Then the linear function is increasing. As x approaches negative infinity, the value of ax + 2a approaches negative infinity if a > 0. Wait, no: If a > 0, then as x approaches negative infinity, ax + 2a approaches negative infinity. However, since x is restricted to x < a, which for a > 0, includes all real numbers less than a. Therefore, the linear part, when a > 0, can take values from negative infinity up to the value at x approaching a from the left, which is a*a + 2a = a² + 2a. So the range of the linear part is (-∞, a² + 2a).

Then, for the quadratic part, ax² for x ≥ a. Since a > 0, the quadratic function ax² is a parabola opening upwards. For x ≥ a, the minimum value occurs at x = a, which is a*(a)^2 = a³. Then as x increases beyond a, the quadratic part increases to infinity. Therefore, the range of the quadratic part is [a³, ∞).

Therefore, combining both parts, the total range of f(x) is (-∞, a² + 2a) ∪ [a³, ∞). So for the function to intersect every horizontal line, this union must be all of ℝ. Therefore, we need that the upper bound of the linear part (a² + 2a) is greater than or equal to the lower bound of the quadratic part (a³). Because if the linear part goes up to a² + 2a and the quadratic part starts at a³, then for the two intervals to cover all of ℝ, we need that a² + 2a ≥ a³, so that there's no gap between (-∞, a² + 2a) and [a³, ∞). Otherwise, if a² + 2a < a³, then there's a gap between a² + 2a and a³, which would mean that horizontal lines between a² + 2a and a³ would not be intersected by the graph. Therefore, the condition is a² + 2a ≥ a³, which simplifies to a³ - a² - 2a ≤ 0, or a(a² - a - 2) ≤ 0.

Let me solve that inequality. Let's consider the equation a(a² - a - 2) = 0. As before, the roots are a = 0, a = 2, a = -1. Therefore, the critical points are -1, 0, 2. So to solve a(a² - a - 2) ≤ 0, we can test intervals.

First, factor the expression:

a(a - 2)(a + 1) ≤ 0.

So the critical points divide the real line into four intervals:

1. (-∞, -1)
2. (-1, 0)
3. (0, 2)
4. (2, ∞)

Let me test each interval.

1. For a < -1, let's pick a = -2:

(-2)(-2 - 2)(-2 + 1) = (-2)(-4)(-1) = (-2)(4) = -8 ≤ 0. So satisfies the inequality.

2. For -1 < a < 0, let's pick a = -0.5:

(-0.5)(-0.5 - 2)(-0.5 + 1) = (-0.5)(-2.5)(0.5) = (-0.5)(-1.25) = 0.625 > 0. So doesn't satisfy.

3. For 0 < a < 2, let's pick a = 1:

(1)(1 - 2)(1 + 1) = (1)(-1)(2) = -2 ≤ 0. So satisfies.

4. For a > 2, let's pick a = 3:

(3)(3 - 2)(3 + 1) = 3*1*4 = 12 > 0. Doesn't satisfy.

Therefore, the inequality a(a - 2)(a + 1) ≤ 0 holds when a ∈ (-∞, -1] ∪ [0, 2].

But remember, we were analyzing the case where a > 0. So in this case, for a > 0, the inequality holds when a is in [0, 2]. However, we need to consider the original problem's condition where the function intersects every horizontal line. Therefore, in the case where a > 0, the range is (-∞, a² + 2a) ∪ [a³, ∞). For this to cover all real numbers, we need that a² + 2a ≥ a³. Which, as we saw, gives a ∈ [0, 2]. But since a > 0 in this case, it's a ∈ (0, 2].

But wait, the problem says "the largest value of a". So if a can be up to 2 in this case, maybe a = 2 is the answer. But let me check.

But hold on, we need to check if when a = 2, the ranges indeed cover all real numbers. Let's check when a = 2:

Linear part: x < 2, f(x) = 2x + 4. As x approaches -infty, it approaches -infty, and at x approaching 2 from the left, it approaches 2*2 + 4 = 8. Therefore, the range of the linear part is (-infty, 8).

Quadratic part: x ≥ 2, f(x) = 2x². At x = 2, it is 2*(2)^2 = 8. For x ≥ 2, it goes from 8 upwards to infinity. Therefore, the range of the quadratic part is [8, ∞). Therefore, the total range of f(x) is (-infty, 8) ∪ [8, ∞) = ℝ. Perfect, so for a = 2, the range is all real numbers, so every horizontal line intersects the graph. Therefore, a = 2 is acceptable.

Now, if a is larger than 2, say a = 3. Then, the linear part is x < 3, f(x) = 3x + 6. As x approaches -infty, it goes to -infty, and at x approaching 3 from the left, it approaches 3*3 + 6 = 15. The quadratic part is x ≥ 3: f(x) = 3x². At x = 3, that is 3*(9) = 27. So the quadratic part starts at 27 and goes to infinity. Therefore, the range would be (-infty, 15) ∪ [27, ∞), which leaves a gap between 15 and 27. Therefore, horizontal lines between 15 and 27 would not be intersected by the graph. Therefore, a = 3 is invalid.

Therefore, for a > 2, the function doesn't cover all real numbers. For a = 2, it does. So for positive a, the maximum is 2.

But let me check the case for a < 0. Maybe there are larger values of a? Wait, the question is asking for the largest value of a. If a can be negative, but maybe there's a larger a? Wait, but if a is positive, 2 is positive, so if there are negative values of a, they are less than 2, so 2 would still be the largest. However, we need to check if for some negative a, the function could also cover all real numbers. Wait, but since the problem is asking for the largest value of a, even if negative a's are possible, but since 2 is positive, it's larger. However, I need to check if for some negative a, maybe there's a higher value? Wait, no, negative numbers are less than positive numbers. So if a can be 2, that's larger than any negative number. Therefore, the maximum is 2? But maybe not. Let me check the case when a is negative.

Case 2: a < 0.

First, analyze the linear part: ax + 2a for x < a. Since a is negative, the slope of the linear part is negative. Therefore, as x approaches -infty, ax + 2a approaches positive infinity (because a is negative, x approaches -infty, so ax approaches positive infinity). As x approaches a from the left, the value is a*a + 2a = a² + 2a. Therefore, since the linear function is decreasing (because slope a is negative), the range of the linear part is (a² + 2a, ∞). Because as x increases towards a, the value decreases to a² + 2a, so the range is from that value up to infinity.

For the quadratic part: ax² for x ≥ a. Since a is negative, and x ≥ a. Let's note that x ≥ a, but since a is negative, x could be anything greater than or equal to a. But since a is negative, and the quadratic function ax² is opening downward (because a is negative). The maximum of ax² occurs at x = 0, but x ≥ a. However, if a is negative, then x ≥ a includes x from a up to infinity. However, the quadratic ax² for x ≥ a. Let's check the behavior of this quadratic.

Since a is negative, ax² is a downward opening parabola. The vertex is at x = 0. But our domain for the quadratic part is x ≥ a (where a is negative). Therefore, for x ≥ a, the maximum occurs at x = 0 if 0 ≥ a, which it is, since a is negative. Therefore, the maximum value of the quadratic part is at x = 0: a*(0)^2 = 0. Wait, but x must be ≥ a. Since x = 0 ≥ a (because a is negative), so yes. Therefore, the quadratic function ax² on x ≥ a has maximum value 0 at x = 0, and as x approaches infinity, ax² approaches negative infinity. However, we need to check the minimum value of the quadratic part. Wait, since it's a downward opening parabola, the values go from the maximum at x = 0 (which is 0) down to negative infinity as x approaches infinity. However, on the interval x ≥ a, we need to find the range of ax². Let me check the value at x = a: a*(a)^2 = a³. As x increases from a to infinity, ax² goes from a³ down to negative infinity. Therefore, the range of the quadratic part is (-∞, 0].

Wait, hold on. Let me verify this. For a negative a, the quadratic function ax² is negative or zero. So when x ≥ a, the values of ax² depend on x. For example, if a = -1, and x ≥ -1. Then ax² = -x². At x = -1, it's -(-1)^2 = -1; as x increases from -1 to 0, -x² goes from -1 to 0; then from x = 0 to infinity, it goes from 0 to negative infinity. Therefore, the range of the quadratic part is (-∞, 0]. Therefore, if a is negative, the quadratic part's range is (-∞, 0].

Meanwhile, the linear part's range is (a² + 2a, ∞). Therefore, combining both parts, the total range of the function is (-∞, 0] ∪ (a² + 2a, ∞). For this to be all real numbers, we need that the upper part (a² + 2a, ∞) must cover the gap between the lower part's upper bound (0) and infinity. Wait, but the upper part starts at a² + 2a. For the entire range to be ℝ, we need that (-∞, 0] ∪ (a² + 2a, ∞) covers all real numbers. Therefore, we need that 0 ≤ a² + 2a. Because if 0 ≤ a² + 2a, then (a² + 2a, ∞) would start at or above 0, and since the lower part already covers up to 0, then there is no gap. However, if 0 > a² + 2a, then there's a gap between a² + 2a and 0. Therefore, the condition is that 0 ≤ a² + 2a. Let's solve that inequality:

a² + 2a ≥ 0 ⇒ a(a + 2) ≥ 0.

Which occurs when a ≤ -2 or a ≥ 0. But in this case, we are considering a < 0. Therefore, the condition becomes a ≤ -2.

Therefore, for a < 0, in order for the function to have range ℝ, we need a ≤ -2. Therefore, if a is ≤ -2, then the range is (-∞, 0] ∪ (a² + 2a, ∞). But since a ≤ -2, let's check what a² + 2a is. Let me compute a² + 2a for a ≤ -2. Let's take a = -2: a² + 2a = 4 + (-4) = 0. For a < -2, say a = -3: a² + 2a = 9 + (-6) = 3. Wait, that's positive. Wait, but for a < -2, let's see:

a² + 2a = a(a + 2). Since a ≤ -2, then a is negative, and a + 2 is negative when a < -2 (since a + 2 < 0). Therefore, a(a + 2) is positive (negative times negative). Therefore, for a ≤ -2, a² + 2a is positive or zero. Therefore, the upper part (a² + 2a, ∞) is (positive number, ∞). Therefore, combining with (-∞, 0], the total range is (-∞, 0] ∪ (something positive, ∞). Therefore, there is a gap between 0 and the something positive. Hence, the function does not cover all real numbers. Wait, that's a problem. Wait, but if a² + 2a is positive, then the upper part starts at a² + 2a, which is greater than 0, so the range would be (-∞, 0] ∪ (a² + 2a, ∞), which would leave a gap between 0 and a² + 2a. Therefore, the horizontal lines between 0 and a² + 2a are not covered. Therefore, even for a ≤ -2, the function does not cover all real numbers. Therefore, that suggests that for a < 0, there's no value of a where the function covers all real numbers?

Wait, but if a = -2, then a² + 2a = 4 + (-4) = 0. Therefore, the upper part is (0, ∞). Therefore, the total range is (-∞, 0] ∪ (0, ∞) = ℝ. Wait, so for a = -2, the range is ℝ. Wait, is that correct?

For a = -2, the linear part is x < -2, f(x) = -2x + 2*(-2) = -2x - 4. Let's check its range. Since the slope is -2 (negative), as x approaches -infty, -2x - 4 approaches positive infinity, and as x approaches -2 from the left, the value approaches -2*(-2) - 4 = 4 - 4 = 0. Therefore, the range of the linear part is (0, ∞). Wait, but earlier I thought it was (a² + 2a, ∞), which for a = -2 is 0, so (0, ∞). And the quadratic part is for x ≥ -2, f(x) = -2x². So for x ≥ -2, the quadratic function -2x². The maximum value occurs at x = 0, which is 0. As x increases beyond 0, -2x² becomes negative, and as x approaches -infty, but wait x is ≥ -2. So for x ≥ -2, the quadratic function -2x² has maximum value 0 at x = 0 and goes to -infty as x approaches ±infty? Wait, no. For x ≥ -2, when x approaches -infty? No, x is restricted to x ≥ -2. So x can go from -2 to infinity. For x between -2 and 0, the quadratic function -2x² is decreasing from -2*(-2)^2 = -8 to 0. For x ≥ 0, it's -2x², which decreases from 0 to negative infinity. Therefore, the range of the quadratic part is (-∞, 0]. Therefore, combining the linear part (0, ∞) and quadratic part (-∞, 0], the total range is ℝ. Therefore, for a = -2, the range is ℝ. However, when a < -2, for example, a = -3, then as I mentioned earlier, the linear part has range (a² + 2a, ∞). For a = -3, that's 9 + (-6) = 3, so (3, ∞). The quadratic part has range (-∞, 0]. Therefore, the total range is (-∞, 0] ∪ (3, ∞). So there is a gap between 0 and 3, hence not covering all real numbers. Therefore, only when a = -2 does the range become ℝ. Therefore, for a < 0, only a = -2 gives the entire real numbers. Therefore, if we consider the case for a < 0, the only value that works is a = -2.

Therefore, putting it together, for the function to have range ℝ, a must be in [0, 2] (for a > 0) or a = -2 (for a < 0). Therefore, the largest value of a is 2. However, let me check a = -2. Is -2 larger than 2? No, -2 < 2, so 2 is larger. Therefore, the maximum value is 2. However, let me check if for some a between 0 and 2, maybe there's a possibility that the function covers all real numbers? Wait, no. For a between 0 and 2, we saw that the condition a² + 2a ≥ a³ holds. Which ensures that the two ranges (-∞, a² + 2a) and [a³, ∞) overlap or meet, so that the total range is ℝ. For example, take a = 1. Then linear part range is (-∞, 1 + 2) = (-∞, 3). Quadratic part range is [1, ∞). Since 3 ≥ 1, the union is ℝ. So that works. Similarly, for a = 2, the linear part approaches 8, quadratic part starts at 8, so union is ℝ. Therefore, for all a between 0 and 2 inclusive, the range is ℝ. Therefore, the maximum value of a is 2. However, wait, the problem says "the largest value of a". But let me check if maybe for a = -2, which is smaller than 2, but maybe there is something else?

Wait, but no, because -2 is less than 2. So 2 is larger. Therefore, the answer is 2. Wait, but let me check for a = 0. If a = 0, the function becomes:

For x ≥ 0: f(x) = 0*x² = 0.

For x < 0: f(x) = 0*x + 2*0 = 0. Therefore, f(x) is identically 0. Therefore, the range is {0}, which is not all real numbers. Therefore, a = 0 is invalid. Therefore, in the earlier case, when a is between 0 and 2, we need to check if the ranges actually cover ℝ. Wait, when a is between 0 and 2, the linear part's upper bound is a² + 2a, and quadratic part starts at a³. Since a² + 2a ≥ a³, so the upper bound of the linear part is higher than or equal to the lower bound of the quadratic part, so there's no gap. Therefore, the union is (-∞, upper bound) ∪ [lower bound, ∞) which becomes ℝ if upper bound ≥ lower bound. Wait, no, the upper bound is a² + 2a and lower bound is a³. If upper bound ≥ lower bound, then (-∞, upper bound) ∪ [lower bound, ∞) is (-∞, upper bound) ∪ [lower bound, ∞). But since upper bound ≥ lower bound, the union is ℝ if upper bound is infinity? Wait, no. Wait, if upper bound is some finite number, say, for a between 0 and 2, say a = 1, upper bound is 3, lower bound is 1. Therefore, the union is (-∞, 3) ∪ [1, ∞) = ℝ. Because (-∞, 3) includes from -infty to 3, and [1, ∞) includes from 1 to infinity. Therefore, together they cover all real numbers. Similarly, for a = 2, upper bound is 8 and lower bound is 8, so the union is (-∞, 8) ∪ [8, ∞) = ℝ.

Therefore, yes, for all a in [0, 2], the function's range is ℝ. However, when a = 0, the function is constant 0, which only covers {0}, so a must be in (0, 2]. Therefore, the largest value of a is 2. Therefore, the answer is 2. However, let me check if maybe there's a case where a is greater than 2 but still somehow covers all real numbers? Earlier, we saw for a = 3, there's a gap. Let me think again.

For a > 2, the linear part has range (-∞, a² + 2a) and the quadratic part has range [a³, ∞). Since a > 2, let's check if a² + 2a ≥ a³? For a = 2, a² + 2a = 4 + 4 = 8, a³ = 8, so equality. For a > 2, let's see:

a² + 2a - a³ = -a³ + a² + 2a = -a(a² - a - 2). For a > 2, the quadratic a² - a - 2 is positive (since for a > 2, a² - a - 2 is positive). Therefore, -a times a positive number is negative. Therefore, a² + 2a - a³ < 0, so a² + 2a < a³. Therefore, there's a gap between a² + 2a and a³, so the function doesn't cover all real numbers. Therefore, a cannot be greater than 2.

Therefore, the maximum value of a is 2. So the answer is 2. Wait, but let me check a = -2 again. Since a = -2 is less than 2, so 2 is larger. So even though a = -2 works, the problem asks for the largest value of a, which is 2. Therefore, I think the answer is 2.

But let me check the case where a is between -2 and 0. Suppose a = -1. Then, the linear part is for x < -1, f(x) = -x + 2*(-1) = -x - 2. Wait no, original definition is ax + 2a. For a = -1, that's -1*x + 2*(-1) = -x - 2. The slope is -1, which is negative. As x approaches -infty, -x - 2 approaches +infty, and as x approaches -1 from the left, it approaches -(-1) - 2 = 1 - 2 = -1. Therefore, the range of the linear part is (-1, ∞). The quadratic part for x ≥ -1 is f(x) = -1x². Which is a downward opening parabola. For x ≥ -1, the maximum occurs at x = 0, which is 0. As x approaches infinity, it approaches -infty. At x = -1, the value is -(-1)^2 = -1. Therefore, the quadratic part has range (-infty, 0]. Therefore, combining both parts, the total range is (-infty, 0] ∪ (-1, ∞) = ℝ. Wait, because (-infty, 0] ∪ (-1, ∞) is ℝ? Wait, no. Because (-infty, 0] includes up to 0, and (-1, ∞) starts at -1. Therefore, the union would be (-infty, ∞). Wait, because everything less than or equal to 0 is covered by the first set, and everything greater than -1 is covered by the second set. But between -1 and 0, both sets cover it. Therefore, yes, the union is all real numbers. Wait, but this contradicts earlier reasoning. Wait, for a = -1, which is between -2 and 0, the function does have range ℝ?

Wait, earlier I thought that for a between -2 and 0, the function does not cover ℝ. But for a = -1, it seems like it does. Wait, let me check this again. For a = -1, the linear part is for x < -1, f(x) = -x - 2. The range of that is (-infty, -(-1) - 2) = (-infty, 1 - 2) = (-infty, -1). Wait, wait, no. Wait, when a = -1, the linear function is ax + 2a = -x - 2. Since the slope is -1 (negative), as x increases towards -1 from the left, the function value decreases to -(-1) -2 = 1 - 2 = -1. Wait, but if the slope is negative, increasing x leads to decreasing f(x). Therefore, as x approaches -infty, f(x) approaches +infty, and as x approaches -1 from the left, f(x) approaches -1. Therefore, the range of the linear part is (-1, ∞), not (-infty, -1). Wait, that's different from what I thought earlier. Wait, confusion here.

Let me be precise. For the linear function f(x) = -x - 2 for x < -1. Let me think about how it behaves. Let me pick a value of x approaching -infty. For example, x = -N where N is large positive. Then f(x) = -(-N) - 2 = N - 2, which approaches infinity as N approaches infinity. So f(x) approaches +infty as x approaches -infty. When x approaches -1 from the left, say x approaches -1⁻, then f(x) approaches -(-1) - 2 = 1 - 2 = -1. Therefore, since the function is decreasing (slope is -1), as x increases from -infty to -1, f(x) decreases from +infty to -1. Therefore, the range of the linear part is (-1, ∞). The quadratic part is for x ≥ -1: f(x) = -x². At x = -1, f(-1) = -(-1)^2 = -1. As x increases from -1 to infinity, f(x) decreases from -1 to -infty. Therefore, the range of the quadratic part is (-infty, -1]. Therefore, combining both parts, the total range is (-1, ∞) ∪ (-infty, -1] = ℝ. Therefore, for a = -1, the range is all real numbers. Wait, so this contradicts my earlier conclusion that for a between -2 and 0, the function doesn't have range ℝ. So my previous analysis was wrong. There's a mistake here.

Earlier, I thought that for a < 0, the linear part's range is (a² + 2a, ∞). But that seems incorrect. Let me re-examine. For a negative a, the linear part is ax + 2a for x < a. The slope is a < 0, so the function is decreasing. Therefore, as x approaches -infty, ax + 2a approaches +infty (since a is negative, x approaches -infty, so ax approaches +infty). As x approaches a from the left, it approaches a*a + 2a = a² + 2a. Therefore, since the function is decreasing, the range is (a² + 2a, ∞). However, in the case of a = -1, this would be ( (-1)^2 + 2*(-1), ∞ ) = (1 - 2, ∞) = (-1, ∞), which matches the actual calculation. Then the quadratic part for x ≥ a (which is x ≥ -1) is ax², which for a = -1 is -x². The range of that is (-infty, 0], but since the domain is x ≥ -1, the minimum value is at x approaching infinity, which approaches -infty, and the maximum is at x = 0, which is 0. Wait, but for x ≥ -1, the maximum value of -x² is at x = 0, which is 0. However, for x between -1 and 0, -x² is between 0 and -(-1)^2 = -1. Wait, no, -x² when x is between -1 and 0: x is negative, so x² is positive, so -x² is negative. The maximum occurs at x = 0, which is 0. The minimum is when x² is maximum, which occurs at x = -1, so -x² = -1. Therefore, the range of the quadratic part is [-1, 0]. Wait, but earlier I thought it was (-infty, -1]. That was a mistake. So, correcting that:

For a negative a, the quadratic part is ax² for x ≥ a. The function ax² is a downward opening parabola. The domain is x ≥ a. The maximum occurs at x = 0 (if 0 is within the domain, which it is when a ≤ 0). The value at x = 0 is a*(0)^2 = 0. The minimum value on the domain x ≥ a depends on the endpoints. At x = a, it's a*(a)^2 = a³. Then, as x increases beyond a, the function decreases from a³ to negative infinity. However, if the domain includes x = 0 (which it does since a ≤ 0), then the maximum is 0. So the range of the quadratic part is [a³, 0] ∪ something? Wait, no. Wait, for x ≥ a, which is a ≤ 0, the function ax² decreases from x = a to x approaching infinity. But since the function is continuous, the range is (-infty, max_value]. The max_value is at x = 0, which is 0. Therefore, the range is (-infty, 0]. But when x is restricted to x ≥ a, where a ≤ 0, what is the minimum value? At x = a, it is a³. Then as x increases from a to infinity, the value decreases from a³ to -infty. Therefore, the range is (-infty, max_value], where max_value is 0 (since x can go up to 0). Wait, but if the domain is x ≥ a, which includes x from a to ∞, then the maximum is 0 (at x = 0), and the minimum is -infty. Therefore, the range is (-infty, 0]. However, at x = a, the value is a³. Therefore, depending on a³, there might be an issue.

Wait, for example, if a = -1, then at x = -1, the value is a³ = -1. The rest of the values go from -1 to 0 (as x goes from -1 to 0) and then from 0 to -infty (as x goes from 0 to ∞). So the range is (-infty, 0]. But specifically, between -infty and 0. So when combined with the linear part's range of (-1, ∞), the total range is (-infty, 0] ∪ (-1, ∞) = ℝ. Therefore, even though for a = -1, which is between -2 and 0, the function does cover all real numbers. This suggests that my previous analysis was incorrect.

Therefore, I need to redo the analysis for a < 0. Let me try again.

For a < 0:

Linear part: ax + 2a for x < a. As discussed, range is (a² + 2a, ∞).

Quadratic part: ax² for x ≥ a. As discussed, range is (-∞, 0].

Therefore, combining the ranges: (-∞, 0] ∪ (a² + 2a, ∞). To cover ℝ, we need that the intervals overlap or fill in the gap. The gap would be between 0 and a² + 2a if a² + 2a > 0. If a² + 2a ≤ 0, then (a² + 2a, ∞) starts at ≤ 0, which would overlap with (-∞, 0], covering all real numbers. Therefore, the condition is a² + 2a ≤ 0. Solving a² + 2a ≤ 0 ⇒ a(a + 2) ≤ 0 ⇒ a ∈ [-2, 0].

Therefore, for a ∈ [-2, 0], the function f(x) has range ℝ. Therefore, for a between -2 and 0, the function's range is ℝ. Therefore, earlier mistake was due to incorrect calculation of the quadratic part's range. Therefore, in this case, for a between -2 and 0, the range is ℝ. Hence, possible values of a are [-2, 0] ∪ [0, 2], but since for a = 0, the function is constant 0, which doesn't cover ℝ. Therefore, a ∈ [-2, 0) ∪ (0, 2]. Therefore, the largest value of a is 2.

Therefore, even though there are negative values of a that work, the maximum a is still 2. Therefore, the answer is 2.

Wait, but let me verify with a = -1, which is in [-2, 0], the range is ℝ, as we saw. Similarly, for a = -2, we have the linear part's range is (a² + 2a, ∞) = (4 - 4, ∞) = (0, ∞), and quadratic part is (-∞, 0], so union is ℝ. Therefore, yes. Therefore, for a between -2 and 0, the range is ℝ. For a between 0 and 2, also range is ℝ. For a = 0, it's not. Therefore, the maximum value of a is 2. Therefore, the answer is 2.

But let me check if there is a case when a > 2, but somehow the function covers all real numbers. For example, suppose a = 3. Then linear part range is (-infty, a² + 2a) = (-infty, 15), quadratic part range is [a³, ∞) = [27, ∞). So there is a gap between 15 and 27, so not all real numbers. Therefore, no. So, the maximum a is 2.

But just to check another value in between, say a = 1. The linear part range is (-infty, 1 + 2) = (-infty, 3), quadratic part range is [1, ∞). So union is ℝ. Correct. For a = 2, linear part is (-infty, 8), quadratic part [8, ∞). Union is ℝ. For a = -1, linear part is (-1, ∞), quadratic part (-infty, 0]. Union is ℝ. For a = -2, linear part (0, ∞), quadratic part (-infty, 0]. Union is ℝ. So all these cases work. Therefore, the maximum a is 2.

Therefore, the answer is \boxed{2}.

**Final Answer**
\boxed{2}

To determine the **largest value of $ a $** such that the graph of the function
$$
f(x) =
\begin{cases}
ax^2 & \text{if } x \geq a,\\
ax + 2a & \text{if } x < a,
\end{cases}
$$
intersects **every horizontal line at least once**, we must ensure that the **range of $ f(x) $ is the entire set of real numbers $ \mathbb{R} $**.

This is because for every real number $ k $, there should be at least one $ x $ such that $ f(x) = k $, meaning that the range must be all real numbers.

---

### Step 1: Analyze the two pieces of the function

#### Case 1: $ x < a $

Define: $ f(x) = ax + 2a = a(x + 2) $

- This is a **linear** function with slope $ a $.
- The output values lie in the interval:
  $$
  (-\infty, a^2 + 2a) \quad \text{(if } a > 0 \text{ and the function is increasing)}
  $$
  or
  $$
  (a^2 + 2a, \infty) \quad \text{(if } a < 0 \text{ and the function is decreasing)}
  $$

#### Case 2: $ x \geq a $

Define: $ f(x) = ax^2 $

- This is a **quadratic function**.
- If $ a > 0 $, it opens **upward**, with **minimum value at $ x = a $**:
  $$
  f(a) = a(a)^2 = a^3
  $$
  So the range on $ x \geq a $ is:
  $$
  [a^3, \infty)
  $$
- If $ a < 0 $, it opens **downward**, with **maximum value at $ x = 0 $**:
  $$
  f(0) = a(0)^2 = 0
  $$
  So the range on $ x \geq a $ is:
  $$
  (-\infty, 0]
  $$
- If $ a = 0 $, then $ f(x) = 0 $ for all $ x $, and the range is just $ \{0\} $, which does **not** cover $ \mathbb{R} $.

---

### Step 2: Determine when the range equals $ \mathbb{R} $

To have the total range of $ f(x) $ as $ \mathbb{R} $, the ranges of the two parts must **cover all real numbers** without gaps.

#### Case: $ a > 0 $

- Linear part: $ (-\infty, a^2 + 2a) $
- Quadratic part: $ [a^3, \infty) $
- To cover all real numbers, we must have overlap (i.e., the upper bound of the linear part is **at least** the lower bound of the quadratic part):

$$
a^2 + 2a \geq a^3
\Rightarrow a^3 - a^2 - 2a \leq 0
\Rightarrow a(a^2 - a - 2) \leq 0
$$

Solving $ a(a - 2)(a + 1) \leq 0 $, we get:
$$
a \in (-\infty, -1] \cup [0, 2]
$$

But since $ a > 0 $, we only care about:
$$
a \in (0, 2]
$$

#### Case: $ a < 0 $

- Linear part: $ (a^2 + 2a, \infty) $
- Quadratic part: $ (-\infty, 0] $
- To cover all real numbers, the linear part must reach at most 0:

$$
a^2 + 2a \leq 0
\Rightarrow a(a + 2) \leq 0
\Rightarrow a \in [-2, 0]
$$

So for $ a \in [-2, 0] $, the range of $ f(x) $ is $ \mathbb{R} $.

#### Case: $ a = 0 $

- Entire function is constant zero (range = {0}), which **does not** cover $ \mathbb{R} $

---

### Step 3: Combine all valid ranges

So, the function will have full range $ \mathbb{R} $ if:
$$
a \in [-2, 0) \cup (0, 2]
$$

We are asked for the **largest** value of $ a $, so we choose the **maximum** value in this union:
$$
\boxed{2}
$$

---

### Final Answer

$$
\boxed{2}
$$

Solution:
For $x < a,$ the graph of $y = f(x)$ is the same as the graph of $y = ax+2a,$ which is a line with slope $a$ and which passes through the point $(a, a^2+2a).$ For $x \ge a,$ the graph of $y = f(x)$ is the same as the graph of $y = ax^2,$ which is a parabola passing through the point $(a, a^3).$

Notice that the parabola only ever takes nonnegative values. Therefore, the line portion of the graph must have positive slope, because it must intersect horizontal lines which lie below the $x-$axis. Thus, $a > 0.$

For $a > 0,$ the line portion of the graph passes through all horizontal lines with height less than or equal to $a^2+2a,$ and the parabola portion of the graph passes through all horizontal lines with height greater than or equal to $a^3.$ Therefore, all horizontal lines are covered if and only if \[a^2 + 2a \ge a^3.\]Since $ a > 0,$ we can divide by $a$ to get \[a + 2 \ge a^2,\]so $0 \ge a^2 - a - 2 = (a-2) ( a+1).$ This means that $-1 \le a \le 2,$ so the greatest possible value of $a$ is $\boxed{2}.$

The graph of $y = f(x)$ for $a = 2$ is shown below (not to scale); note how the parabola and line meet at one point: [asy]
size(8cm);
import graph;

real a =2;
draw((-5,0)--(6,0),EndArrow());
draw((0,-6)--(0,14),EndArrow());

real g(real x) {return 0.5*a*(x-a)^2+a^3;}
real f(real x) {return a*x+2*a;}

draw(graph(f,-4.6,a),BeginArrow());
draw(graph(g,a,4.5),EndArrow());

label("$f(x)$",(0,15.5));
label("$x$",(6,0),E);
dot((2,8));
[/asy]