-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14002.java
More file actions
58 lines (43 loc) · 1.32 KB
/
14002.java
File metadata and controls
58 lines (43 loc) · 1.32 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0; i<n; i++)
arr[i] = sc.nextInt();
ArrayList<Integer>[] max = new ArrayList[n];
max[0] = new ArrayList<>();
max[0].add(arr[0]);
for(int i=1; i<n; i++) {
int highest = 1;
ArrayList<Integer> temp = null;
for(int j=i-1; j>=0; j--) {
if(arr[j] < arr[i] && highest < 1+max[j].size()) {
temp = max[j];
highest = 1+max[j].size();
}
}
if(temp == null) {
max[i] = new ArrayList<>();
max[i].add(arr[i]);
}
else {
max[i] = (ArrayList)temp.clone();
max[i].add(arr[i]);
}
}
int size = 0;
int idx = 0;
for(int i=0; i<n; i++) {
if(max[i].size() > size) {
size = max[i].size();
idx = i;
}
}
System.out.println(max[idx].size());
for(int i=0; i<max[idx].size(); i++) {
System.out.print(Integer.toString(max[idx].get(i)) + " ");
}
}
}