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

tourist vs rainboy

Back to problem

Editorial

For tourist, the time AiA_iAi​ spent on problem iii is included in the completion times of problems i,i+1,⋯ ,Ni,i+1,\cdots,Ni,i+1,⋯,N. Therefore, tourist's penalty is

∑i=1N(N−i+1)Ai.\sum_{i=1}^{N}(N-i+1)A_i.i=1∑N​(N−i+1)A

For rainboy, AiA_iAi​ is included in the completion times of problems 1,2,⋯ ,i1,2,\cdots,i1,2,⋯,i, so rainboy's penalty is

∑i=1NiAi.\sum_{i=1}^{N}iA_i.i=1∑N​iAi​.

Thus, the required difference is

∑i=1N(2i−N−1)Ai.\sum_{i=1}^{N}(2i-N-1)A_i.i=1∑N​(2i−N−1)A

It can be computed in one pass over the array. The time complexity is O(N)O(N)O(N).

Solution written by GPT5.6

i
​
.
i
​
.