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

Laboratory

Back to problem

Editorial

First, sort all type numbers lexicographically by (Bi,i)(B_i,i)(Bi​,i) to obtain a permutation p1,p2,…,pNp_1,p_2,\ldots,p_Np1​,p2​,…,pN​. The growth-coefficient order of any selected sample is a subsequence of this permutation.

Fix a day ttt, and place the pair Xj(t)=(Apj+Bpjt,pj)X_j(t)=(A_{p_j}+B_{p_j}t,p_j)Xj​(t)=(Ap at each position of the permutation. Compare pairs by their first components, breaking ties by their second components. The selected types have the same population order exactly when their pairs are strictly increasing under this comparison. Thus, the maximum number of types that can be selected on day is the length of the longest increasing subsequence of these pairs. Comparing only populations would miss the rule for tied populations, so type numbers must also be included.

Compute the longest increasing subsequence by maintaining the smallest possible final pair for every subsequence length. Process the pairs in order. Use binary search to find the first stored final pair greater than or equal to the current pair, and replace it with the current pair. If no such position exists, append the current pair. This check takes O(Nlog⁡N)O(N\log N)O(NlogN) time.

A sample that satisfies the condition on a certain day also satisfies it on every later day. Consequently, the predicate that the longest increasing subsequence has length at least KKK changes only from false to true as the day increases. Let U=109U=10^9U=109. If the condition holds on day 000, output 000 immediately. Otherwise, if the length is still less than on day , output . In the remaining cases, binary search over for the first day satisfying the condition.

The initial sort takes O(Nlog⁡N)O(N\log N)O(NlogN) time, and there are O(log⁡U)O(\log U)O(logU) checks. The total time complexity is O(Nlog⁡Nlog⁡U)O(N\log N\log U)O(NlogN, and the space complexity is .

Proof

Consider two types p,qp,qp,q in their growth-coefficient order, with ppp appearing first. Then Bp≤BqB_p\leq B_qBp​≤B. The population difference never decreases as increases.

Solution written by GPT6

j​
​
+
Bpj​​t,pj​)
Xj(t)X_j(t)Xj​(t)
ttt
KK
K
UUU
-1
[0,U][0,U][0,U]
log
U
)
O(N)O(N)O(N)
q​
D(t)=(Aq+Bqt)−(Ap+Bpt)D(t)=(A_q+B_qt)-(A_p+B_pt)D(t)=(Aq​+Bq​t)−(Ap​+Bp​t)
ttt

If ppp also precedes qqq in population order on day ttt, then either D(t)>0D(t)>0D(t)>0, or D(t)=0D(t)=0D(t)=0 and p<qp<qp<q. The difference can only increase or stay equal on a later day, so ppp continues to precede qqq in both cases. Therefore, a valid sample remains valid on every later day, proving monotonicity.

If Bp<BqB_p<B_qBp​<Bq​, then on day UUU the difference satisfies D(U)=(Aq−Ap)+(Bq−Bp)U≥(1−109)+109=1D(U)=(A_q-A_p)+(B_q-B_p)U\geq (1-10^9)+10^9=1D(U)=(Aq​−Ap​)+(Bq​. Hence, two types with different growth coefficients necessarily have consistent orders on day UUU. If their growth coefficients are equal, their population difference never changes, so whether their orders agree never changes either. Thus, any sample that is valid on any day is also valid on day UUU. If no sufficiently large sample exists on day UUU, none can exist on any day. Otherwise, the earliest valid day lies in [0,U][0,U][0,U].

Listing the selected types in growth-coefficient order gives a subsequence of p1,p2,…,pNp_1,p_2,\ldots,p_Np1​,p2​,…,pN​. Their population order is identical exactly when the corresponding pairs (Ai+Bit,i)(A_i+B_it,i)(Ai​+Bi​t,i) are strictly increasing. Valid samples therefore correspond exactly to increasing subsequences.

Among subsequences of the same length, keeping the smallest final pair cannot reduce the possible extensions by later pairs. Replacing the first stored final pair greater than or equal to the current pair extends a shorter subsequence to the current length; no longer subsequence can end at the current pair. Maintaining this invariant makes the length of the stored array exactly the longest increasing subsequence length. The check is therefore correct, and binary search with this correct monotone predicate returns the earliest valid day.

−
Bp​)U≥
(1−
109)+
109=
1