fork download
  1. #include<bits/stdc++.h>
  2. #define PB push_back
  3. using namespace std;
  4. int const N=1e6+1;
  5. vector<int>g[N];
  6. priority_queue<int>q;
  7.  
  8. int main(){
  9. ios_base::sync_with_stdio(0);
  10. cin.tie(0);
  11. int n,a,t,w=0;
  12. cin>>n;
  13. for(int i=0;i<n;i++){
  14. cin>>a>>t;
  15. g[a].PB(t);
  16. }
  17. /*for(int i=n;i>0;i--){
  18. cout<<i<<" ";
  19. for(int j:g[i]) cout<<j<<' ';
  20. cout<<endl;
  21. }*/
  22. for(int i=n;i>0;i--){
  23. while(g[i].empty()==false){
  24. t=g[i].back();
  25. g[i].pop_back();
  26. q.push(t);
  27. }
  28. if(!q.empty()){
  29. w+=q.top();
  30. q.pop();
  31. }
  32. }
  33. /*for(int i=n;i>0;i--){
  34. cout<<i<<" ";
  35. for(int j:g[i]) cout<<j<<' ';
  36. cout<<endl;
  37. }*/
  38. cout<<w;
  39. }
Success #stdin #stdout 0.01s 26932KB
stdin
5
3 2
1 3
2 1
2 4
1 1
stdout
9