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

Crop Harvest

Back to problem

Editorial

Sort the seeds by increasing coordinate. Suppose the seed at sorted position jjj was originally seed pjp_jpj​. Thus, pjp_jpj​ is also the only day on which that seed can be harvested.

Consider only the selected seeds, in coordinate order. Their day indices must strictly decrease and then strictly increase, with at most one turning point.

Let the selected seed harvested earliest be the pivot. For selected seeds to the left of the pivot, the day indices must strictly decrease as coordinates increase. Otherwise, when moving to a farther-left seed that must be harvested earlier, Dadas would cross another selected seed that is still unharvested.

For the same reason, the day indices of selected seeds to the right of the pivot must strictly increase as coordinates increase.

Conversely, every selected set whose day indices strictly decrease and then strictly increase can be harvested. Start at the pivot. In chronological order, each next selected seed lies immediately outside the already harvested range on either the left or the right, so the path never crosses another selected seed that is still unharvested.

Therefore, the problem becomes a maximum-weight valley-shaped bitonic subsequence problem on

p1,p2,⋯ ,pN,p_1,p_2,\cdots,p_N,p1​,p2​,⋯,pN​

where the weight of position jjj is wpjw_{p_j}wpj​​.

Let LjL_jLj​ be the maximum total value of a strictly decreasing subsequence ending at position jjj. Then

Lj=wpj+max⁡k<j, pk>pjLk.L_j=w_{p_j}+\max_{k<j,\ p_k>p_j}L_k.Lj​=wpj​

Let RjR_jRj​ be the maximum total value of a strictly increasing subsequence starting at position jjj. Then

Rj=wpj+max⁡k>j, pk>pjRk.R_j=w_{p_j}+\max_{k>j,\ p_k>p_j}R_k.Rj​=wpj​

Both transitions need the maximum DP value among day indices greater than pjp_jpj​. Since the day indices are exactly the distinct integers from 111 to NNN, a segment tree indexed by day supports the required range maximum queries.

Compute LjL_jLj​ from left to right. Query the maximum on (pj,N](p_j,N](pj​,N], add wpjw_{p_j}, and update position .

Compute RjR_jRj​ from right to left in the same way.

If position jjj is the pivot, the best value is

Lj+Rj−wpj,L_j+R_j-w_{p_j},Lj​+Rj​−w

because the pivot weight was counted twice.

The maximum of this expression over all jjj is the answer. Sorting and segment tree operations give O(Nlog⁡N)O(N\log N)O(NlogN) time and O(N)O(N)O(N) memory.

All weights are positive, so the answer is at most the sum of all weights. Hence,

answer≤N⋅109≤2⋅1014,\text{answer}\leq N\cdot10^9\leq2\cdot10^{14},answer≤N⋅109≤2⋅1014,

which always fits in a signed 64-bit integer.

Solution written by GPT5.6

,
​
+
k<j, pk​>pj​max​Lk​.
​
+
k>j, pk​>pj​max​Rk​.
wpj​​
pjp_jpj​
p
j​
​
,