# An Upgrade Plan for Battles: Solution **Problem setter’s solution: quotient-change events and a monotone defence pointer. GPT-6 adds the convexity, tie-breaking, and integer-safety proofs, a standard C++17 implementation, and a heap-scheduled alternative.** ## 1. Fix attack and defence With $x,y$ attack and defence upgrades, $$ A=A_0+xV_A,\qquad D=D_0+yV_D. $$ We need $A>d_i$ . One enemy of type $i$ takes $$ t_i(x)=\left\lceil\frac{h_i}{A-d_i}\right\rceil $$ rounds. Simultaneous attacks mean that the enemy also deals damage in its final round. Total damage is $$ F_x(y)=\sum_i k_it_i(x)\max(a_i-D_0-yV_D,0). $$ All damage is nonnegative and there is no healing, so enemy order does not affect survival. The least upgraded health is $$ H=\max(H_0,F_x(y)+1), $$ and cost is $xC_A+yC_D+(H-H_0)C_H$ . It suffices to consider attack counts $$ L=\max\left(0,\max_i\left\lceil\frac{d_i+1-A_0}{V_A}\right\rceil\right), \quad R=\max\left(0,\max_i\left\lceil\frac{d_i+h_i-A_0}{V_A}\right\rceil\right). $$ Below $L$ , some enemy cannot be defeated. Above $R$ , every enemy already takes one round. Defence counts need not exceed $$ Y=\max\left(0,\max_i\left\lceil\frac{a_i-D_0}{V_D}\right\rceil\right), $$ which prevents all damage. Even harmless enemies must contribute to the required attack lower bound. ## 2. Convexity and a monotone defence pointer For fixed $x$ , $F_x(y)$ is a sum of nonnegative piecewise-linear functions, hence discretely convex and nonincreasing in integer $y$ . Required extra health $\max(F_x(y)+1-H_0,0)$ remains convex. Adding the linear defence price preserves convexity. Start at $Y$ and decrease defence only when doing so **strictly** reduces cost. This stops at the largest defence count attaining minimum cost. If adjacent defence counts have equal cost, the larger defence count requires less health: its positive defence price must otherwise be compensated by extra health. Keeping the larger defence count therefore respects the health-first tie rule. As attack increases, every enemy’s round count decreases or stays fixed. For adjacent defence counts $y-1,y$ , both their damage difference and the damage at the lower defence are nonincreasing. Their extra-health difference equals $$ \min\left(F_x(y-1)-F_x(y),\ \max(F_x(y-1)+1-H_0,0)\right). $$ It too is nonincreasing. Thus the benefit of more defence never increases with attack, and the largest optimal defence count never moves right. One pointer makes at most $Y$ left moves throughout the attack sweep. ## 3. Events when round counts change The expression $\lceil h/d\rceil=\lfloor(h-1)/d\rfloor+1$ has only $O(\sqrt h)$ distinct values. If the current round count is $t>1$ , the smallest damage causing a decrease is $$ d'=\left\lceil\frac{h}{t-1}\right\rceil. $$ The next attack-upgrade count is therefore $$ x'=\left\lceil\frac{d_i+d'-A_0}{V_A}\right\rceil. $$ It is strictly larger than the current count and may skip several quotient values. Repeating this process lists all changes for an enemy. The setter’s method stores events in linked lists indexed by attack-upgrade count and scans them in order. Between events, the damage function is unchanged while attack cost increases, so evaluate only $L$ and event positions. The reference program sizes the index array from $R$ . The total event count is $E=O(\sum_i\sqrt{h_i})$ . ## 4. Defence buckets For an enemy that can cause damage, write $$ a_i-D_0=p_iV_D+r_i,\qquad 0\le r_i