-
Notifications
You must be signed in to change notification settings - Fork 0
/
25710.java
81 lines (62 loc) · 2.09 KB
/
25710.java
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import org.w3c.dom.Node;
import java.io.*;
import java.util.*;
public class Main {
static int N;
static int ans = Integer.MIN_VALUE;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
Map<Integer, Integer> map = new HashMap<>();
st = new StringTokenizer(br.readLine());
for(int i=0;i<N;i++){
int idx = Integer.parseInt(st.nextToken());
if(map.containsKey(idx)){
if(map.get(idx) == 2){
continue;
}
map.put(idx, 2);
} else{
map.put(idx, 1);
}
}
List<Integer> list = new ArrayList<>();
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if(entry.getValue() > 1){
list.add(entry.getKey());
list.add(entry.getKey());
} else{
list.add(entry.getKey());
}
}
combination(new int[2], new boolean[N], list.stream()
.mapToInt(Integer::intValue)
.toArray(), 0, 0);
bw.write(ans+"\n");
bw.flush();
}
public static void combination(int[] output, boolean[] visited, int[] seq, int depth, int start){
if(output.length == depth){
ans = Math.max(ans, getValue(output[0] * output[1]));
return;
}
for(int i = start;i<seq.length;i++){
if(!visited[i]){
visited[i] = true;
output[depth] = seq[i];
combination(output, visited, seq, depth + 1, i + 1);
visited[i] = false;
}
}
}
public static int getValue(int num){
int ans = 0;
while(num > 0){
ans += (num % 10);
num /= 10;
}
return ans;
}
}