title: Maximum Sum Subsequence Least K Distant Elements
signature: maximum_sum_subsequence_least_k_distant_elements <- function(arr, n, k) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(arr, n, k, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(arr, n, k), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(3L, 5L, 20L, 21L, 23L, 26L, 27L, 31L, 33L, 38L, 39L, 41L, 48L, 48L, 50L, 51L, 56L, 57L, 64L, 68L, 69L, 70L, 71L, 74L, 76L, 86L, 97L), 23L, 15L, 
          98L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(32L, 34L, -40L, 90L, -82L, -70L, 30L, 26L, -76L, -46L, -84L, 76L, -76L), 9L, 10L, 
          90L
      )))
  
      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, 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), 22L, 34L, 
          0L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(96L, 15L, 30L, 25L, 83L), 2L, 3L, 
          96L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-90L, -82L, -80L, -76L, -62L, -58L, -50L, -48L, -46L, -38L, -38L, -38L, -38L, -38L, -34L, -32L, -24L, -22L, -16L, -16L, -4L, -2L, 10L, 10L, 20L, 26L, 26L, 32L, 38L, 38L, 44L, 44L, 46L, 48L, 58L, 62L, 64L, 66L, 76L, 78L, 78L, 82L, 92L, 96L, 96L, 98L), 27L, 30L, 
          26L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L), 9L, 9L, 
          1L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(1L, 2L, 9L, 17L, 24L, 31L, 31L, 33L, 56L, 57L, 61L, 71L, 73L, 74L, 76L, 77L, 79L, 83L, 86L, 95L, 99L), 12L, 10L, 
          72L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(-12L, 52L, -44L, 80L, -66L, 34L, 42L, -46L, 8L, 12L, -22L, -56L, 74L, -98L, -44L, 2L, -24L, -14L, -54L, -56L, -26L, -18L, -72L), 13L, 19L, 
          80L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 13L, 13L, 
          1L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(65L, 1L, 34L, 38L, 15L, 6L, 55L, 21L, 32L, 90L, 39L, 25L, 43L, 48L, 64L, 66L, 88L, 70L, 82L, 75L, 25L, 56L, 23L, 27L, 41L, 33L, 33L, 55L, 60L, 90L, 41L, 58L, 42L, 53L, 38L, 90L, 7L, 15L), 37L, 33L, 
          155L
      )))
  
  }
  
  main()
source_py: def maximum_sum_subsequence_least_k_distant_elements(arr, N, k):
      MS = [0 for i in range(N)]
      MS[(N - 1)] = arr[(N - 1)]
      for i in range((N - 2), (- 1), (- 1)):
          if (((i + k) + 1) >= N):
              MS[i] = max(arr[i], MS[(i + 1)])
          else:
              MS[i] = max((arr[i] + MS[((i + k) + 1)]), MS[(i + 1)])
      return MS[0]
source_cpp: int maximum_sum_subsequence_least_k_distant_elements(vector<int> arr, int N, int k) {
    int MS [ N ];
    MS [ N - 1 ] = arr [ N - 1 ];
    for ( int i = N - 2;
    i >= 0;
    i -- ) {
      if ( i + k + 1 >= N ) MS [ i ] = max ( arr [ i ], MS [ i + 1 ] );
      else MS [ i ] = max ( arr [ i ] + MS [ i + k + 1 ], MS [ i + 1 ] );
    }
    return MS [ 0 ];
  }