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

New City Planning

Back to problem

Editorial

The height of each building can be chosen independently. Thus, the answer is the sum of max⁡(B1,…,BAi)\max(B_1,\ldots,B_{A_i})max(B1​,…,BAi​​) over all buildings iii.

For h=2,3,…,Mh=2,3,\ldots,Mh=2,3,…,M, update Bh←max⁡(Bh,Bh−1)B_h \gets \max(B_h,B_{h-1})Bh​← in this order. Then stores the maximum of the first elements of the original array. The answer is .

The time complexity is O(N+M)O(N+M)O(N+M) and the space complexity is O(N+M)O(N+M)O(N+M).

max
(
Bh​
,
Bh−1​
)
BhB_hBh​
hhh
∑i=1NBAi\sum_{i=1}^{N}B_{A_i}∑i=1N​BAi​​