Skip to content

Commit 65bffe4

Browse files
authored
Merge pull request #1278 from AlgorithmWithGod/JHLEE325
[20251031] BOJ / G3 / 괄호 추가하기 / 이준희
2 parents 892ef5c + a2b7d69 commit 65bffe4

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
static int N;
7+
static ArrayList<Integer> nums = new ArrayList<>();
8+
static ArrayList<Character> ops = new ArrayList<>();
9+
static int answer = Integer.MIN_VALUE;
10+
11+
public static void main(String[] args) throws IOException {
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
N = Integer.parseInt(br.readLine());
14+
String str = br.readLine();
15+
16+
for (int i = 0; i < N; i++) {
17+
char c = str.charAt(i);
18+
if (i % 2 == 0) nums.add(c - '0');
19+
else ops.add(c);
20+
}
21+
22+
dfs(0, nums.get(0));
23+
System.out.println(answer);
24+
}
25+
26+
static void dfs(int idx, int sum) {
27+
if (idx >= ops.size()) {
28+
answer = Math.max(answer, sum);
29+
return;
30+
}
31+
32+
int cur = calc(sum, ops.get(idx), nums.get(idx + 1));
33+
dfs(idx + 1, cur);
34+
35+
if (idx + 1 < ops.size()) {
36+
int next = calc(nums.get(idx + 1), ops.get(idx + 1), nums.get(idx + 2));
37+
int cur2 = calc(sum, ops.get(idx), next);
38+
dfs(idx + 2, cur2);
39+
}
40+
}
41+
42+
static int calc(int a, char op, int b) {
43+
if (op == '+') return a + b;
44+
else if (op == '-') return a - b;
45+
else return a * b;
46+
}
47+
}
48+
```

0 commit comments

Comments
 (0)