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

Nostalgia

Back to problem

Editorial

If an arrangement realizes X=MX=MX=M, transposing it realizes X=−MX=-MX=−M. It is therefore enough to construct nonnegative XXX.

The column-maximum sum is maximized by making N2−N+1,…,N2N^2-N+1,\ldots,N^2N2−N+1,…,N2 the maxima of the NNN columns. If the row maxima in increasing order are M1<⋯<MNM_1<\cdots<M_NM1​<⋯<MN​, the first iii rows contain NiNiNi values, so necessarily Mi≥NiM_i\geq NiMi​≥Ni. This yields the exact absolute bound

L=N(N−1)22.L=\frac{N(N-1)^2}{2}.L=2N(N−1)2​.

The row-major arrangement attains X=LX=LX=L.

For 0≤X≤(N−1)20\leq X\leq(N-1)^20≤X≤(N−1)2, start from an X=0X=0X=0 arrangement with on the diagonal. Lower only the first-row maximum to and move the displaced large value elsewhere in the first column. This changes by exactly .

For larger XXX, start from the maximum row-major construction. Keep the last row fixed so the column maxima stay fixed, and increase the maxima of the first N−1N-1N−1 rows from the bottom upward until their sum has increased by L−XL-XL−X. The target maxima satisfy Mi≥NiM_i\geq NiM and remain strictly increasing, allowing the remaining smaller values to be distributed among the rows.

For negative XXX, transpose the constructed matrix. The construction takes O(N2)O(N^2)O(N2) time. Solution written by GPT5.6

N2−N+1,…,N2N^2-N+1,\ldots,N^2
N2−N+1,…,N2
N2−N+1−XN^2-N+1-XN2−N+1−X
S1−S2S_1-S_2S1​−S2​
XXX
i
​
≥
Ni