File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed
Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments