-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14888.java
More file actions
74 lines (60 loc) · 1.89 KB
/
14888.java
File metadata and controls
74 lines (60 loc) · 1.89 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.io.*;
import java.util.*;
public class Main {
static ArrayList<Integer> calc = new ArrayList<>();
static int[] opArr = new int[4];
static int[] numArr;
static int[] opSeq;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
numArr = new int[N];
opSeq = new int[N - 1];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
numArr[i] = Integer.parseInt(st.nextToken());
}
st = new StringTokenizer(br.readLine());
for (int i = 0; i < 4; i++) {
opArr[i] = Integer.parseInt(st.nextToken());
}
dfs(0, N - 1);
calc.sort(Comparator.naturalOrder());
System.out.println(calc.get(calc.size() - 1));
System.out.println(calc.get(0));
}
private static void dfs(int depth, int opNum) {
if (depth == opNum) {
calc.add(calculate(depth + 1));
return;
}
for (int i = 0; i < 4; i++) {
if (opArr[i] > 0) {
opArr[i]--;
opSeq[depth] = i;
dfs(depth + 1, opNum);
opArr[i]++;
}
}
}
private static int calculate(int n) {
int num = numArr[0];
for (int i = 1; i < n; i++) {
switch (opSeq[i - 1]) {
case 0:
num = num + numArr[i];
break;
case 1:
num = num - numArr[i];
break;
case 2:
num = num * numArr[i];
break;
case 3:
num = num / numArr[i];
break;
}
}
return num;
}
}