title: Maximum Sum Alternating Subsequence Sum
signature: function maximum_sum_alternating_subsequence_sum(arr, n)
source_py: def maximum_sum_alternating_subsequence_sum(arr, n):
      if (n == 1):
          return arr[0]
      dec = [0 for i in range((n + 1))]
      inc = [0 for i in range((n + 1))]
      dec[0] = inc[0] = arr[0]
      flag = 0
      for i in range(1, n):
          for j in range(i):
              if (arr[j] > arr[i]):
                  dec[i] = max(dec[i], (inc[j] + arr[i]))
                  flag = 1
              elif ((arr[j] < arr[i]) and (flag == 1)):
                  inc[i] = max(inc[i], (dec[j] + arr[i]))
      result = (- 2147483648)
      for i in range(n):
          if (result < inc[i]):
              result = inc[i]
          if (result < dec[i]):
              result = dec[i]
      return result
source_cpp: int maximum_sum_alternating_subsequence_sum(vector<int> arr, int n) {
    if ( n == 1 ) return arr [ 0 ];
    int dec [ n ];
    memset ( dec, 0, sizeof ( dec ) );
    int inc [ n ];
    memset ( inc, 0, sizeof ( inc ) );
    dec [ 0 ] = inc [ 0 ] = arr [ 0 ];
    int flag = 0;
    for ( int i = 1;
    i < n;
    i ++ ) {
      for ( int j = 0;
      j < i;
      j ++ ) {
        if ( arr [ j ] > arr [ i ] ) {
          dec [ i ] = max ( dec [ i ], inc [ j ] + arr [ i ] );
          flag = 1;
        }
        else if ( arr [ j ] < arr [ i ] && flag == 1 ) inc [ i ] = max ( inc [ i ], dec [ j ] + arr [ i ] );
      }
    }
    int result = INT_MIN;
    for ( int i = 0;
    i < n;
    i ++ ) {
      if ( result < inc [ i ] ) result = inc [ i ];
      if ( result < dec [ i ] ) result = dec [ i ];
    }
    return result;
  }
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({2, 5, 9, 15, 15, 20, 21, 26, 28, 32, 34, 38, 42, 42, 42, 46, 47, 48, 50, 54, 55, 60, 60, 61, 63, 63, 66, 74, 79, 80, 85, 91, 93}, 23, 2)
      print(string.format("TEST-0...%s",result))
  
      result = driver({-98}, 0, -2147483648)
      print(string.format("TEST-1...%s",result))
  
      result = driver({0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1}, 6, 0)
      print(string.format("TEST-2...%s",result))
  
      result = driver({70, 5, 20, 22, 44, 94, 69, 89, 45, 92, 56, 58, 36, 91, 82, 95, 9, 60, 4, 34, 37, 95, 38, 47, 81, 68, 73, 15, 88, 8, 95, 28, 97, 12, 24, 5, 26, 82, 47, 88, 28, 33, 17, 28, 11, 71, 74}, 24, 1207)
      print(string.format("TEST-3...%s",result))
  
      result = driver({-98, -96, -94, -94, -92, -86, -86, -84, -82, -68, -66, -66, -50, -48, -46, -36, -26, -8, -6, 8, 14, 16, 18, 22, 32, 48, 48, 50, 62, 70, 70, 74, 78, 78, 84, 86, 92, 94, 98}, 32, 0)
      print(string.format("TEST-4...%s",result))
  
      result = driver({0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1}, 5, 0)
      print(string.format("TEST-5...%s",result))
  
      result = driver({1, 2, 8, 8, 12, 14, 19, 24, 25, 32, 36, 45, 47, 53, 54, 56, 56, 58, 59, 60, 65, 68, 86, 86, 91, 98}, 13, 1)
      print(string.format("TEST-6...%s",result))
  
      result = driver({-94, 88, 94, 78, -34, 84, -32, 68, -72, 80}, 7, 162)
      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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 30, 0)
      print(string.format("TEST-8...%s",result))
  
      result = driver({76, 62, 62, 61, 63, 15, 61, 74, 50, 86, 60, 35, 91, 32, 93, 14, 52, 18, 14, 39}, 18, 832)
      print(string.format("TEST-9...%s",result))
  
  end
  
  main()
question_info.entry_cls_name: Solution
question_info.test_list: [{"idx": 0, "outputs": 2, "inputs": {"arr": [2, 5, 9, 15, 15, 20, 21, 26, 28, 32, 34, 38, 42, 42, 42, 46, 47, 48, 50, 54, 55, 60, 60, 61, 63, 63, 66, 74, 79, 80, 85, 91, 93], "n": 23}}, {"idx": 1, "outputs": -2147483648, "inputs": {"arr": [-98], "n": 0}}, {"idx": 2, "outputs": 0, "inputs": {"arr": [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], "n": 6}}, {"idx": 3, "outputs": 1207, "inputs": {"arr": [70, 5, 20, 22, 44, 94, 69, 89, 45, 92, 56, 58, 36, 91, 82, 95, 9, 60, 4, 34, 37, 95, 38, 47, 81, 68, 73, 15, 88, 8, 95, 28, 97, 12, 24, 5, 26, 82, 47, 88, 28, 33, 17, 28, 11, 71, 74], "n": 24}}, {"idx": 4, "outputs": 0, "inputs": {"arr": [-98, -96, -94, -94, -92, -86, -86, -84, -82, -68, -66, -66, -50, -48, -46, -36, -26, -8, -6, 8, 14, 16, 18, 22, 32, 48, 48, 50, 62, 70, 70, 74, 78, 78, 84, 86, 92, 94, 98], "n": 32}}, {"idx": 5, "outputs": 0, "inputs": {"arr": [0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1], "n": 5}}, {"idx": 6, "outputs": 1, "inputs": {"arr": [1, 2, 8, 8, 12, 14, 19, 24, 25, 32, 36, 45, 47, 53, 54, 56, 56, 58, 59, 60, 65, 68, 86, 86, 91, 98], "n": 13}}, {"idx": 7, "outputs": 162, "inputs": {"arr": [-94, 88, 94, 78, -34, 84, -32, 68, -72, 80], "n": 7}}, {"idx": 8, "outputs": 0, "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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], "n": 30}}, {"idx": 9, "outputs": 832, "inputs": {"arr": [76, 62, 62, 61, 63, 15, 61, 74, 50, 86, 60, 35, 91, 32, 93, 14, 52, 18, 14, 39], "n": 18}}]
question_info.extension: lua
question_info.entry_fn_name: maximum_sum_alternating_subsequence_sum
question_info.test_case_ids: 0 1 2 3 4 5 6 7 8 9
question_info.commands: lua __FILENAME__