title: Maximum Sum Subarray Removing One Element
signature: maximum_sum_subarray_removing_one_element <- function(arr, n) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(arr, n, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(arr, n), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(2L, 8L, 14L, 17L, 19L, 35L, 38L, 45L, 50L, 53L, 55L, 70L, 82L, 88L, 92L, 96L), 13L, 
          488L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(-64L, -56L, -80L, -82L, 72L, 62L, -8L, 48L, -96L, 34L, 64L, -38L, -60L, 80L, 4L, -64L, -62L, 34L, 94L, -16L, 38L, 62L, -84L, 48L, 42L, -40L), 22L, 
          344L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 0L, 1L, 1L, 1L), 6L, 
          2L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(3L, 7L, 50L, 53L, 72L, 14L, 18L, 74L, 27L, 65L, 41L, 20L, 54L, 17L, 87L, 40L, 63L, 15L, 47L), 11L, 
          424L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-96L, -96L, -94L, -80L, -74L, -74L, -74L, -74L, -70L, -64L, -60L, -58L, -52L, -52L, -44L, -42L, -40L, -38L, -36L, -34L, -30L, -14L, -12L, -8L, -2L, 6L, 12L, 16L, 24L, 24L, 48L, 48L, 66L, 76L, 76L, 84L, 90L), 32L, 
          178L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L), 8L, 
          7L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(4L, 4L, 5L, 9L, 11L, 13L, 13L, 15L, 16L, 21L, 23L, 25L, 27L, 30L, 31L, 35L, 35L, 43L, 43L, 47L, 49L, 50L, 52L, 54L, 55L, 55L, 57L, 57L, 57L, 59L, 62L, 64L, 66L, 68L, 69L, 71L, 73L, 76L, 80L, 84L, 88L, 88L, 90L, 90L, 97L, 97L, 99L), 34L, 
          1255L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(-86L, -60L, 4L, 14L, 6L, -6L, -50L, 46L, -50L, -62L, -56L, 16L, -76L, 90L, 40L, 2L, 36L, 48L, -26L, 34L, 78L, 84L, 2L, -54L, 94L, 60L, -26L, 60L, 84L, 2L, -98L, 2L, -74L), 25L, 
          482L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 9L, 
          1L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(36L, 99L, 27L, 8L, 90L, 74L, 67L, 77L, 49L, 23L, 43L, 25L, 68L, 56L, 85L, 6L), 12L, 
          646L
      )))
  
  }
  
  main()
source_py: def maximum_sum_subarray_removing_one_element(arr, n):
      fw = [0 for k in range(n)]
      bw = [0 for k in range(n)]
      (cur_max, max_so_far) = (arr[0], arr[0])
      for i in range(n):
          cur_max = max(arr[i], (cur_max + arr[i]))
          max_so_far = max(max_so_far, cur_max)
          fw[i] = cur_max
      cur_max = max_so_far = bw[(n - 1)] = arr[(n - 1)]
      i = (n - 2)
      while (i >= 0):
          cur_max = max(arr[i], (cur_max + arr[i]))
          max_so_far = max(max_so_far, cur_max)
          bw[i] = cur_max
          i -= 1
      fans = max_so_far
      for i in range(1, (n - 1)):
          fans = max(fans, (fw[(i - 1)] + bw[(i + 1)]))
      return fans
source_cpp: int maximum_sum_subarray_removing_one_element(vector<int> arr, int n) {
    int fw [ n ], bw [ n ];
    int cur_max = arr [ 0 ], max_so_far = arr [ 0 ];
    fw [ 0 ] = arr [ 0 ];
    for ( int i = 1;
    i < n;
    i ++ ) {
      cur_max = max ( arr [ i ], cur_max + arr [ i ] );
      max_so_far = max ( max_so_far, cur_max );
      fw [ i ] = cur_max;
    }
    cur_max = max_so_far = bw [ n - 1 ] = arr [ n - 1 ];
    for ( int i = n - 2;
    i >= 0;
    i -- ) {
      cur_max = max ( arr [ i ], cur_max + arr [ i ] );
      max_so_far = max ( max_so_far, cur_max );
      bw [ i ] = cur_max;
    }
    int fans = max_so_far;
    for ( int i = 1;
    i < n - 1;
    i ++ ) fans = max ( fans, fw [ i - 1 ] + bw [ i + 1 ] );
    return fans;
  }