title: Maximum Sum Bitonic Subarray
signature: maximum_sum_bitonic_subarray <- 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(7L, 12L, 16L, 23L, 26L, 27L, 39L, 39L, 40L, 44L, 57L, 58L, 77L, 78L, 81L, 82L, 84L, 86L, 91L, 94L, 94L, 95L, 97L), 13L, 
          315L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(72L, 38L, -60L, 98L, -52L, -38L, -2L, 94L, 34L, 56L, 90L, 46L, 6L, -2L, 30L, -96L, -76L, -96L, -76L, 32L, 68L, 64L, -32L, -4L, 72L, -62L, 58L, 20L, -84L, 0L, -96L, 70L, -22L, -56L, 70L, -74L, -90L, -86L, -14L, 82L, -90L, 40L, -64L, 94L), 37L, 
          230L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 33L, 
          1L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(69L, 31L, 85L, 84L, 28L, 28L), 5L, 
          228L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-80L, -74L, -24L, -22L, -22L, 4L, 20L, 28L, 30L, 32L, 36L, 58L, 58L, 68L, 92L, 94L, 98L), 16L, 
          312L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(1L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 1L, 1L), 10L, 
          1L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(2L, 2L, 2L, 4L, 6L, 7L, 8L, 10L, 15L, 17L, 19L, 20L, 21L, 27L, 28L, 29L, 32L, 32L, 40L, 44L, 46L, 47L, 49L, 50L, 50L, 52L, 55L, 56L, 58L, 59L, 64L, 69L, 73L, 74L, 74L, 77L, 80L, 80L, 84L, 89L, 91L, 95L, 96L, 96L, 97L, 98L, 98L, 99L), 46L, 
          610L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(26L, -86L, 8L, 64L, -40L, 64L, 60L, -16L, 54L, -42L, -86L, 14L, -48L, -20L, -42L, -4L, -34L, -52L, -74L, 22L, 10L), 18L, 
          68L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 27L, 
          1L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(8L, 5L, 5L, 56L, 5L, 38L), 3L, 
          13L
      )))
  
  }
  
  main()
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;
  }