DOJ logoDOJ
Problems
Recently added problemsSaved problems
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

Pair of Sequences

Back to problem

Editorial

Consider each column kkk independently.

For two sequences Pi,PjP_i,P_jPi​,Pj​, the value f(Pi,Pj)f(P_i,P_j)f(Pi​,Pj​) is the maximum of Pi,k+Pj,kP_{i,k}+P_{j,k}Pi,k​+Pj,k​ over all columns kkk. For a fixed column kkk, the largest possible sum is obtained by choosing the two largest values in that column from two distinct sequences.

While reading the input, maintain the largest and second largest values for each column, together with the indices of the sequences where they appear.

After reading the input, check every column kkk. The pair corresponding to the largest sum of the two largest values in a column is an optimal answer.

For correctness, fix any pair Pi,PjP_i,P_jPi​,Pj​ and any column kkk. The value Pi,k+Pj,kP_{i,k}+P_{j,k} cannot exceed the sum of the two largest values in column . Hence no pair can achieve a value larger than the best candidate we check. Conversely, the two sequences chosen from the best column actually achieve that candidate value, so the answer is optimal.

The time complexity is O(NM)O(NM)O(NM), and the memory complexity is O(N)O(N)O(N).

Pi,k​+
Pj,k​
kkk