-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOJ16637.cpp
59 lines (50 loc) · 1.09 KB
/
BOJ16637.cpp
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
//
// Created by 전형진 on 2020-03-01.
//
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <climits>
using namespace std;
vector<char> ar;
vector<int> num;
long long ans = LLONG_MIN;
long long cal(char op, long long n1, long long n2){
if(op == '+')
return n1+n2;
else if(op == '-')
return n1-n2;
else
return n1*n2;
}
void solve(long long result, int idx){
if(idx >= ar.size()){
if(ans < result)
ans = result;
return;
}
long long temp1 = cal(ar[idx],result,num[idx+1]);
solve(temp1,idx+1);
if(idx+1<ar.size()){
long long temp2 = cal(ar[idx+1],num[idx+1],num[idx+2]);
solve(cal(ar[idx],result,temp2),idx+2);
}
}
int main(){
int N;
string str;
scanf("%d",&N);
cin>>str;
for (int i = 0; i < str.length(); ++i) {
if(str[i] == '+' || str[i] == '-' || str[i] == '*'){
ar.push_back(str[i]);
}
else{
num.push_back(str[i] - '0');
}
}
solve(num[0],0);
cout<<ans<<endl;
}