DOJ logoDOJ
Problems
Recently added problemsSaved problemsWorkbooks
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord account linking.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Santa Claus

Back to problem

Editorial

For selected positions j1<⋯<jmj_1<\cdots<j_mj1​<⋯<jm​, coverage is equivalent to j1≤K+1j_1\leq K+1j1​≤K+1, jm≥N−Kj_m\geq N-Kjm​≥N−K, and gaps at most 2K+12K+12K+1. Let dpidp_idpi​ be the minimum cost with last chimney iii covering houses 1 through iii. If i≤K+1i\leq K+1i≤K+1, dpi=Aidp_i=A_idpi​=Ai​; otherwise dpi=Ai+min⁡max⁡(1,i−2K−1)≤j<idpjdp_i=A_i+\min_{\max(1,i-2K-1)\leq j<i}dp_jdpi​=Ai​+min. Positive costs make earlier chimneys unnecessary in the first case. The answer is min⁡max⁡(1,N−K)≤i≤Ndpi\min_{\max(1,N-K)\leq i\leq N}dp_iminmax(1,N−K)≤i≤N​dpi​. Time is O(N2)O(N^2)O(N2) and space O(N)O(N)O(N), sufficient for N=5000N=5000N=5000. The small-subtask solution enumerates chimney subsets and combines coverage masks in O(N2N)O(N2^N)O(N2N). Costs and DP values are at most 5⋅10125\cdot10^{12}5⋅1012; use 64-bit.

Solution written by GPT6

max(1,i−2K−1)≤j<i
​
d
pj​