We need to parse problem statement.
We have a team of exactly n participants in each finals (year). The team formation: each participant can only attend at most two finals. So each person can be a rookie (first time) or a veteran (second time). The team composition is given by the number of veterans (participants who have attended a previous finals). Let ak be the number of veterans in the team at the k-th finals. Then there are n - ak rookies.
We need to choose a sequence a1, a2, a3,... (each integer 0 ≤ ak ≤ n) subject to the constraint that each participant can only attend at most twice. How does this constraint translate to the sequence? The condition: each person can appear as rookie (first time) then possibly as veteran later. That implies that the total number of appearances across all years is limited per individual: each person can appear at most twice, but they can be in at most two teams, consecutive or not? The statement: "each person can participate in the finals no more than twice." Typically, this means that over all years, each individual may be a participant at most twice. Also, they can be on the team at most twice. They might have a gap? But the typical constraint in ACM ICPC: a student can compete in at most two world finals (ICPC rules). So they can compete at most twice in total. In this problem, the university has an endless supply of students. So we can recruit any number of rookies each year, but each rookie can later become a veteran at most once (if they are selected again). And after they have been veteran (i.e., second time), they cannot compete again.
The constraint must be expressed mathematically: For each year k, the set of participants consists of some veterans (who have participated before) and some rookies (who have not). The veterans must have participated exactly once before. After they participate this second time, they cannot be used again. So the system is like: we have a pool of students that can be used at most twice. The sequence ak must be such that it's possible to assign students to years respecting these constraints.
We need to maximize the limit Ψ = lim_{K→∞} (1/K) * Σ_{k=1}^K p_{a_k} (the average probability of winning a medal) under the existence of that limit, given the ability to choose the sequence a_k subject to the constraints.
We want the maximum possible average probability achievable in the long run by a feasible sequence.
This is a classic problem of maximizing the average reward in a deterministic system with constraints. The sequence a_k is not free; it's constrained by the "resource" of students. Each rookie uses a new student; if we later want to use that student as a veteran, we need to "reuse" them exactly once later. Each veteran in a year consumes a student who is on his second appearance, and then that student disappears from the pool. So the process is: each year, we have a team of n participants; they can be either rookies or veterans. Rookies become "available to be veterans" next year? Actually, if a rookie participates in year k, then in future years they could be a veteran (i.e., participate again) at most once. But we need to decide whether to bring them back. So the "resource" is like: each student we recruit gives us one "first appearance". Then we can later (at some later year) use them for a second appearance, after which they are retired.
Thus, the dynamics: Let R_k be the number of rookies in year k, so R_k = n - a_k. Those rookies become "potential veterans" for future years. We can think of a stock of "once-used" students who are eligible to be veterans. Let S_k be the number of students that have participated exactly once before year k and are still available to be used as veterans (i.e., haven't been used a second time yet). Then, when we choose a_k veterans in year k, we take a_k of those from S_k. After they compete, they retire, so they are removed. Additionally, the R_k rookies are added to the stock for future years (they become part of S_{k+1} after they have participated once). So we have the state variable: S_k, the number of students who have participated exactly once and are available to be veterans at the beginning of year k (before selecting the team). The dynamics: we choose a_k ≤ min(n, S_k) (since veterans must come from S_k) and also a_k ≤ n obviously. Then the team consists of a_k veterans and R_k = n - a_k rookies. After the year, those a_k veterans are removed (they are used twice, now they are done). The R_k rookies are new participants; they will be added to the stock for future years. However, there may be also some previous stock that were not used as veterans; they remain available for future years. So the update: S_{k+1} = S_k - a_k + R_k, because: from S_k we used a_k, so remaining S_k - a_k are still once-used; plus the rookies from this year (n - a_k) become new once-used (they have participated once now). So:
S_{k+1} = S_k - a_k + (n - a_k) = S_k + n - 2 a_k.
We also have S_1 = 0 initially (no one has participated before year 1). So we start with S_1 = 0.
The feasibility constraints: for each k, 0 ≤ a_k ≤ min(n, S_k) (since we cannot have more veterans than available once-used participants). Also a_k integer. And we want to choose an infinite sequence a_k (and corresponding S_k) to maximize the average of p_{a_k}.
Note that p_i is nondecreasing in i (more veterans yields higher probability). So ideally we'd like as many veterans as possible each year, because p_i increases with i. However, using veterans consumes the stock S_k, and we need rookies to replenish the stock. If we always take a_k = n (all veterans), then S_{k+1} = S_k + n - 2n = S_k - n. Starting from S_1 = 0, we cannot take a_1 = n because S_1=0, min(0,n)=0. So we must start with some rookies. The trade-off: using more veterans gives higher immediate reward but depletes the stock; using fewer veterans (more rookies) builds stock for future. This is similar to managing a resource.
We are asked for the maximum limit average of p_{a_k}. This is an optimization of average reward per step in an infinite-horizon deterministic system with state S_k (nonnegative integer? Actually S_k can be any integer but must be nonnegative; also is there an upper bound? It could become large). The decisions a_k affect both immediate reward and state transition. Since p_i nondecreasing, we want to maximize average p.
This is a classic problem: it's like we have a "resource" S; each period we can choose a ∈ [0, min(n, S)]; we get reward p_a; and the state update: S' = S + n - 2a. The goal: maximize long-run average reward.
Because n is up to 100, and p_i are given as real numbers, we need to compute the optimal average reward.
This is a deterministic dynamic programming average reward problem with unbounded state. Since the rewards are bounded (between 0 and 1), there exists an optimal average reward λ. For such problems, often the optimal policy is periodic or stationary, and we can treat it as a linear programming problem or find a cycle that maximizes average reward.
Given the linear dynamics and convexity? Possibly the optimal policy is to maintain a certain stock level and choose a constant a (maybe fractional?) but a must be integer. However, we might be able to achieve any rational fraction of time with certain a values by cycling.
We can think of the system as: over the long run, the average "inflow" of rookies per year is n - a (average), and average "outflow" of veterans per year is a (since each veteran retires after second appearance). For stock S to be stable on average, we need average change in S to be zero: E[ΔS] = n - 2a = 0 => a = n/2. If a > n/2, stock decreases on average; if a < n/2, stock increases. Since S must be nonnegative, we cannot sustain a > n/2 indefinitely unless we sometimes have a < n/2 to replenish. So the long-run average a cannot exceed n/2. But wait, is that necessary? Let's analyze.
Define average over K steps: (S_{K+1} - S_1)/K = n - 2 * (average a_k). Since S_1 = 0, and S_{K+1} must be nonnegative, but can be arbitrarily large. For the limit average to exist (the limit of average p) we don't need S to be bounded; it could go to infinity. But if S grows without bound, that means we are accumulating many once-used participants who never get used as veterans. That's wasteful because they could be used to increase a_k later. However, maybe it's beneficial to sometimes accumulate stock to allow periods of high a. So the average a could be > n/2, but then S would tend to negative? Actually from update: S_{k+1} = S_k + n - 2a_k. If average a > n/2, then average increment negative, so eventually S would become negative, which is impossible because we cannot have negative stock (can't have more veterans than available). So to maintain nonnegative S_k for all k, the sequence must satisfy that for any prefix, the total depletion cannot exceed total accumulation. More precisely, from S_1 = 0, we have S_{k+1} = Σ_{i=1}^{k} (n - 2a_i). Since S_{k+1} ≥ 0, we need for all k: Σ_{i=1}^{k} (n - 2a_i) ≥ 0, i.e., Σ_{i=1}^{k} a_i ≤ (n/2) k + (S_1/2? Actually rearr: Σ (n - 2a_i) = n k - 2 Σ a_i ≥ 0 => Σ a_i ≤ (n/2) k. So for each k, the cumulative number of veterans used up to year k cannot exceed (n/2) k. This is a necessary and sufficient condition for feasibility given S_1=0? Also we need each a_i ≤ n and a_i integer and a_i ≤ S_i, but the cumulative condition might be enough if we can always schedule appropriately? However, the stock constraint is more subtle: a_k ≤ S_k, where S_k = Σ_{i=1}^{k-1} (n - 2a_i). So we need a_k ≤ Σ_{i=1}^{k-1} (n - 2a_i). This is a more stringent condition than just the cumulative sum at the end. It imposes that we cannot use too many veterans early before enough stock is built.
Nevertheless, the long-run average a (if it exists) must satisfy limsup_{K→∞} (1/K) Σ_{i=1}^{K} a_i ≤ n/2, because otherwise the cumulative sum would exceed (n/2)K for large K, violating Σ a_i ≤ (n/2)K (which follows from S_{K+1} ≥ 0). Actually from S_{K+1} = Σ_{i=1}^{K} (n - 2a_i) ≥ 0 => (1/K) Σ a_i ≤ n/2. So the average a cannot exceed n/2. Also, if average a is less than n/2, S_k will go to infinity linearly, which is feasible. So the maximum achievable long-run average a is n/2? But is it possible to achieve exactly n/2? That would require S_{K+1} = Σ (n - 2a_i) = 0 in the limit (i.e., average increment zero), but S_{K+1} might be bounded or grow sublinearly. For average a = n/2, we need the cumulative sum of (n - 2a_i) to be o(K). This can be achieved if we balance.
But note: a_k must be integer between 0 and n. Also, S_k must be large enough to allow a_k. Achieving average a = n/2 might require careful scheduling. For example, if n is even, we can set a_k = n/2 every year? But then S_{k+1} = S_k + n - 2*(n/2) = S_k, so if we start with S_1=0, can we have a_1 = n/2? No, because S_1=0, we need a_1 ≤ 0, so a_1 must be 0. So we need to build up stock first. Suppose we start with a_1=0, then S_2 = n. Then a_2 could be up to n. To maintain average a = n/2, we might use pattern: a_1=0, a_2=n, then S_3 = S_2 + n - 2n = n + n - 2n =0. This cycle repeats: (0,n) gives average a = n/2, and S cycles: 0 -> n -> 0. Check feasibility: For k=1: a_1=0 ≤ S_1=0? Actually S_1=0, min(n,S_1)=0, so a_1=0 OK. Then S_2 = 0 + n - 0 = n. For k=2: a_2 = n ≤ S_2 = n, OK. Then S_3 = n + n - 2n =0. So pattern works if n ≤ n obviously. So for any n, this pattern is feasible: a_k alternating 0 and n. Then average a = n/2. For odd n, n/2 is not integer. But average can be n/2 (non-integer) by alternating between floor and ceil appropriately. For example, n=3, pattern: 0,3 gives average 1.5; but is a=3 allowed? Yes, if S_k >=3. In pattern 0,3, S after 0: S_2=3, then a_2=3 uses all stock, S_3=0. So cycle of length 2 works. So average a=1.5, exactly n/2. So it's feasible to achieve average a = n/2. But can we achieve average a > n/2? Possibly not, because cumulative constraint prevents it.
But is there any other constraint that might allow average a > n/2 if S_k becomes negative? No, S_k must be nonnegative. So average a ≤ n/2 is a hard bound. So the maximum possible average of p_{a_k} is at most the supremum of p_i for i up to n, but we also have average a constraint. Since p_i is nondecreasing, to maximize average p, we want as many high i as possible, but limited by average a ≤ n/2. However, p_i might be nonlinear; maybe we could use mixture of high and low i to get a higher average p than p_{⌊n/2⌋}? Because p_i is increasing, but convex/concave? Not necessarily. We want to maximize average p subject to average a ≤ n/2 and each a_k integer 0..n and also the more detailed constraints about stock (the sequence must be feasible). However, the average constraint may be the only binding if we can realize any sequence of a_k with that average and satisfying the additional feasibility of the stock process? But not all sequences with average a ≤ n/2 are feasible; we also need that for each prefix, the sum of (n - 2a_i) is nonnegative (i.e., cumulative veterans ≤ (n/2)k). That is equivalent to the condition that the "stock" never goes negative. This is like a random walk reflecting. For infinite horizon, the set of achievable long-run average a values is [0, n/2] (including n/2) because we can construct sequences with any average ≤ n/2, I think. For average a less than n/2, we can just take constant a = some value < n/2? But constant a must also satisfy a ≤ S_k. Starting from S_1=0, if we choose constant a = c > 0, then S_2 = n - 2c. For c > 0, S_2 might be negative if c > n/2, but if c ≤ n/2, S_2 = n - 2c ≥ 0. Then a_2 must be ≤ S_2. If we set a_2 = c, we need c ≤ S_2 = n - 2c => 3c ≤ n => c ≤ n/3. So constant a is only feasible if c ≤ n/3? Let's check: we require a_k ≤ S_k for all k. For constant a = c, we have recurrence S_{k+1} = S_k + n - 2c. Starting from S_1 = 0. Then S_2 = n - 2c. Condition a_2 = c ≤ S_2 => c ≤ n - 2c => 3c ≤ n => c ≤ n/3. Then S_3 = S_2 + n - 2c = n - 2c + n - 2c = 2n - 4c. Need c ≤ S_3 => c ≤ 2n - 4c => 5c ≤ 2n => c ≤ (2/5)n. This is looser? Actually c ≤ 2n/5 = 0.4n, while n/3 ≈ 0.333n. So the binding constraint is early steps. For constant a, we need for all k: c ≤ S_k. The sequence S_k is arithmetic: S_k = (k-1)(n - 2c) (since S_1 = 0, S_2 = n-2c, S_3 = 2(n-2c), etc). So condition: c ≤ (k-1)(n-2c) for all k≥1? For k=1, S_1=0, condition a_1 ≤ S_1 gives c ≤ 0, so constant a > 0 fails immediately unless c=0. So constant a > 0 is not feasible because at first step we have no stock. So we cannot have a constant positive a from start; we need to start with a=0 to build stock. So any feasible sequence must have a_1=0 because S_1=0. So the first term is forced to be 0. But in long-run average, that single 0 becomes negligible, so average can still approach any value ≤ n/2 by appropriate patterns.
Thus, the set of achievable limit points of averages (liminf/limsup) is [0, n/2]. More precisely, for any target average a* ∈ [0, n/2], we can construct a sequence such that the long-run average of a_k is a*, and the sequence is feasible. For a* = n/2, we have the alternating 0,n pattern. For a* lower, we can interpose patterns with lower a. But need to ensure that the stock never goes negative.
Given that we aim to maximize average p, we need to choose frequencies for each possible a value (i) such that the average a ≤ n/2, and also the sequence can be realized with those frequencies while respecting the stock constraints (i.e., there exists a feasible infinite sequence with given limiting frequencies). This is similar to a constrained optimization over distributions.
But note: p_i are nondecreasing, so we want as high a as possible on average. However, average a cannot exceed n/2. So the maximum average p might be achieved by always using a as high as possible subject to average a = n/2? That is, we want to maximize average p given that the average of a is at most n/2. Since p_i is increasing, to maximize the average p for a fixed average a, we would want to allocate the a values to be as high as possible, but due to integer constraints and the fact that p_i is increasing, the optimal would put mass on two values: perhaps the largest possible a (n) and the smallest (0) to achieve average a exactly n/2. But is it always optimal to use only extremes? Consider p_i concave or convex. If p_i is concave, mixing extremes yields lower average p than using the mean? Actually if p is concave (increasing concave), then by Jensen's inequality, the average of p at extremes is less than or equal to p of the average (if p concave). Since we want to maximize average p, given a fixed average a, we would like to have p as high as possible. For a concave p, using the average directly (i.e., constant a = average) would give higher p than mixing extremes because p(mean) ≥ average of p(values). Conversely, if p is convex, mixing extremes yields higher average p than p(mean). Since p_i is nondecreasing, but we don't know its shape. The problem does not guarantee convexity or concavity. So we need to determine the maximum possible average p given that we can choose any feasible sequence. This is essentially a linear programming over the set of invariant measures of the Markov chain (deterministic dynamics) but with integer controls. However, we can think of it as a "resource" system where we can schedule veterans.
We can model it as a deterministic control problem: state S (nonnegative integer), action a (integer, 0 ≤ a ≤ min(S, n)), reward p_a, transition S' = S + n - 2a. We want to maximize the average reward over infinite horizon. This is a deterministic dynamic programming with countable state space. Because the state can be unbounded, but we suspect the optimal average reward is constant and can be achieved by a periodic policy (like the (0,n) pattern) or maybe something more complex.
Given the linear nature (S update linear in a) and reward depends only on a, this is reminiscent of a "bin packing" or "inventory" problem where the decision a is like consumption. The average reward per step is limited by the average a constraint. In such problems, the optimal average reward is the solution to a linear program: maximize E[p_a] subject to E[a] ≤ n/2, where the expectation is over the stationary distribution of the state-action pairs that satisfy the constraints. But we also have the additional constraint that the actions must be feasible given the state, which ties the sequence.
But perhaps the optimal average reward is simply the maximum of the convex envelope of the points (i, p_i) at the line a = n/2? Wait, consider we want to maximize average p subject to average a ≤ n/2. If we could achieve any distribution over a with average a exactly n/2, then the maximum average p would be the supremum of linear combinations of p_i with weights summing to 1 and weighted average i = n/2, i.e., the upper concave envelope of the points (i, p_i) evaluated at n/2. Because given the constraint on average a, to maximize average p, we solve: max_{distribution over i} Σ q_i p_i subject to Σ q_i i ≤ n/2, Σ q_i = 1, q_i ≥ 0. Since p_i is increasing, the optimal will have Σ q_i i = n/2 (can't be less because we could increase some i and get higher p_i without violating constraint). So it's maximizing a linear functional over a convex polytope. The maximum is at an extreme point of the convex hull of points (i, p_i). This is the upper convex envelope? Actually, we want to maximize Σ q_i p_i given fixed Σ q_i i. This is a linear programming: maximize c·q subject to A q = b. The optimal solution is at a vertex, which corresponds to putting weight on at most two points (since we have two equality constraints: sum q=1 and sum i q = n/2). So the optimal distribution is supported on at most two values of i (the endpoints of the interval containing n/2). Then the maximum average p is the convex combination of p at those two points that yields average i = n/2. But careful: Since p_i may not be convex, the upper envelope (the maximum achievable by mixing) is the concave envelope? Actually, consider we want to maximize p given a linear constraint on i. The set of achievable (i, p) pairs is the convex hull of the points (i, p_i). For a target i, the maximum p achievable by mixing is the upper hull (the convex envelope from above). That is the concave envelope? Let's define: For a set of points, the convex hull yields all possible (i, p) that can be expressed as convex combinations. For a fixed i, the maximum p among convex combinations with that i is the upper part of the convex hull (the "upper convex hull" which is concave). That is indeed the concave envelope of the function p(i) (if we consider p as a function of i defined only at integer points, the concave envelope at non-integer i is the linear interpolation of the points on the upper convex hull). So the maximum average p we could achieve if we could arbitrarily mix a values (i.e., use fractions of time on different a) is the value of the upper concave envelope at i = n/2.
But can we achieve any such mixture in the actual deterministic sequence subject to stock constraints? That is, can we realize a sequence with long-run frequencies q_i such that average a = Σ i q_i = n/2? We need to check if any distribution with average a = n/2 can be realized as a feasible infinite sequence (with integer a_k). Since we can only have integer a_k, the set of achievable long-run frequencies is the set of all probability distributions on {0,...,n} that satisfy Σ i q_i ≤ n/2? Actually, from the cumulative constraint, we know that average a cannot exceed n/2. But can we achieve any distribution with average a exactly n/2? For instance, if n=3, can we achieve a distribution that puts weight 0.5 on a=1 and 0.5 on a=2 (average 1.5)? This would be mixing 1 and 2. Is there a feasible sequence with those frequencies? Possibly yes, by constructing a cycle that yields these frequencies and respects stock constraints. For example, pattern: 1,2? Let's try n=3. Starting S1=0 => a1=0? Actually a1 must be 0 because S1=0. So first step forced 0. But in long-run, initial transient negligible. But for a cycle, we need to maintain stock nonnegative and return to same stock after cycle. Let's try to design a cycle that yields average a = 1.5, maybe (a=1, a=2) repeating. Check feasibility: Suppose we start with some stock S. Need to find S such that we can do a=1, then a=2, and return to S. Update: after a=1, S' = S + 3 - 2*1 = S+1. After a=2, S'' = S' + 3 - 4 = S' -1 = S. So cycle returns to S if we do (1,2) in order. However, we need S such that a=1 ≤ S and a=2 ≤ S' = S+1. Conditions: S ≥ 1 (for first action) and S+1 ≥ 2 => S ≥ 1. So S≥1 works. But can we reach a state S≥1 from initial S1=0? Yes, by doing some initial steps (like a=0) to build stock. So after building stock to S=1, we can repeat (1,2) indefinitely. The average a = (1+2)/2 = 1.5, which is n/2. And p average = (p1 + p2)/2. But is this better than (0,3) pattern? Possibly if p is not convex (maybe p1 and p2 are higher relative to p0 and p3). For example, if p0=0, p1=0.9, p2=0.9, p3=1, then (0,3) gives average (0+1)/2=0.5; (1,2) gives average 0.9, which is better. So indeed mixing non-extreme values can be optimal.
But can we achieve any convex combination of two integer a values with average n/2? Suppose we want weights q_i for i ∈ I. For a cycle of length L, we need to choose a sequence of actions such that the total change in S over the cycle is zero (so that S returns to its starting value) and the stock never goes negative. The total change over cycle: Σ (n - 2 a_k) = L n - 2 Σ a_k = 0 => Σ a_k = (n/2) L. So the average a over the cycle must be exactly n/2. That's necessary for a periodic cycle that returns to same S. Also, the cycle must be feasible: there exists some S0 such that for each step, the chosen a_k ≤ current stock S before that step, where S evolves accordingly. This is reminiscent of a "balanced sequence" condition.
Given we want to maximize average p, we can consider periodic policies. Because in deterministic infinite horizon average reward problems, there exists an optimal stationary policy that is periodic or has a bounded state (maybe). Since rewards are bounded, the optimal average can be achieved by a periodic policy (by standard results for deterministic systems with finite action space and integer state? Actually, we might need to consider that the state can become large, but the optimal may be a cycle). We can attempt to formulate as: we need to choose a sequence (a_1, a_2, ..., a_L) that is feasible from some stock S (which will be the state at start of cycle), such that after the cycle, stock returns to S (so that the cycle can repeat). The average reward is (1/L) Σ p_{a_k}. Then we can vary L and actions.
But also we could have a non-cyclic policy that yields higher average? However, by the theory of average reward for deterministic systems with finite action set and integer state, the optimal average reward is equal to the maximum over cycles of the average reward, because any infinite sequence yields a limit point that can be represented as a convex combination of cycles? I'm not entirely sure, but I think the problem reduces to finding the maximum over all feasible cycles (or "closed walks") of the average p, because the system is essentially a graph where states are possible stock levels (nonnegative integers), and from state S we can go to S' = S + n - 2a with a between 0 and min(S, n). This defines transitions. The average reward of an infinite path, if it converges, is the average reward over the recurrent class. For a strongly connected component (SCC), the optimal average reward is the maximum cycle mean in that SCC. Since the state space is infinite but maybe we can restrict to a finite set? Because if stock becomes too large, we could always waste by using a=0 to increase stock further, but that likely not optimal if p_i increasing. However, we need to consider all possible stock levels.
But note: the graph is such that from any state S, we can go to many states. Since n is up to 100, maybe the state can be bounded for optimal cycles? Typically, to maximize average reward, we would not want to accumulate excess stock because that would lower average a? Actually, having more stock allows using more veterans, but using more veterans depletes stock. If we hold too much stock, it means we have used few veterans in the past, which might lower average a. So the optimal cycles likely operate within some bounded range.
Observation: The transition: S' = S + n - 2a. Since a ≤ n, S' can be as low as S - n (if a=n) and as high as S + n (if a=0). So stock can change by at most n in one step.
We want to maximize average p, and p_i is increasing. To have high average p, we want a high on average, but we must keep average a = n/2 for a cycle (since otherwise stock drifts). For a cycle that repeats, we need net change zero, i.e., average a exactly n/2. For a non-cyclic infinite sequence, the average a might be less than n/2, and stock would go to infinity, but average p would be lower than possible because we are not using veterans enough. So the maximum average should be attained by a sequence with average a = n/2, because if average a < n/2, we could occasionally increase some a (using accumulated stock) to raise average a and thus average p, as p_i is increasing. But careful: increasing a uses stock, but if average a < n/2, stock is growing without bound, so we could replace some low a actions with higher a actions without violating constraints (since stock large), increasing average p. So the optimal average will have average a = n/2 exactly. Therefore we only need to consider sequences with average a = n/2.
Thus, we are to maximize average p over sequences with average a = n/2, and feasible (stock never negative). The average p is then (1/L) Σ p_{a_k} for a cycle with Σ a_k = (n/2) L.
Now, is it possible to achieve any convex combination (i.e., any distribution over a with mean n/2) via a feasible cycle? Not all distributions may be realizable due to the ordering constraints (stock feasibility). For a cycle, we need a sequence of a_k such that there exists an initial stock S0 that never drops below 0 and returns to S0 after L steps, with S_{t+1} = S_t + n - 2 a_t. This is equivalent to the condition that for all prefixes, cumulative veterans ≤ (n/2) * t plus some constant? More precisely, given S0, we require that for each t, S_t = S0 + Σ_{i=1}^{t-1} (n - 2 a_i) ≥ 0 (since stock before action t is S_t? Actually careful: At step t, before choosing a_t, the stock is S_t. Condition: a_t ≤ S_t. Also S_{t+1} = S_t + n - 2 a_t. So starting with S1 = initial stock before first action. For a cycle that repeats, we need S_{L+1} = S1. So we need to find S1 ≥ 0 and a sequence a_1..a_L such that for all t, a_t ≤ S_t, and S_{t+1} = S_t + n - 2 a_t.
This is reminiscent of the "Dyck path" constraints.
We can try to characterize feasible sequences for cycles with average a = n/2. Since Σ (n - 2a_i) = 0, the net change zero. Also, define D_t = Σ_{i=1}^{t} (n - 2 a_i) (partial sum). Then S_{t+1} = S1 + D_t. Condition: For each t, we need a_{t+1} ≤ S_{t+1} = S1 + D_t. But also need that a_{t+1} is at most n and nonnegative.
Our goal: maximize average p.
We can think of this as a deterministic control problem on the stock. Since p_i is increasing, we would like to have a_t as high as possible when we have enough stock. But we must maintain stock nonnegative. So it's like we have a "budget" S that increases when we choose low a, and decreases when we choose high a. Average a is fixed to n/2. So we can view it as we need to allocate a_t values such that the cumulative stock path never goes below zero (with appropriate S1). This is analogous to a random walk with step d_t = n - 2 a_t. Since average d_t = 0, the walk is mean zero. We need to avoid negative values (with some initial buffer S1). We can choose S1 as large as we want by initially building stock, but in a cycle we want S1 to be the same after cycle, so S1 must be the minimum stock encountered? Actually, in a cycle, we can start with some S1 >0, then the path may go up and down, but must return to S1. The condition is that for all t, S1 + D_t ≥ 0, and also a_{t+1} ≤ S1 + D_t. However, a_{t+1} is at most n, but also at most S1 + D_t. Since a_t can be up to n, to allow high a, we need S1 + D_{t-1} large.
Thus, the problem reduces to: find a cycle (a_1..a_L) with Σ (n - 2a_i)=0, and there exists S1 such that for all t, S1 + D_{t-1} ≥ a_t (since a_t ≤ S_t). Note D_0=0. Also S_t = S1 + D_{t-1}. And we also need S_t ≥ 0 obviously, but that's covered by a_t ≤ S_t because a_t≥0, so S_t ≥ 0 automatically if a_t ≤ S_t? Not necessarily: a_t ≤ S_t ensures S_t ≥ a_t ≥ 0, so S_t ≥ 0. So the only constraint besides sum zero is that there exists S1 ≥ 0 such that for all t, S1 + D_{t-1} ≥ a_t.
We can think of this as: given a sequence, define the required initial stock as the maximum over t of (a_t - D_{t-1}) (since we need S1 ≥ a_t - D_{t-1} for all t). Also need S1 to be such that after the cycle, S_{L+1}=S1 + D_L = S1 (since D_L=0), so S1 can be any value satisfying S1 ≥ max_t (a_t - D_{t-1}). Also, S1 must be integer? S1 is stock, nonnegative integer. But we can always take S1 as the ceiling of that max if needed. Since we can start with that stock, the cycle is feasible.
Thus, a sequence is feasible (repeatable) if Σ (n-2a_i)=0 and there exists a real (integer) S1 ≥ max_t (a_t - D_{t-1}). Since we can always increase S1 arbitrarily by adding a buildup phase, but then we need to return to same S1 after cycle. Actually, if we start with S1 larger than needed, after the cycle we would return to S1 only if D_L=0, which holds. So if we choose S1 larger than the required minimum, the conditions a_t ≤ S1 + D_{t-1} still hold because the RHS increases by the same amount. So we can pick S1 to be any integer >= that max. So the existence condition is simply that max_t (a_t - D_{t-1}) is finite (it is), and we can set S1 = ceil(max_t (a_t - D_{t-1})). But also S1 must be such that after the cycle, we return to S1 (which is satisfied if D_L=0). So the only necessary and sufficient condition for a sequence to be repeatable (i.e., there exists an initial stock such that the cycle can be repeated indefinitely) is Σ (n-2a_i)=0 and there exists some S1 such that for all t, S1 + D_{t-1} ≥ a_t. Equivalent to: the minimum over t of (S1 + D_{t-1} - a_t) ≥ 0. Since we can choose S1 arbitrarily large, the only potential issue is if a_t - D_{t-1} is unbounded below? But for a finite sequence, it's bounded. So we can always pick S1 large enough. However, we also need S1 to be achievable from initial state 0. But we can have a transient phase to reach that S1. Since we care about long-run average, we can start with a finite prefix that builds up the required stock, then repeat the cycle indefinitely. As long as the cycle yields the maximum average, the initial phase doesn't affect the limit. So we can ignore the requirement of starting from S1=0 by considering that we can first build up stock to S1 using some actions (like a=0 repeatedly) and then start the cycle. So any cycle with Σ (n-2a_i)=0 is feasible overall (asymptotically) because we can reach the required S1 by a finite prefix and then repeat the cycle. But wait: we need to ensure that during the building phase, we also satisfy constraints. We can simply take a=0 repeatedly to increase stock: each a=0 gives S' = S + n, so after m steps starting from 0, we can reach S = m*n. So we can make S1 arbitrarily large. So any cycle with net zero change can be made feasible by a sufficiently long build-up to a high enough stock level, and then we can repeat the cycle. However, we also need to ensure that during the cycle, the stock never goes negative given the chosen S1. As argued, we can set S1 high enough to satisfy a_t ≤ S1 + D_{t-1} for all t. Since D_{t-1} could be negative, we need S1 ≥ max_t (a_t - D_{t-1}). That's a finite number; we can make S1 that large by building up. So indeed any sequence with zero net change is feasible in the long run.
But careful: the condition a_t ≤ S_t must hold for all t. With S1 set to M = max_t (a_t - D_{t-1}) (plus maybe some margin), we have S_t = M + D_{t-1} ≥ a_t by construction. Also S_t will be nonnegative because a_t ≥ 0 ensures M + D_{t-1} ≥ a_t ≥ 0. So it's fine.
Thus, the problem reduces to: we can achieve any infinite sequence of actions with long-run average a = n/2, as long as there exists a periodic sequence (or more generally a sequence) with that average that is feasible in the sense above. But we argued any sequence with zero net change over the period is feasible after a transient. However, we also need the long-run average of p to be the limit. If we use a cycle, it's periodic and average is well-defined.
Therefore, the set of achievable average p values is essentially the convex hull of the points p_i with the constraint that the average i = n/2 (since we can mix integer a's in a cycle). But is it possible to realize any rational convex combination of a values with mean n/2? That is, given rational weights q_i (with denominator L), can we find a sequence of length L with those counts (so that average i = n/2) and net change zero? Net change zero is equivalent to average i = n/2, which is satisfied by the counts. However, we also need to order the actions in a sequence such that there exists an S1 making it feasible. Is any ordering possible? Possibly not; the ordering might impose additional constraints due to the stock levels. But perhaps we can always order the actions in a way that satisfies the stock constraints if we start with sufficiently large initial stock? For a given multiset of a values with total sum = (n/2)L, we need to arrange them in a sequence such that there exists S1 with S_t never dropping below a_t. This is like scheduling tasks with resource consumption: each step we have stock S_t, we consume a_t (veterans) and then add n - a_t? Actually we add n - 2a_t = (n - a_t) - a_t? Not exactly. But we can think of the stock evolution as: S_{t+1} = S_t - a_t + (n - a_t) = S_t + n - 2a_t. So it's like we have resource S_t, we use a_t, and then we receive new "rookies" n - a_t which become resource for future. So if we order the a's, we need to ensure that the resource never goes below zero. With a large enough initial stock, maybe we can always schedule them? Let's test.
Suppose we have a set of a values with average n/2. Consider the cumulative sum of (n - 2a_t) which is D_t. The condition for feasibility with initial stock M is: for all t, M + D_{t-1} ≥ a_t. Since we can choose M arbitrarily large, we can always satisfy this if we can make M large enough to cover the maximum deficit. But M must be finite. The required M is max_t (a_t - D_{t-1}). If we reorder the sequence, this maximum can change. So we need to determine if for any multiset of a's with sum = (n/2)L, there exists an ordering that makes max_t (a_t - D_{t-1}) not too large, but we can always increase M by building up stock. However, M must be an integer, but we can make it as large as we want by a long build-up. So as long as the required M is finite, we can realize the sequence. Since for any ordering, the required M is some finite number (because the sequence is finite). So for any ordering, we can achieve the cycle with a sufficiently large initial stock. Therefore, any multiset (hence any rational distribution) can be realized by first building up enough stock (using many a=0 steps) to reach a high S1, then execute the cycle in any order, then repeat. However, we must ensure that after the cycle, the stock returns to the initial S1 (since D_L=0), so we can repeat. That works regardless of ordering? Let's check: Suppose we start the cycle with initial stock S1. After the cycle, we return to S1. So if we pick S1 high enough to satisfy the constraints during the cycle, we can repeat indefinitely. That is fine. So the only requirement for the cycle is that net change is zero. The ordering does not affect the existence of a sufficiently large S1, because we can always increase S1. But is it always possible to find S1 such that for a given ordering, all constraints hold? We need to check: given a sequence a_1..a_L with Σ (n-2a_i)=0, define D_{t-1} = Σ_{i=1}^{t-1} (n-2a_i). Then we need S1 ≥ max_t (a_t - D_{t-1}). Since D_{t-1} can be negative large if we have many high a early, then a_t - D_{t-1} could be large. But it is still finite. So we can set S1 to that maximum. There's no upper bound on S1; we can make S1 as large as needed by building up before starting the cycle. So any ordering is feasible as long as we can reach that S1 from initial stock 0. We can reach any S1 by taking enough a=0 steps (each adds n). So yes, any ordering is feasible with a sufficiently long prelude.
Thus, for any rational distribution (i.e., frequencies of a values) with mean exactly n/2, we can construct a cycle that yields that distribution, by taking L as common denominator, and then arrange the actions in any order (say sorted) and build up enough stock before starting. So the set of achievable limit average p is exactly the set of convex combinations of p_i with mean i = n/2. In other words, the maximum achievable average p is the maximum over distributions q_i (probabilities on {0,...,n}) of Σ q_i p_i subject to Σ q_i i = n/2, Σ q_i = 1, q_i ≥ 0. Since p_i is nondecreasing, the constraint Σ i q_i = n/2 is binding (if < n/2 we could increase some q_i to increase average p). So we maximize Σ q_i p_i given Σ i q_i = n/2.
This is a linear programming problem in two dimensions (except n up to 100). The maximum is achieved at an extreme point of the feasible polytope, which involves at most two nonzero q_i (since we have two equations: sum q =1 and sum i q = n/2). So the optimum is achieved by mixing at most two distinct a values, say i and j (i ≤ n/2 ≤ j), with weights such that λ*i + (1-λ)*j = n/2, and λ ∈ [0,1]. Then average p = λ p_i + (1-λ) p_j. The maximum over all such pairs (i,j) gives the optimal value.
But is there any additional restriction that both i and j must be integers? Yes, because a_k are integer. So the mixture corresponds to a rational combination: we can take a cycle with some number of steps with a=i and some with a=j, such that the overall average is n/2. That's possible if (n/2) is between i and j, and we can find integers L, counts c_i, c_j such that (c_ii + c_jj)/L = n/2. Since n/2 might not be rational combination of two integers? But n/2 is rational (n is integer, so n/2 is either integer or half-integer). For n even, n/2 is integer; then we could just take all a = n/2, if that is allowed. But we need to check feasibility of constant a = n/2. As we saw, constant a = n/2 is not feasible from start, but we can do a cycle that returns to same stock? Let's test: if we try constant a = n/2, then each step change is n - 2*(n/2) = 0, so S stays constant. Starting from S1, we need a ≤ S1. So we need S1 ≥ n/2. And we also need S1 to be such that after each step S remains S1. That's fine. But can we reach S1 from 0? Yes, by building up with a=0 steps. However, after we start the cycle of constant a = n/2, S stays constant S1. So we can maintain it indefinitely. So constant a = n/2 is feasible if we have initial stock at least n/2. So for n even, we can achieve pure a = n/2, giving average p = p_{n/2}. That would be a candidate. But could mixing give a higher average? Possibly if p is not concave: e.g., p_i is convex, then mixing extremes gives higher than p at the mean. But is mixing extremes feasible? For n even, extremes are 0 and n. That yields average n/2, as we already have pattern (0,n). So both pure and mixed are feasible.
For n odd, n/2 is not integer, so we cannot have constant a = n/2 exactly. We must mix at least two integers to achieve average n/2. So the optimum will be a convex combination of two adjacent integers around n/2: floor(n/2) and ceil(n/2) (which are (n-1)/2 and (n+1)/2). But could mixing non-adjacent integers yield higher average p? Possibly if p is not monotonic? It is monotonic nondecreasing, so p_i increases with i. For a fixed mean, to maximize Σ q_i p_i, we want to put as much weight as possible on higher i, but the mean constraint limits that. The extreme points of the feasible region are exactly those with at most two nonzero q_i, and if the two i's are not adjacent, we could shift some weight to a higher i while adjusting weight on a lower i to keep mean constant? Let's analyze: Suppose we have weights on i and j with i < j, and mean = m. The average p is linear in the weight. For fixed m, if we replace i and j with i' and j' where i ≤ i' < j' ≤ j, can we get higher average? Not necessarily; the linear programming solution: The feasible region is a convex polytope. The objective is linear. The maximum occurs at a vertex. The vertices are where q_i are zero except for two that satisfy the equations. But not all pairs are vertices? Actually, with variables q_0,...,q_n, constraints: sum q_i = 1, sum i q_i = m, q_i ≥ 0. This is a transportation polytope. The vertices are basic feasible solutions with at most 2 nonzero variables (since rank of equality constraints is 2). However, some pairs may not be feasible if m is not between them? Actually, for any pair (i,j) with i ≠ j, we can solve λ i + (1-λ) j = m => λ = (j - m)/(j - i). For this to be between 0 and 1, we need i ≤ m ≤ j. So only pairs that bracket m are feasible. So the candidates are all pairs (i,j) with i ≤ m ≤ j. So the maximum over these pairs of the convex combination value. Typically, the maximum will be attained by the pair that gives the highest "upper convex envelope" at m. That is, the concave envelope (since we maximize linear combination). Actually, since p_i may not be concave, the maximum over convex combinations of points with given mean is the upper concave envelope. This is equivalent to the maximum of linear interpolations between points that are on the upper convex hull (the one that is convex? Wait: The set of points (i, p_i) in the plane. The convex hull of these points yields all achievable (mean, avg p) via mixing. For a given mean m, the maximum avg p is the highest y-coordinate on the vertical line x=m of the convex hull. That's the upper envelope of the convex hull, which is the concave envelope (since the convex hull is a convex polygon, its upper boundary is concave). So the optimal value is the value at m of the concave envelope of the discrete points (i, p_i). That's a piecewise-linear concave function connecting some of the points (the "upper convex hull" from the leftmost to the rightmost, but note: because m is fixed, we only care about the segment that contains m. So the optimum is either p_m if m is integer and p is concave? Actually, if m is integer, it could be that p_m is below the line connecting two surrounding points; then mixing gives higher.
Thus, the answer is the maximum over λ of λ p_i + (1-λ) p_j, where i and j are integers, i ≤ n/2 ≤ j, and λ = (j - n/2)/(j - i) (so that the average i is n/2). This is simply the linear interpolation between (i,p_i) and (j,p_j) at x = n/2.
But we must also consider if we can achieve any such mixture exactly as a cycle. As argued, yes, by taking appropriate counts. For rational λ, we can have a cycle of length L with appropriate numbers of i and j. Since n/2 might be a rational number with denominator 2, λ will be rational. So we can realize exactly the weighted average with finite cycle.
Hence the problem reduces to computing the concave envelope at x