75分求助
查看原帖
75分求助
1025097
koukou楼主2024/7/25 19:31

rt

思路双指针

#include<iostream>
#include<algorithm>
using namespace std;
int n, s[1001], ans = 0, k;
int main()
{
	cin >> n >> k;
	for(int i = 0; i < n; i++)
	{
		cin >> s[i];
		ans += s[i];
	}
	sort(s, s + n);
	int l = 0, r = n - 1;
	while(s[r] - s[l] > k)
	{
		ans -= s[r];
		r--;
	}
	cout << ans;
	return 0;
}
2024/7/25 19:31
加载中...