title: Maximum Sum Subsequence Least K Distant Elements
signature: function maximumSumSubsequenceLeastKDistantElements(arr, n, k) {
test_code: 
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  function convertToString(value) {
      return JSON.stringify(value, function (key, val) {
          if (val instanceof Set) {
              return Array.from(val);
          } return val;});
  }
  
  function validateSolution(actual, expected){
      return convertToString(actual)===convertToString(expected);
  }
  
  function driver(arr, n, k, expected){
      try{
          if (validateSolution(PLACEHOLDER_FN_NAME(arr, n, k),expected)){
              return "PASSED";
          }
          return "FAILED";
      } catch (error) {
        return error.name;
      }
      
  }
      
  function main(){
    let result = '';
    result = driver([3, 5, 20, 21, 23, 26, 27, 31, 33, 38, 39, 41, 48, 48, 50, 51, 56, 57, 64, 68, 69, 70, 71, 74, 76, 86, 97], 23, 15, 98);
    console.log(`TEST-0...${result}`);
  
    result = driver([32, 34, -40, 90, -82, -70, 30, 26, -76, -46, -84, 76, -76], 9, 10, 90);
    console.log(`TEST-1...${result}`);
  
    result = driver([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, 1, 1, 1, 1, 1, 1, 1], 22, 34, 0);
    console.log(`TEST-2...${result}`);
  
    result = driver([96, 15, 30, 25, 83], 2, 3, 96);
    console.log(`TEST-3...${result}`);
  
    result = driver([-90, -82, -80, -76, -62, -58, -50, -48, -46, -38, -38, -38, -38, -38, -34, -32, -24, -22, -16, -16, -4, -2, 10, 10, 20, 26, 26, 32, 38, 38, 44, 44, 46, 48, 58, 62, 64, 66, 76, 78, 78, 82, 92, 96, 96, 98], 27, 30, 26);
    console.log(`TEST-4...${result}`);
  
    result = driver([1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0], 9, 9, 1);
    console.log(`TEST-5...${result}`);
  
    result = driver([1, 2, 9, 17, 24, 31, 31, 33, 56, 57, 61, 71, 73, 74, 76, 77, 79, 83, 86, 95, 99], 12, 10, 72);
    console.log(`TEST-6...${result}`);
  
    result = driver([-12, 52, -44, 80, -66, 34, 42, -46, 8, 12, -22, -56, 74, -98, -44, 2, -24, -14, -54, -56, -26, -18, -72], 13, 19, 80);
    console.log(`TEST-7...${result}`);
  
    result = driver([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 13, 13, 1);
    console.log(`TEST-8...${result}`);
  
    result = driver([65, 1, 34, 38, 15, 6, 55, 21, 32, 90, 39, 25, 43, 48, 64, 66, 88, 70, 82, 75, 25, 56, 23, 27, 41, 33, 33, 55, 60, 90, 41, 58, 42, 53, 38, 90, 7, 15], 37, 33, 155);
    console.log(`TEST-9...${result}`);
  
    process.exit(0);
  };
  
  if (require.main === module) {
    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 ];
  }