summaryrefslogtreecommitdiff
path: root/progs/a934.py
blob: 8dc5f52a995f522b268e22646fcb67b34c03ee67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
def max_sum_pair_diff_lessthan_K(arr, N, K): 
	arr.sort() 
	dp = [0] * N 
	dp[0] = 0
	for i in range(1, N): 
		dp[i] = dp[i-1] 
		if (arr[i] - arr[i-1] < K): 
			if (i >= 2): 
				dp[i] = max(dp[i], dp[i-2] + arr[i] + arr[i-1]); 
			else: 
				dp[i] = max(dp[i], arr[i] + arr[i-1]); 
	return dp[N - 1]