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

Farewell

Back to problem

Editorial

Suppose the element initially at position iii is the one that remains. Exactly i−1i-1i−1 elements to its left and N−iN-iN−i elements to its right are deleted. Its final value is therefore

Ai−(i−1)+(N−i)=Ai+N+1−2i,A_i-(i-1)+(N-i)=A_i+N+1-2i,Ai​−(i−1)+(N−i)=Ai​+

regardless of the deletion order.

Evaluate this expression at every position iii and take the maximum. Each expression can be evaluated as its element is read, keeping only the largest value seen so far. The time complexity for one case is O(N)O(N)O(N) and the auxiliary space complexity is O(1)O(1)O(1).

Proof

While one element is kept until the end, its relative order with respect to every other element never changes. Deleting an element that was initially to its left decreases its value by 111, and deleting an element that was initially to its right increases its value by 111. Every other element is deleted exactly once, so the element initially at position iii always finishes with value Ai+N+1−2iA_i+N+1-2iAi​.

For subtask 111, exhaustive search can try every choice of an element to delete from the current sequence and examine all possible final values. Modify the sequence in place, and restore it after each recursive call. This takes O(N!)O(N!)O(N!) time and O(N)O(N)O(N) space for the sequence and recursion stack.

For subtask 222, choose each candidate for the final element in turn and add the contribution of deleting every other element individually. This takes O(N time and space to store the sequence.

N
+
1−
2i,
+
N+
1−
2i

For every deletion order, the final element came from some position in the initial sequence. Its final value therefore cannot exceed the maximum of these expressions. Conversely, choose a position attaining that maximum and delete all other elements. This achieves that value, so the maximum of the expressions is exactly the answer.

2)O(N^2)
O(N2)
O(N)O(N)O(N)

For subtask 333, the condition Ai≥Ai+1A_i \geq A_{i+1}Ai​≥Ai+1​ means that Ai+N+1−2iA_i+N+1-2iAi​+N+1−2i decreases by at least 222 when moving one position to the right. Keeping the first element is therefore optimal, and the answer is A1+N−1A_1+N-1A1​+N−1. Including the time to read the input, this takes O(N)O(N)O(N) time and O(1)O(1)O(1) auxiliary space.

Solution written by GPT6