-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
72 lines (59 loc) · 1.89 KB
/
Main.java
File metadata and controls
72 lines (59 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int add;
static int sub;
static int mul;
static int div;
static int minV = (int) (1e9);
static int maxV = -(int) (1e9);
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
StringTokenizer st1 = new StringTokenizer(br.readLine());
int[] numbers = new int[N];
int i = 0;
while (st1.hasMoreTokens()) {
numbers[i] = Integer.parseInt(st1.nextToken());
i++;
}
StringTokenizer st2 = new StringTokenizer(br.readLine());
add = Integer.parseInt(st2.nextToken());
sub = Integer.parseInt(st2.nextToken());
mul = Integer.parseInt(st2.nextToken());
div = Integer.parseInt(st2.nextToken());
int total = numbers[0];
DFS(numbers, total, 1);
System.out.println(maxV);
System.out.println(minV);
}
private static void DFS(int[] numbers, int total, int index) {
if (index == numbers.length) {
minV=Integer.min(minV,total);
maxV=Integer.max(maxV,total);
return;
}
if (add > 0) {
add -= 1;
DFS(numbers, total + numbers[index], index + 1);
add += 1;
}
if (sub > 0) {
sub -= 1;
DFS(numbers, total - numbers[index], index + 1);
sub += 1;
}
if (mul > 0) {
mul -= 1;
DFS(numbers, total * numbers[index], index + 1);
mul += 1;
}
if (div > 0) {
div -= 1;
DFS(numbers, total / numbers[index], (index + 1));
div += 1;
}
}
}