title: Maximum Subsequence Sum Such That No Three Are Consecutive
signature: maximum_subsequence_sum_such_that_no_three_are_consecutive <- 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(5L, 6L, 8L, 9L, 10L, 10L, 16L, 17L, 17L, 20L, 21L, 22L, 23L, 28L, 29L, 32L, 36L, 37L, 40L, 41L, 42L, 43L, 47L, 47L, 48L, 48L, 49L, 49L, 52L, 52L, 53L, 59L, 61L, 64L, 65L, 79L, 79L, 81L, 87L, 91L, 92L, 98L), 35L, 
          832L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(98L, 76L, -80L, -30L, 82L, 52L, -14L, 28L, 98L, 18L, 82L, 52L, 26L, -62L, -8L), 7L, 
          308L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L), 7L, 
          2L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(21L, 26L, 85L, 73L, 47L, 10L, 54L, 9L, 11L, 70L, 42L, 95L, 44L, 91L), 12L, 
          419L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-94L, -92L, -90L, -84L, -76L, -68L, -60L, -50L, -34L, -34L, -20L, -16L, -6L, 18L, 50L, 54L, 66L, 70L, 96L), 9L, 
          -182L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(1L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L), 16L, 
          7L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(2L, 3L, 4L, 4L, 14L, 14L, 18L, 21L, 24L, 26L, 29L, 31L, 32L, 34L, 36L, 37L, 38L, 40L, 42L, 44L, 44L, 54L, 63L, 69L, 77L, 77L, 82L, 82L, 86L, 87L, 90L, 93L, 95L), 31L, 
          895L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(-46L, 64L, -44L, 88L, -74L, 54L, 40L, -2L, -24L, 94L, 40L, -44L, 56L, -54L, -60L, -86L, -58L, 48L, -90L, 12L, -76L, -30L, 94L, -34L, 14L, 12L, 80L, -40L, 60L), 22L, 
          450L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L), 7L, 
          0L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(4L, 32L, 63L, 23L, 44L, 57L, 59L, 69L, 88L, 61L, 66L, 61L, 65L, 33L, 79L, 58L, 71L, 2L, 80L, 41L, 83L, 12L, 20L, 9L, 7L, 40L, 36L, 97L, 10L, 98L, 66L, 78L, 71L, 37L, 53L), 26L, 
          895L
      )))
  
  }
  
  main()
source_py: def maximum_subsequence_sum_such_that_no_three_are_consecutive(arr, n):
      sum = [0 for k in range(n)]
      if (n >= 1):
          sum[0] = arr[0]
      if (n >= 2):
          sum[1] = (arr[0] + arr[1])
      if (n > 2):
          sum[2] = max(sum[1], max((arr[1] + arr[2]), (arr[0] + arr[2])))
      for i in range(3, n):
          sum[i] = max(max(sum[(i - 1)], (sum[(i - 2)] + arr[i])), ((arr[i] + arr[(i - 1)]) + sum[(i - 3)]))
      return sum[(n - 1)]
source_cpp: int maximum_subsequence_sum_such_that_no_three_are_consecutive(vector<int> arr, int n) {
    int sum [ n ];
    if ( n >= 1 ) sum [ 0 ] = arr [ 0 ];
    if ( n >= 2 ) sum [ 1 ] = arr [ 0 ] + arr [ 1 ];
    if ( n > 2 ) sum [ 2 ] = max ( sum [ 1 ], max ( arr [ 1 ] + arr [ 2 ], arr [ 0 ] + arr [ 2 ] ) );
    for ( int i = 3;
    i < n;
    i ++ ) sum [ i ] = max ( max ( sum [ i - 1 ], sum [ i - 2 ] + arr [ i ] ), arr [ i ] + arr [ i - 1 ] + sum [ i - 3 ] );
    return sum [ n - 1 ];
  }