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

Fertilizer Delivery

Back to problem

Editorial

ttt batches yield 2t2t2t containers of ammonia, so the minimum number of batches is t=⌈K/2⌉t=\lceil K/2\rceilt=⌈K/2⌉. Producing more batches requires more raw material and cannot reduce the cost. It suffices to buy ttt containers of nitrogen and 3t3t3t containers of hydrogen, optimizing each purchase separately.

For either material, buy from the cheapest suppliers first. If an expensive supplier sells a container while a cheaper supplier still has unused stock, buy that container from the cheaper supplier instead. This keeps the quantity unchanged and reduces the cost. The order of suppliers with equal prices does not affect the cost. Sort the suppliers by price and buy the smaller of their stock and the remaining demand from each supplier. If the available stock of either material falls short, print -1.

With M=kn+khM=k_n+k_hM=kn​+kh​, the time complexity is O(Mlog⁡M)O(M\log M)O and space complexity is . We buy containers, each costing at most , so the maximum feasible answer is .

Solution written by GPT

(
M
log
M
)
O(M)O(M)O(M)
4t≤2⋅1094t\leq 2\cdot 10^94t≤2⋅109
10610^6106
2⋅10152\cdot 10^{15}2⋅1015