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

Dadas's Ramen Mukbang

Back to problem

Editorial

For a fixed KKK, define

f(K)=∑i=1Nmin⁡(Ai,K).f(K)=\sum_{i=1}^N \min(A_i,K).f(K)=i=1∑N​min(Ai​,K).

As KKK increases, every term min⁡(Ai,K)\min(A_i,K)min(Ai​,K) is nondecreasing, so f(K)f(K)f(K) is also nondecreasing. Therefore, we can binary-search for the smallest KK such that .

It is enough to use max⁡Ai\max A_imaxAi​ as the right endpoint of the search interval. Since the existence of a valid answer is guaranteed, the first KKK with f(K)≥Lf(K)\ge Lf(K)≥L must satisfy f.

For each candidate KKK, we can compute f(K)f(K)f(K) in O(N)O(N)O(N) by scanning the array once. Since Ai≤1013A_i\le10^{13}Ai​, the binary search uses at most about iterations. The total time complexity over all test cases is

O((∑N)log⁡1013),O\left(\left(\sum N\right)\log 10^{13}\right),O((∑N)log1013),

and the extra space complexity is O(N)O(N)O(N) per test case.

Also, ∑Ai≤2⋅1018\sum A_i\le 2\cdot10^{18}∑Ai​≤2⋅1018, so f(K)f(K)f(K) and every related sum fit in a signed 64-bit C++ .

Solution written by GPT5.6

K
f(K)≥Lf(K)\ge Lf(K)≥L
(K)=Lf(K)=L
f(K)=L
≤
1013
444444
long long