Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Editorial: G0. Roadworks (all roads ready)

What G0 asks

Houses $1, \ldots, n$ lie in a row; every adjacent pair is connected from day $1$. You start at house $x$. Each day you may move to a neighbor or stay, then add $h_j$ for your current house $j$. Maximize total satisfaction after $k$ days.

This is G. Roadworks with every road-opening day $d_i = 1$.


Step 1: Why a simple day DP is not enough

Let $\mathrm{dp}[s][i]$ be the best total after day $s$ while at house $i$:

$$ \mathrm{dp}[s][i] = h_i + \max\bigl(\mathrm{dp}[s-1][i],\ \mathrm{dp}[s-1][i-1],\ \mathrm{dp}[s-1][i+1]\bigr). $$

That is $O(nk)$ — impossible for $k \le 10^9$.

We need to describe an optimal plan without iterating every day.


Step 2: Shape of an optimal walk

Two structural facts (you can justify by exchange arguments on small counterexamples):

  1. Monotone movement. You never go left and later go right (or the reverse). An optimal plan only moves left, only moves right, or never leaves $x$.

  2. Single camp. You never spend two or more consecutive days at a house and then walk farther away. All multi-day “farming” happens at one final house $c$.

So an optimal strategy is:

  1. Walk monotonically from $x$ toward some house $c$ (at most one day’s collection per house along the way).
  2. Camp at $c$ for every remaining day.

The entire problem reduces to choosing the camp house $c$.


Step 3: Earliest collection day $T[i]$

Let $T[i]$ be the earliest day $s$ such that you can collect $h_i$ on day $s$ (after the move on that day).

With all roads open on day $1$, using 0-indexed start $x$:

Rule Meaning
$T[x] = 1$ You collect at the start on day $1$.
$T[x \pm 1] = 1$ Neighbors are reachable on day $1$.
$T[i] = T[i+1] + 1$ for $i \le x - 2$ Each step farther left costs one more day.
$T[i] = T[i-1] + 1$ for $i \ge x + 2$ Each step farther right costs one more day.

Example ($n = 5$, 1-based start $3$, index $x = 2$): $T = [2, 1, 1, 1, 2]$.

Pitfall: $T[i] = 1 + |i - x|$ is wrong (it gives $T[x \pm 1] = 2$). Neighbors must be $1$.

If $T[c] > k$, house $c$ is unreachable — skip it.


Step 4: Profit when you camp at $c$

Split earnings into two parts.

Walking from $x$ to $c$

You move monotonically along the segment between $x$ and $c$. Each interior house on that segment contributes its $h$ once (the day you step through). The endpoints $x$ and $c$ are not included in this middle sum:

  • $x$ is handled by the start (if $c = x$, there is no walk at all).
  • $c$ is counted in the camping term below.

With prefix sums $\mathrm{pref}$ on $h$ (0-indexed, $\mathrm{pref}[i] = \sum_{t=0}^{i} h_t$):

$$ \text{walk}(x, c) = \begin{cases} 0 & |x - c| \le 1, \\ \mathrm{pref}[\max(x,c) - 1] - \mathrm{pref}[\min(x,c)] & \text{otherwise}. \end{cases} $$

Camping at $c$

You first collect at $c$ on day $T[c]$, then can stay through day $k$. That is $k - T[c] + 1$ collections at $h_c$:

$$ \text{camp}(c) = (k - T[c] + 1) \cdot h_c. $$

Total for a fixed $c$

$$ \text{score}(c) = \text{camp}(c) + \text{walk}(x, c). $$

Answer: $\max\limits_{c \,:\, T[c] \le k} \text{score}(c)$.


Step 5: Walkthrough (first sample)

Input: $n = 5$, $k = 10$, $x = 3$ (1-based), $h = [14, 2, 3, 5, 6]$.

0-indexed $x = 2$, $T = [2, 1, 1, 1, 2]$.

$c$ $T[c]$ Camp Walk (strictly between) Total
0 2 $14 \times 9 = 126$ $h_2 = 2$ (only index $1$ between) 128
1 1 $2 \times 10 = 20$ 20
2 1 $3 \times 10 = 30$ 30
3 1 $5 \times 10 = 50$ 50
4 2 $6 \times 9 = 54$ $h_3 = 5$ 59

Best: camp at house $1$ (index $0$), total 128.

Interpretation: day $1$ move toward house $1$ and collect along the way; from day $2$ onward live at house $1$.


Step 6: Algorithm and complexity

  1. Build $T$ in $O(n)$.
  2. Build prefix sums in $O(n)$.
  3. For each $c = 0, \ldots, n - 1$ with $T[c] \le k$, compute $\text{score}(c)$ in $O(1)$.

Time: $O(n)$ per test case. Memory: $O(n)$.

The reference code in model_sol.cpp follows exactly this camp formulation.


Bridge to the full problem G

G0 is special because every road exists on day $1$, so a single monotone walk plus one camp is always optimal.

In the full problem, road $i$ opens on day $d_i$. Then:

  • $T[i]$ is computed from the $d_i$ schedule (two sweeps from $x$, same neighbor-day-$1$ exceptions when $d_i = 1$).
  • You may need to collect at a lower peak before a higher peak becomes reachable later. That is no longer captured by one camp house.

The full solution keeps the camp formula as one way to finish, and adds a small DP over hops to strictly higher houses (see G editorial). For G0 alone, the camp formula is enough.


Common pitfalls

  1. Using $1 + |i - x|$ instead of the neighbor rule for $T$.
  2. Including endpoint houses in sum_between (double-counts $h_c$ or $h_x$).
  3. Using $k - \mathrm{dist}$ instead of $k - T[c] + 1$ for camping days.
  4. Forgetting long long when $k$ and $h$ are large.