前缀和 + upper_bound
#include <bits/stdc++.h>
using i64 = long long;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, q;
std::cin >> n >> q;
std::vector<int> a(n);
for (int i = 0; i < n; i ++) {
std::cin >> a[i];
}
std::sort(a.begin(), a.end());
std::vector<i64> s(n + 1);
for (int i = 0; i < n; i ++) {
s[i + 1] = s[i] + a[i];
}
while (q--) {
int k, x;
std::cin >> k >> x;
int j = std::upper_bound(a.begin(), a.end(), x) - a.begin();
std::cout << s[j] - s[std::max(0, j - k)] << "\n";
}
return 0;
}