F
Anonymous
c_cpp
02/28/2026 12:22 PM
2.0 KB
50
Indexable
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
#define int long long
const int N = 1e6 + 10;
const int inf = 1e15;
const int mod = 998244353;
inline void solve(){
int n, m;
cin >> n >> m;
vector<vector<int>> v(n + 1);
// top particles which allow >= i, so the size = i + 1
// those who allow i - 1
for(int i = 0;i < n;i += 1){
int x, y;
cin >> x >> y;
v[y].push_back(x);
}
multiset<int> st;
vector<int> top(n + 2, -inf), original(n + 2);
int sum = 0, base = 0;
for(int i = n + 1;i >= 1;i -= 1){
for(auto &x : v[i - 1]){
st.insert(x);
sum += x;
}
while(st.size() > i){
sum -= *st.begin();
st.erase(st.find(*st.begin()));
}
if(st.size() == i){
base = max(base, sum);
top[i] = sum - *st.begin();
}
else if(st.size() == i - 1){
top[i] = sum;
}
}
original.front() = top[1];
for(int i = 1;i <= n;i += 1){
original[i] = max(original[i - 1], top[i + 1]);
}
for(int i = 0;i < m;i += 1){
int x, y;
cin >> x >> y;
cout << max(base, x + original[y]) << ' ';
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
cin >> t;
while(t--){
solve();
cout << '\n';
}
return 0;
}
Editor is loading...
Leave a Comment