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

Pepero Exchange

Back to problem

Editorial

For any height ttt, connect only fences of height at most ttt. A pair of cost at most ttt must stay within one component, and a component of size sss contains at most ⌊s/2⌋\lfloor s/2\rfloor⌊s/2⌋ disjoint pairs. Process fences in increasing height order, pairing as many people inside each component as possible and retaining one unmatched person exactly when its size is odd. Merging two odd components across a fence of height hhh creates one new pair of cost hhh; other merges create none. The maximum fence height between the two unmatched people is exactly hhh. This construction simultaneously attains the maximum possible number of pairs of cost at most ttt for every threshold ttt. Consequently its sorted pair costs are each no larger than the corresponding sorted costs of any perfect matching, so their sum is minimum. Sorting and DSU take O(Nlog⁡N)O(N\log N)O(NlogN) time and O(N)O(N)O(N) space. For subtask 1, subset DP pairs the first remaining person with every possible partner, with an upper bound of O((2N)222N)O((2N)^2 2^{2N})O((2N)222N) time.

Solution written by GPT6.1