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-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

Careful Deletion

Back to problem

Editorial

Cost for a fixed last position

Let kkk be the original position of the last deleted element. Every element to its left must be deleted from the front, and every element to its right must be deleted from the back. If position kkk can remain last, its cost is therefore

C(k)=∑i<kLPi+∑i>kRPi+min⁡(LPk,RPk).C(k)=\sum_{i<k}L_{P_i}+\sum_{i>k}R_{P_i}+\min(L_{P_k},R_{P_k}).C(k)=i<k∑​LPi​​+

Exchanging deletions

Let xxx be an endpoint that can currently be deleted. Until deleted, it remains at that endpoint and remains an extreme value. In any complete deletion order, move the deletion of xxx to the beginning. All other deletions remain legal: removing an element cannot destroy another element's extreme-value property, and deletions from the opposite end still work. The direction used for the final element may change, so use this argument only for feasibility.

Consequently, deleting any currently eligible endpoint preserves the existence of a complete deletion order. If a desired last position differs from the position of xxx, deleting xxx first also preserves that desired last position.

Feasible last positions form an interval

Induct on the remaining length. Length one is immediate. If neither end is eligible, deletion is impossible. If only one end is eligible, deleting it is forced.

If both ends are eligible, consider the two states obtained by deleting either end first. At length two, both positions are feasible. At length at least three, if complete deletion is possible, the exchange property lets us delete both endpoints first. The resulting nonempty middle sequence is also deletable. Its feasible last positions belong to both child states. By induction, the feasible sets of those children are intervals, and they overlap, so their union is an interval as well.

Finding its boundaries

Simulate deletions, preferring the left endpoint whenever it is eligible. This choice preserves every feasible last position strictly to its right. If the left endpoint is ineligible, deleting the right endpoint is forced. Thus this simulation leaves the rightmost feasible last position. Preferring the right endpoint instead leaves the leftmost feasible last position.

If a simulation gets stuck with more than one element, output -1. Otherwise, minimize C(k)C(k)C(k) over the interval between the two resulting positions.

The remaining positions form an interval, and the remaining values form a consecutive integer interval. Maintain both pairs of boundaries to process each deletion in O(1)O(1)O(1). Maintain prefix and suffix cost sums to evaluate candidates. The total complexity is O(N)O(N)O(N) time and O(N)O(N)O(N) space.

Every cost sum is at most N⋅109≤2⋅1014N\cdot10^9\leq2\cdot10^{14}N⋅109≤2⋅1014, so use 64-bit integers.

The supplied naive solution uses interval DP, considering either eligible endpoint as the first deletion. Processing by interval length and retaining only the previous length gives O(N2)O(N^2)O(N2) time and O(N)O(N)O(N) space.

Solution written by GPT

i>k∑​
RPi​​
+
min(LPk​​,RPk​​).