title: Maximum Sum Alternating Subsequence Sum
signature: maximum_sum_alternating_subsequence_sum <- 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, 5L, 9L, 15L, 15L, 20L, 21L, 26L, 28L, 32L, 34L, 38L, 42L, 42L, 42L, 46L, 47L, 48L, 50L, 54L, 55L, 60L, 60L, 61L, 63L, 63L, 66L, 74L, 79L, 80L, 85L, 91L, 93L), 23L, 
          2L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(-98L), 0L, 
          -2147483648L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L), 6L, 
          0L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(70L, 5L, 20L, 22L, 44L, 94L, 69L, 89L, 45L, 92L, 56L, 58L, 36L, 91L, 82L, 95L, 9L, 60L, 4L, 34L, 37L, 95L, 38L, 47L, 81L, 68L, 73L, 15L, 88L, 8L, 95L, 28L, 97L, 12L, 24L, 5L, 26L, 82L, 47L, 88L, 28L, 33L, 17L, 28L, 11L, 71L, 74L), 24L, 
          1207L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-98L, -96L, -94L, -94L, -92L, -86L, -86L, -84L, -82L, -68L, -66L, -66L, -50L, -48L, -46L, -36L, -26L, -8L, -6L, 8L, 14L, 16L, 18L, 22L, 32L, 48L, 48L, 50L, 62L, 70L, 70L, 74L, 78L, 78L, 84L, 86L, 92L, 94L, 98L), 32L, 
          0L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 1L), 5L, 
          0L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(1L, 2L, 8L, 8L, 12L, 14L, 19L, 24L, 25L, 32L, 36L, 45L, 47L, 53L, 54L, 56L, 56L, 58L, 59L, 60L, 65L, 68L, 86L, 86L, 91L, 98L), 13L, 
          1L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(-94L, 88L, 94L, 78L, -34L, 84L, -32L, 68L, -72L, 80L), 7L, 
          162L
      )))
  
      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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 30L, 
          0L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(76L, 62L, 62L, 61L, 63L, 15L, 61L, 74L, 50L, 86L, 60L, 35L, 91L, 32L, 93L, 14L, 52L, 18L, 14L, 39L), 18L, 
          832L
      )))
  
  }
  
  main()
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;
  }