int solution(vector<vector<int>>& apples, int m, int n, int k){
vector<vector<int>> tree_heights(n);
for (int i = 0; i < m; i++) {
tree_heights[apples[i][0]-1].push_back(apples[i][1]);
}
int res = 0;
for (int i = 0; i < n; i++) {
sort(tree_heights[i].begin(), tree_heights[i].end());
int max_cnt = INT_MIN;
for (int j = 0; j < tree_heights[i].size(); j++) {
int upper = tree_heights[i][j] + k;
int cnt = 1, start = j + 1;
while (start < tree_heights[i].size() && tree_heights[i][start] <= upper) {
cnt++;
start++;
}
max_cnt = max(max_cnt, cnt);
}
res += max_cnt;
}
return res;
}