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

Fat Sequence

Back to problem

Editorial

Let the level LiL_iLi​ of AiA_iAi​ be the length of the longest increasing subsequence ending at AiA_iAi​, and let K=max⁡LiK = \max L_iK=maxLi​. All LiL_iLi​ can be computed in O(Nlog⁡N)O(N \log N)O(NlogN) with the well-known lower\_bound LIS algorithm.

Observation 1. In an increasing subsequence of length KKK, the kkk-th element has level exactly kkk. The first kkk elements form an increasing subsequence, so the level is at least kkk. If it were larger than kkk, appending the remaining elements would give an increasing subsequence longer than , a contradiction.

Observation 2. Elements with the same level are non-increasing in index order. If j<ij < ij<i, Lj=LiL_j = L_iLj​=Li​ and , then can be appended after , so , a contradiction.

Observation 3. Pick the largest value level by level from the top. Define gK,gK−1,⋯ ,g1g_K, g_{K-1}, \cdots, g_1gK​,gK−1​,⋯,g as follows.

  • gKg_KgK​ is the first element of level KKK, which is the one with the largest value.
  • gkg_kgk​ (k) is the first element of level whose value is smaller than . By Observation 2 it has the largest value among those elements.

Since gk+1g_{k+1}gk+1​ has level k+1k+1k+1, some element of level kkk lies before gk+1g_{k+1} and has a smaller value. The element is the first such element, so its index is automatically before . Hence is an increasing subsequence of length .

Now take any increasing subsequence h1,h2,⋯ ,hKh_1, h_2, \cdots, h_Kh1​,h2​,⋯,hK​ of length . By Observation 1, has level . We show from downwards. The inequality follows from the definition of . If , then , and is the largest value of level below , so .

Therefore hk≤gkh_k \leq g_khk​≤gk​ for every kkk, and ggg is the fat sequence. Equal sums force all elements to be equal, so the values of the fat sequence are always unique.

Implementation. Bucket the elements by level while keeping index order. For each level, scan its list from the front until a value smaller than gk+1g_{k+1}gk+1​ appears. Every list is scanned at most once, so this part is O(N)O(N)O(N).

int bound = INT_MAX;
for (int k = K - 1; k >= 0; k--) {
    int p = st[k];
    while (a[ord[p]] >= bound) p++;
    pick[k] = a[ord[p]];
    bound = pick[k];
}

The total time complexity is O(Nlog⁡N)O(N \log N)O(NlogN). The sum can reach about 101510^{15}1015, so a 64-bit integer is required.

The usual LIS reconstruction, which scans from the back and takes the first matching element, picks the smallest possible value on every level and is wrong. Also note that the increase is strict. Using upper\_bound instead of lower\_bound fails on inputs with equal values.

Solutions for the subtasks.

  • Subtask 1: check every subsequence in O(2NN)O(2^N N)O(2NN).
  • Subtask 2: an O(N2)O(N^2)O(N2) DP whose state is the lexicographically largest pair (length,sum)(\text{length}, \text{sum}), followed by backtracking.

Solution written by ClaudeOpus5.5

K−kK-kK−k
KKK
Aj<AiA_j < A_i
Aj​<Ai​
AiA_iAi​
AjA_jAj​
Li≥Lj+1L_i \geq L_j + 1Li​≥Lj​+1
1
​
<Kk < K
k<K
kkk
gk+1g_{k+1}gk+1​
gk+1​
gkg_kgk​
gk+1g_{k+1}gk+1​
g1,g2,⋯ ,gKg_1, g_2, \cdots, g_Kg1​,g2​,⋯,gK​
KKK
KK
K
hkh_khk​
kkk
hk≤gkh_k \leq g_khk​≤gk​
k=Kk = Kk=K
hK≤gKh_K \leq g_KhK​≤gK​
gKg_KgK​
hk+1≤gk+1h_{k+1} \leq g_{k+1}hk+1​≤gk+1​
hk<hk+1≤gk+1h_k < h_{k+1} \leq g_{k+1}hk​<hk+1​≤gk+1​
gkg_kgk​
kkk
gk+1g_{k+1}gk+1​
hk≤gkh_k \leq g_khk​≤gk​
(
length
,
sum
)
  • Subtask 3: keep the same DP state once per value. An element with value xxx only looks at the states of values below xxx, which gives O(100N)O(100N)O(100N).
  • Subtask 4: the solution above. A Fenwick tree over compressed values that stores the lexicographically largest pair (length,sum)(\text{length}, \text{sum})(length,sum) also runs in O(Nlog⁡N)O(N \log N)O(NlogN).