-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
43 lines (37 loc) · 1.2 KB
/
Main.java
File metadata and controls
43 lines (37 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Stack;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int arr[] = new int[n+1];
int dp[] = new int[n+1];
StringTokenizer st = new StringTokenizer(br.readLine());
int max = -1;
for(int i = 1; i < n+1; i++){
arr[i] = Integer.parseInt(st.nextToken());
for(int j = 0 ; j < i; j++){
if(arr[i] > arr[j]){
dp[i] = Math.max(dp[j] + 1, dp[i]);
max = Math.max(dp[i], max);
}
}
}
int len = max;
Stack<Integer> stack = new Stack<>();
for(int i=n; i > 0; i--){
if(len == dp[i]) {
stack.push(arr[i]);
len--;
}
}
System.out.println(max);
while (!stack.isEmpty()){
System.out.print(stack.pop() + " ");
}
br.close();
}
}