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

旨いステーキ(Delicious Steak)

Back to problem

Editorial

Suppose that every steak initially receives vinegar, giving a baseline score B=∑i=13NViB=\sum_{i=1}^{3N}V_iB=∑i=13N​Vi​. Assigning A1 sauce to steak iii instead gains Ai−ViA_i-V_iAi​−Vi​, while assigning salt gains Si−ViS_i-V_iSi​−Vi​.

Sort the steaks by decreasing Ai−SiA_i-S_iAi​−Si​. If an earlier steak receives salt while a later steak receives A1 sauce, swapping their sauces cannot decrease the total score. Thus there exists an optimal assignment in which every steak receiving A1 sauce occurs before every steak receiving salt.

Let kkk be a split between these groups. Necessarily N≤k≤2NN\le k\le 2NN≤k≤2N. Let PkP_kPk​ be the sum of the largest gains among the first steaks, and let be the sum of the largest gains among the last steaks. The answer is

B+max⁡N≤k≤2N(Pk+Qk).B+\max_{N\le k\le 2N}(P_k+Q_k).B+N≤k≤2Nmax​(Pk​+

Compute all prefix values PkP_kPk​ and suffix values QkQ_kQk​ using min-heaps of size NNN, then check all possible split positions. This takes O( time and space. The magnitude of any answer is at most , so signed 64-bit integers suffice.

Solution written by GPT6

NN
N
Ai−ViA_i-V_iAi​−Vi​
kkk
QkQ_kQk​
NNN
Si−ViS_i-V_iSi​−Vi​
3N−k3N-k3N−k
Qk​).
Nlog⁡N)O(N\log N)
O(NlogN)
O(N)O(N)O(N)
3⋅10143\cdot10^{14}3⋅1014