题号 P6785 记录 R159681403 thx for help!
#include <bits/stdc++.h>
using namespace std;
int t[2000033];
int main(){
int n, k; cin >> n >> k;
int x = 0;
for(int i = 1; i <= n; ++ i){
int a, b; cin >> a >> b;
t[a]+=b; x=max(x,a);
}
int ans = -1;
if(k == 0){
for(int i = 0; i <= x; ++ i){
if(t[i] > 1){
ans = max(ans, i * t[i]);
}
}
if(ans == -1){
cout << "NO";
} else{
cout << ans;
}
return 0;
}
for(int i = 0; i <= x; ++i){
if(t[i] && t[i + k]){
ans = max(ans, min(t[i], t[i + k]) * (2 * i + k));
}
}
if(ans == -1){
cout << "NO";
} else{
cout << ans;
}
return 0;
}