title: Maximum Sum Bitonic Subarray
signature: func maximumSumBitonicSubarray(arr []int, n int) int {
test_code: package main
  
  import (
      "fmt"
      "reflect"
      "math"
      "sort"
  )
  
  // SOLUTION CODE
  // ===================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE
  // ===================================
  func validateSolution(actual int, expected int) bool {
      return reflect.DeepEqual(actual,expected)
  }
  
  func driver(arr []int, n int, expected int) string {
      if validateSolution(PLACEHOLDER_FN_NAME(arr, n),expected) {
              return "PASSED"
      }
      return "FAILED"
  
  }
  
  func main() {
  
      // Use imports to avoid undefined errors
      var _ = reflect.DeepEqual
      _ = math.Abs
      _ = sort.Sort
      result := ""
      result = driver([]int{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)
      fmt.Printf("TEST-0...%s\n", result)
      
      result = driver([]int{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)
      fmt.Printf("TEST-1...%s\n", result)
      
      result = driver([]int{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)
      fmt.Printf("TEST-2...%s\n", result)
      
      result = driver([]int{69, 31, 85, 84, 28, 28}, 
  	5, 228)
      fmt.Printf("TEST-3...%s\n", result)
      
      result = driver([]int{-80, -74, -24, -22, -22, 4, 20, 28, 30, 32, 36, 58, 58, 68, 92, 94, 98}, 
  	16, 312)
      fmt.Printf("TEST-4...%s\n", result)
      
      result = driver([]int{1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1}, 
  	10, 1)
      fmt.Printf("TEST-5...%s\n", result)
      
      result = driver([]int{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)
      fmt.Printf("TEST-6...%s\n", result)
      
      result = driver([]int{26, -86, 8, 64, -40, 64, 60, -16, 54, -42, -86, 14, -48, -20, -42, -4, -34, -52, -74, 22, 10}, 
  	18, 68)
      fmt.Printf("TEST-7...%s\n", result)
      
      result = driver([]int{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)
      fmt.Printf("TEST-8...%s\n", result)
      
      result = driver([]int{8, 5, 5, 56, 5, 38}, 
  	3, 13)
      fmt.Printf("TEST-9...%s\n", result)
      
          
  }
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;
  }