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

Manipulating the Balance Sheet

Back to problem

Editorial

The balance sheet must satisfy

assets=liabilities+equity.\text{assets}=\text{liabilities}+\text{equity}.assets=liabilities+equity.

Let AAA be the sum of undamaged asset amounts, and BBB the sum of undamaged liability and equity amounts. Let cAc_AcA​ and cRc_RcR​ count damaged accounts on the asset side and on the other side, respectively.

  • If cA>0c_A>0cA​>0 and cR>0c_R>0cR​>0, unknown amounts appear on both sides, so the restoration cannot be uniquely determined.
  • If both liability and equity accounts are damaged, the type of the consolidated account is not unique; restoration is impossible.
  • If , the original accounts are valid exactly when . In that case print them unchanged.

If the computed amount is negative, restoration is impossible. Otherwise, print every undamaged account in input order, followed by the consolidated OTHER account with that type and amount. Print it even if the amount is zero.

Each input amount is at most 10910^9109, and there are at most 100 000100\,000100000 accounts, so the absolute value of every sum and recovered amount is at most 101410^{14}1014. Use a signed 64-bit integer.

Reading and printing the accounts takes O(N)O(N)O(N) time and O(N)O(N)O(N) space.

cA=cR=0c_A=c_R=0cA​=cR​=0
A=BA=BA=B
  • If cA>0c_A>0cA​>0 and cR=0c_R=0cR​=0, the required amount of the new asset account is B−AB-AB−A.
  • If cA=0c_A=0cA​=0 and cR>0c_R>0cR​>0, the required amount of the new liability or equity account is A−BA-BA−B, with the corresponding damaged account type.