fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n, ret = -987654321;
  4. string s;
  5. vector<int> nums;
  6. vector<char> operators;
  7.  
  8. int oper(char op, int a, int b){
  9. if(op == '+') return a + b;
  10. if(op == '-') return a - b;
  11. if(op == '*') return a * b;
  12. }
  13.  
  14. void go(int here, int num){
  15. if(here == nums.size() - 1){
  16. ret = max(ret, num);
  17. return;
  18. }
  19.  
  20. go(here + 1, oper(operators[here], num, nums[here + 1]));
  21.  
  22. if(here + 2 < nums.size()){
  23. int temp = oper(operators[here + 1], nums[here + 1], nums[here + 2]);
  24. go(here + 2, oper(operators[here], nums[here], temp));
  25. }
  26. return;
  27. }
  28.  
  29. int main(){
  30. cin >> n >> s;
  31. for(int i = 0; i < n; i++){
  32. if(i % 2 == 0) nums.push_back(s[i] - '0');
  33. else operators.push_back(s[i]);
  34. }
  35.  
  36. go(0, nums[0]);
  37. cout << ret << '\n';
  38. }
Success #stdin #stdout 0.01s 5292KB
stdin
19
1*2+3*4*5-6*7*8*9*9
stdout
426384