DOJ logoDOJ
Problems
Recently added problemsSaved problems
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-backed certification.

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

Courier Lulu

Back to problem

Editorial

Process the bridges in decreasing order of box size.

Let dp[x]dp[x]dp[x] be the maximum number of boxes that can be collected when Lulu ends at island xxx using only bridges whose box sizes are larger than the current bridge's size.

Suppose the current bridge connects uuu and vvv and has box size ccc. If this bridge is used as the last bridge, every previously collected box must have size greater than ccc. Therefore, all valid previous states are already represented in dpdpdp.

There are two possible transitions.

  • Lulu is at island uuu and crosses this bridge to island vvv.
  • Lulu is at island vvv and crosses this bridge to island uuu.

Thus we update:

dp[v]←max⁡(dp[v],dp[u]+1),dp[u]←max⁡(dp[u],dp[v]+1).\begin{aligned} dp[v] &\leftarrow \max(dp[v], dp[u] + 1),\\ dp[u] &\leftarrow \max(dp[u], dp[v] + 1). \end{aligned}dp[v]dp[

However, the two updates must use the old values of dp[u]dp[u]dp[u] and dp[v]dp[v]dp[v], so that the same bridge is not used twice during one transition.

Since all box sizes are distinct, we do not need to group bridges with equal sizes.

Each bridge is processed once, and sorting takes O(Mlog⁡M)O(M \log M)O(MlogM) time. The total time complexity is O(Mlog⁡M)O(M \log M)O(MlogM), and the memory complexity is O(N+M)O(N+M)O(N+.

u
]
​
←max(dp[v],dp[u]+1),←max(dp[u],dp[v]+1).​
M
)