文件目录

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n, k;
    int a[1005] = {};
    int b[1005] = {};
    int sum[1005] = {};
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        b[a[i]]++;
    }
    for (int i = 1; i <= 1004; i++)
    {
        sum[i] = sum[i - 1] + b[i] * i;
    }
    int ans = 0;
    for (int i = k; i <= 1004; i++)
    {
        int num = sum[i];
        if (i - k - 1 >= 0)
            num -= sum[i - k - 1];
        ans = max(ans, num);
    }
    cout << ans;
    return 0;
}