LearnToCP
Sign in
Navigation
HomeRoadmapProblemsAbout Us
Theory
Contest Knowledge
Selecting an IDEInteractive TasksOutput-Only Tasks
Basics
Your First ProgramData types and IOC++ syntaxModuloFunctionsVectorsMatricesTime Complexity
Sorting
SortingCounting sortRadix Sort
Optimization Techniques
Two PointersSum of numbers 1 to nPrefix sumBinary SearchGreedyBinary Search FunctionsBinary Search by AnswerDivide and Conquer
Binary Numbers
Binary NumbersNumbers in codeBitwise OperationsBitmasks
Math
Binary ExponentiationPrime NumbersPrime FactorizationGCD and LCMSieve of EratosthenesModified Sieve
Data Structures
StringsStackQueueMapsSetsPriority QueueCustom Criteria for FunctionsSegment TreesFenwick TreesSparse TablesUnion FindSqrt Decomposition
Combinatorics
Addition PrincipleMultiplication PrincipleCombinatoric ObjectsInclusion Exclusion Principle
Geometry
Geometry BasicsVectorsCross and Dot ProductLinesPolygonsAnglesPoint in PolygonDistances and Intersection PointsConvex HullCircles
Recursion
PointersRecursionGenerating Combinatoric Objects
Dynamic Programming
About DPDP problemsTree DPBitmask DP
Graph Theory
GraphsDFS and BFSShortest PathsTreesTopological SortingDijkstra's AlgorithmMinimum Spanning TreesShortest Path Algorithms
Advanced Graph Theory
BiconnectivityStrongly Connected ComponentsBipartite GraphGraph FlowAugmenting PathsFlow - Minimum Cut DualityHeavy-Light DecompositionCentroid Decomposition
Advanced Data Structures
2D and 3D Segment TreesLazy PropagationImplicit Segment TreesPersistent Segment TreesLowest Common AncestorTrieBalanced Binary Search TreesMo's Algorithm

Longest Increasing Subsequence (LIS)

EasyProblem #6
Time LimitMemoryInputOutput
1 s64 MBstdinstdout

Besides finding the best value, this problem asks you to reconstruct the answer - a very useful DP skill.

Given an array of numbers, find the longest subsequence (the elements don't have to be next to each other) such that the numbers are strictly increasing.

Since there can be multiple such subsequences of the same (longest) length, print the lexicographically smallest one.

A sequence x is lexicographically smaller than a sequence y of the same length if, at the first position where they differ, x has the smaller element.

Input

First line of input will be a single number n, the length of the array.
In the next line will be n numbers a1​,a2​,…,an​.

Output

In the first line, a single number k - the length of the longest strictly increasing subsequence.
In the second line, k numbers - the lexicographically smallest longest strictly increasing subsequence.

Example

Input
8
3 6 1 2 8 2 4 5
Output
4
1 2 4 5

The longest strictly increasing subsequence has 4 elements, and the only one of that length is 1,2,4,5.

Constraints

1≤n≤1000
1≤ai​≤109

Submit your solution

Sign in to submit your solution and track your progress.

Sign in to submit