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

Oranges!

Back to problem

Editorial

Subtask 1: enumerate every subset of oranges bought by the first person. For each subset of size KKK, directly compute the satisfaction and take the maximum. This takes O(N2N)O(N2^N)O(N2N) time and O(N)O(N)O(N) space.

Full solution: if the second person bought everything, the satisfaction would be ∑i=1Nbi\sum_{i=1}^N b_i∑i=1N​bi​. Giving orange iii to the first person changes it by .

Let ci=ai−bic_i=a_i-b_ici​=ai​−bi​. We must choose exactly differences with maximum sum. Sort them in descending order and select the largest . If an unselected value were larger than a selected value, exchanging them would increase the sum. Thus this choice is optimal. Ties may be resolved arbitrarily.

Writing the sorted values as c1≥c2≥⋯≥cNc_1\geq c_2\geq\cdots\geq c_Nc1​≥c2​≥⋯≥c, the answer is

∑i=1Nbi+∑i=1Kci.\sum_{i=1}^N b_i+\sum_{i=1}^K c_i.i=1∑N​bi​+

Exactly KKK oranges must be selected, even when some selected differences are negative.

Each case takes O(Nlog⁡N)O(N\log N)O(NlogN) time and O(N)O(N)O(N) space. The magnitude of the answer and intermediate sums is at most 2×10142\times10^{14}2×10, and each difference has magnitude at most . Store sums in 64-bit integers.

Solution written by GPT

ai−bia_i-b_i
ai​−bi​
KK
K
KKK
N
​
i=1
∑
K
​
ci​
.
14
109−110^9-1109−1