title: Maximum Sum Bitonic Subarray
signature: function maximum_sum_bitonic_subarray(arr, n)
source_py: def maximum_sum_bitonic_subarray(arr, n):
      msis = ([None] * n)
      msds = ([None] * n)
      max_sum = 0
      msis[0] = arr[0]
      for i in range(1, n):
          if (arr[i] > arr[(i - 1)]):
              msis[i] = (msis[(i - 1)] + arr[i])
          else:
              msis[i] = arr[i]
      msds[(n - 1)] = arr[(n - 1)]
      for i in range((n - 2), (- 1), (- 1)):
          if (arr[i] > arr[(i + 1)]):
              msds[i] = (msds[(i + 1)] + arr[i])
          else:
              msds[i] = arr[i]
      for i in range(n):
          if (max_sum < ((msis[i] + msds[i]) - arr[i])):
              max_sum = ((msis[i] + msds[i]) - arr[i])
      return max_sum
source_cpp: int maximum_sum_bitonic_subarray(vector<int> arr, int n) {
    int msis [ n ], msds [ n ];
    int max_sum = INT_MIN;
    msis [ 0 ] = arr [ 0 ];
    for ( int i = 1;
    i < n;
    i ++ ) if ( arr [ i ] > arr [ i - 1 ] ) msis [ i ] = msis [ i - 1 ] + arr [ i ];
    else msis [ i ] = arr [ i ];
    msds [ n - 1 ] = arr [ n - 1 ];
    for ( int i = n - 2;
    i >= 0;
    i -- ) if ( arr [ i ] > arr [ i + 1 ] ) msds [ i ] = msds [ i + 1 ] + arr [ i ];
    else msds [ i ] = arr [ i ];
    for ( int i = 0;
    i < n;
    i ++ ) if ( max_sum < ( msis [ i ] + msds [ i ] - arr [ i ] ) ) max_sum = msis [ i ] + msds [ i ] - arr [ i ];
    return max_sum;
  }
question_info.test_code: 
  
  -- SOLUTION CODE
  -- ============================================
  PLACEHOLDER_CODE_BODY
  
  -- TESTING CODE 
  -- ============================================
  function isCorrect(actual, expected)
      if type(expected) ~= type(actual) then
          return false
      end
      
      if type(expected) == 'table' then
          for k,v_expected in pairs(expected) do
              
              if actual[k] == nil then
                  return false
              end
              local v_actual = actual[k]
              if not isCorrect(v_actual, v_expected) then
                  return false
              end
              actual[k]=nil
          end
      
          if next(actual) then 
              return false
          end
          return true
      else
          return actual == expected
      end
  end
  
  function validateSolution(actual, expected)
      return isCorrect(actual,expected)
  end
  
  function driver(arr, n, expected)
      local exec_result;
      local status, error_msg = pcall(function () 
          exec_result=PLACEHOLDER_FN_NAME(arr, n) 
      end)
      if status then
          if validateSolution(exec_result,expected) then
              return "PASSED"
          end
          return "FAILED"
      else
          return string.format("ERROR=%s",string.gsub(error_msg,".*:[%d]+: ",""))
      end
  end
      
  function main()
      result = driver({7, 12, 16, 23, 26, 27, 39, 39, 40, 44, 57, 58, 77, 78, 81, 82, 84, 86, 91, 94, 94, 95, 97}, 13, 315)
      print(string.format("TEST-0...%s",result))
  
      result = driver({72, 38, -60, 98, -52, -38, -2, 94, 34, 56, 90, 46, 6, -2, 30, -96, -76, -96, -76, 32, 68, 64, -32, -4, 72, -62, 58, 20, -84, 0, -96, 70, -22, -56, 70, -74, -90, -86, -14, 82, -90, 40, -64, 94}, 37, 230)
      print(string.format("TEST-1...%s",result))
  
      result = driver({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 33, 1)
      print(string.format("TEST-2...%s",result))
  
      result = driver({69, 31, 85, 84, 28, 28}, 5, 228)
      print(string.format("TEST-3...%s",result))
  
      result = driver({-80, -74, -24, -22, -22, 4, 20, 28, 30, 32, 36, 58, 58, 68, 92, 94, 98}, 16, 312)
      print(string.format("TEST-4...%s",result))
  
      result = driver({1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1}, 10, 1)
      print(string.format("TEST-5...%s",result))
  
      result = driver({2, 2, 2, 4, 6, 7, 8, 10, 15, 17, 19, 20, 21, 27, 28, 29, 32, 32, 40, 44, 46, 47, 49, 50, 50, 52, 55, 56, 58, 59, 64, 69, 73, 74, 74, 77, 80, 80, 84, 89, 91, 95, 96, 96, 97, 98, 98, 99}, 46, 610)
      print(string.format("TEST-6...%s",result))
  
      result = driver({26, -86, 8, 64, -40, 64, 60, -16, 54, -42, -86, 14, -48, -20, -42, -4, -34, -52, -74, 22, 10}, 18, 68)
      print(string.format("TEST-7...%s",result))
  
      result = driver({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 27, 1)
      print(string.format("TEST-8...%s",result))
  
      result = driver({8, 5, 5, 56, 5, 38}, 3, 13)
      print(string.format("TEST-9...%s",result))
  
  end
  
  main()
question_info.entry_cls_name: Solution
question_info.test_list: [{"idx": 0, "outputs": 315, "inputs": {"arr": [7, 12, 16, 23, 26, 27, 39, 39, 40, 44, 57, 58, 77, 78, 81, 82, 84, 86, 91, 94, 94, 95, 97], "n": 13}}, {"idx": 1, "outputs": 230, "inputs": {"arr": [72, 38, -60, 98, -52, -38, -2, 94, 34, 56, 90, 46, 6, -2, 30, -96, -76, -96, -76, 32, 68, 64, -32, -4, 72, -62, 58, 20, -84, 0, -96, 70, -22, -56, 70, -74, -90, -86, -14, 82, -90, 40, -64, 94], "n": 37}}, {"idx": 2, "outputs": 1, "inputs": {"arr": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], "n": 33}}, {"idx": 3, "outputs": 228, "inputs": {"arr": [69, 31, 85, 84, 28, 28], "n": 5}}, {"idx": 4, "outputs": 312, "inputs": {"arr": [-80, -74, -24, -22, -22, 4, 20, 28, 30, 32, 36, 58, 58, 68, 92, 94, 98], "n": 16}}, {"idx": 5, "outputs": 1, "inputs": {"arr": [1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1], "n": 10}}, {"idx": 6, "outputs": 610, "inputs": {"arr": [2, 2, 2, 4, 6, 7, 8, 10, 15, 17, 19, 20, 21, 27, 28, 29, 32, 32, 40, 44, 46, 47, 49, 50, 50, 52, 55, 56, 58, 59, 64, 69, 73, 74, 74, 77, 80, 80, 84, 89, 91, 95, 96, 96, 97, 98, 98, 99], "n": 46}}, {"idx": 7, "outputs": 68, "inputs": {"arr": [26, -86, 8, 64, -40, 64, 60, -16, 54, -42, -86, 14, -48, -20, -42, -4, -34, -52, -74, 22, 10], "n": 18}}, {"idx": 8, "outputs": 1, "inputs": {"arr": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], "n": 27}}, {"idx": 9, "outputs": 13, "inputs": {"arr": [8, 5, 5, 56, 5, 38], "n": 3}}]
question_info.extension: lua
question_info.entry_fn_name: maximum_sum_bitonic_subarray
question_info.test_case_ids: 0 1 2 3 4 5 6 7 8 9
question_info.commands: lua __FILENAME__