title: Maximum Triplet Sum Array 2
signature: function maximum_triplet_sum_array_2(arr, n)
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({4, 7, 12, 21, 22, 25, 27, 28, 28, 31, 32, 32, 41, 45, 47, 51, 53, 60, 61, 61, 63, 71, 74, 82, 83, 85, 88, 92, 96, 96}, 28, 265)
      print(string.format("TEST-0...%s",result))
  
      result = driver({-52, 26, 74, -62, -76}, 2, -100000026)
      print(string.format("TEST-1...%s",result))
  
      result = driver({0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 11, 3)
      print(string.format("TEST-2...%s",result))
  
      result = driver({63, 71, 15, 28, 31, 84, 8, 17, 24, 42, 66, 95, 30}, 6, 218)
      print(string.format("TEST-3...%s",result))
  
      result = driver({-94, -92, -92, -90, -88, -88, -86, -82, -80, -78, -66, -54, -52, -52, -46, -46, -42, -36, -32, -24, -24, -14, -14, -14, -12, -10, 0, 6, 8, 20, 24, 24, 28, 38, 38, 52, 54, 56, 64, 74, 74, 76, 82, 94, 94}, 31, 52)
      print(string.format("TEST-4...%s",result))
  
      result = driver({0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0}, 30, 3)
      print(string.format("TEST-5...%s",result))
  
      result = driver({15, 19, 80}, 2, -99999966)
      print(string.format("TEST-6...%s",result))
  
      result = driver({4, 80, 18, 74, 36, -30, -72, -28, -32, -16, -8, 38, 78, -48, 98, -64, 86, -60, -44, 84, -98, 40, 14, 30, 44, 90, -30, -42, 24, -28, 24, 40, -96, 98, 90, -68, -54, -52, 62, 34, -98, 68, -56, -94, -78, -12, 28}, 41, 286)
      print(string.format("TEST-7...%s",result))
  
      result = driver({0, 1, 1, 1, 1, 1}, 3, 2)
      print(string.format("TEST-8...%s",result))
  
      result = driver({2, 18, 96, 7, 99, 83, 3, 88, 23, 77, 6, 28, 55, 49, 69, 55, 48, 76, 43, 11, 43, 44, 17, 74, 27, 64, 76, 77, 53, 26, 73, 12, 19, 62, 18, 34, 13, 31, 97, 96, 85, 27, 30, 97, 89, 25}, 41, 292)
      print(string.format("TEST-9...%s",result))
  
  end
  
  main()
source_py: def maximum_triplet_sum_array_2(arr, n):
      maxA = (- 100000000)
      maxB = (- 100000000)
      maxC = (- 100000000)
      for i in range(0, n):
          if (arr[i] > maxA):
              maxC = maxB
              maxB = maxA
              maxA = arr[i]
          elif (arr[i] > maxB):
              maxC = maxB
              maxB = arr[i]
          elif (arr[i] > maxC):
              maxC = arr[i]
      return ((maxA + maxB) + maxC)
source_cpp: int maximum_triplet_sum_array_2(vector<int> arr, int n) {
    int maxA = INT_MIN, maxB = INT_MIN, maxC = INT_MIN;
    for ( int i = 0;
    i < n;
    i ++ ) {
      if ( arr [ i ] > maxA ) {
        maxC = maxB;
        maxB = maxA;
        maxA = arr [ i ];
      }
      else if ( arr [ i ] > maxB ) {
        maxC = maxB;
        maxB = arr [ i ];
      }
      else if ( arr [ i ] > maxC ) maxC = arr [ i ];
    }
    return ( maxA + maxB + maxC );
  }