Spent way too long convincing myself this was a greedy problem before actually verifying it.
Model the problem as a matching or greedy assignment where each guard can either stay or move left if the left city is empty. Use a greedy strategy from left to right, prioritizing moving guards to cities with higher populations when beneficial, or use dynamic programming to consider all possibilities. Prove that the greedy choice is optimal by exchange argument.
Pro tip: Clarify that guards can only move left and only into unoccupied cities, and that moves are sequential but order doesn't affect the final set of occupied cities. Emphasize that the problem reduces to selecting a set of cities to protect such that each selected city either originally had a guard or has a guard that can move into it from the right, with no two guards competing for the same city.
Restate the problem: n cities in a row, each with population, some have guards. Each guard can move at most one step left into an empty city. Maximize sum of populations of cities with guards after moves.
For each guard, decide whether to stay or move left. Moving left is only possible if the left city is empty and not occupied by another guard. This creates dependencies between adjacent cities.
Consider dynamic programming with states representing whether the current city is occupied and whether a guard from the right can move in. Alternatively, model as a maximum weight matching in a path graph where each guard can match to itself or its left neighbor.
For a greedy approach: scan from left to right, and for each city, if it has a guard and the left city is empty and has higher population, move the guard left. For DP: define dp[i][0/1] as max protected population up to city i, with 0/1 indicating if city i is occupied. Transition based on guard presence and movement.
The DP solution runs in O(n) time and O(1) or O(n) space. Prove correctness by induction or exchange argument, showing that the optimal solution can be transformed into the greedy/DP choice without decreasing the total population.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.