Problem:
O: 千咲ちゃんと遠足 (Chisaki and Picnic)
幼稚園生の千咲ちゃんは、遠足にお菓子をもっていくことにした。
お菓子は $N$ 個あって、$i$ 番目のお菓子は値段が $A_i$ 円で、美味しさが $B_i$ である。
千咲ちゃんには友達が $M$ 人いて、$j$ 番目の友達は、値段が $C_j$ 円以上のお菓子を $D_j$ 個以上千咲ちゃんがもっていると泣きわめく。
友達が泣きわめいてしまうと千咲ちゃんはかなしいので、そうならないようにお菓子をもっていきたい。
お菓子の美味しさの合計は最大でいくつになるか求めて、千咲ちゃんを助けなさい。
入力
1 行目には、お菓子の個数 $N$ と、友達の人数 $M$ が空白区切りで与えられる。
続く $N$ 行のうち $i$ 行目には、$A_i, B_i$ が空白区切りで与えられる。
続く $M$ 行のうち $j$ 行目には、$C_j, D_j$ が空白区切りで与えられる。
出力
どの友達も泣かないように持っていくお菓子を選んだ時、その美味しさの合計としてありうる最大値を出力せよ。
制約
$N, M$ は $1$ 以上 $100 \ 000$ 以下の整数
$A_1, A_2, A_3, \dots, A_N$ は $1$ 以上 $1 \ 000 \ 000 \ 000$ 以下の整数
$B_1, B_2, B_3, \dots, B_N$ は $1$ 以上 $1 \ 000 \ 000 \ 000$ 以下の整数
$A_1 \leq A_2 \leq A_3 \leq \cdots \leq A_N$ を満たす
$C_1, C_2, C_3, \dots, C_M$ は $1$ 以上 $1 \ 000 \ 000 \ 000$ 以下の整数
$D_1, D_2, D_3, \dots, D_M$ は $1$ 以上 $1 \ 000 \ 000 \ 000$ 以下の整数
$C_1 \leq C_2 \leq C_3 \leq \cdots \leq C_M$ を満たす
入力例1
3 1
10 1
20 2
30 3
20 2
出力例1
4
$1, 3$ 番目のお菓子を持っていくと、友達を泣かせない範囲で、持ってくるお菓子の美味しさの合計を最大化することができます。
$2, 3$ 番目のお菓子両方を持っていくと、「値段が $20$ 円以上のお菓子を $2$ 個以上持ってくる」ことになるので、友達が泣きます。
入力例2
5 3
10 1
20 4
30 5
40 2
50 3
20 3
30 4
40 2
出力例2
10
$1, 2, 3$ 番目のお菓子を持っていくことによって、どの友達も泣かせない条件のもとで、持ってくるお菓子の美味しさの合計を最大化することができます。

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=2167167167167167167;
const int INF=2100000000;
const ll mod=1e9+7;
#define rep(i,a) for (ll i=0;i<a;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}
template<class T> T min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}
template<class T> T max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}
template<class T> T sum(vector<T> &a){assert(!a.empty());T ans=a[0]-a[0];for(auto &x:a) ans+=x;return ans;}

void solve();
// oddloop
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	//cin>>t;
	rep(i,t) solve();
}

void solve(){
	int N,M;
	cin>>N>>M;
	vector<pair<ll,ll>> p(N),q(M);
	rep(i,N) cin>>p[i].first>>p[i].second;
	rep(i,M) cin>>q[i].first>>q[i].second;
	_pq<ll> pq;
	reverse(all(p));
	reverse(all(q));
	int ind=0;
	rep(i,M){
		while(ind<N&&p[ind].first>=q[i].first) pq.push(p[ind].second),ind++;
		while((int)(pq.size())>=q[i].second) pq.pop();
	}
	ll ans=0;
	while(ind<N) ans+=p[ind].second,ind++;
	while(!pq.empty()){
		ans+=pq.top();
		pq.pop();
	}
	cout<<ans<<"\n";
}
accuracy: 1
time_ms: 30
memory_kb: 6252
final_rank: 4

code_snippet: #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=2167167167167167167;
const int INF=2100000000;
const ll mod=1e9+7;
#define rep(i,a) for (ll i=0;i<a;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}
template<class T> T min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}
template<class T> T max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}
template<class T> T sum(vector<T> &a){assert(!a.empty());T ans=a[0]-a[0];for(auto &x:a) ans+=x;return ans;}

void solve();
// oddloop
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	//cin>>t;
	rep(i,t) solve();
}

void solve(){
	int N,M;
	cin>>N>>M;
	vector<pair<ll,ll>> p(N),q(M);
	rep(i,N) cin>>p[i].first>>p[i].second;
	rep(i,M) cin>>q[i].first>>q[i].second;
	_pq<ll> pq;
	Sore(p);
	Sore(q);
	int ind=0;
	rep(i,M){
		while(ind<N&&p[ind].first>=q[i].first) pq.push(p[ind].second),ind++;
		while((int)(pq.size())>=q[i].second) pq.pop();
	}
	ll ans=0;
	while(!pq.empty()){
		ans+=pq.top();
		pq.pop();
	}
	cout<<ans<<"\n";
}
accuracy: 0.05
time_ms: 30
memory_kb: 6080
final_rank: 20

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline ll time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n,m; cin >> n >> m;
    vector<int> a(n),b(n);
    vector<int> c(m),d(m);
    for(int i=0;i<n;i++){
        cin >> a[i] >> b[i];
    }
    for(int i=0;i<m;i++){
        cin >> c[i] >> d[i];
    }
    vector<int> x(n+1,2e9);
    for(int i=0;i<m;i++){
        int idx = lower_bound(a.begin(), a.end(),c[i])-a.begin();
        x[idx] = min(x[idx], d[i]);
    }
    for(int i=0;i<n;i++){
        x[i+1] = min(x[i+1],x[i]);
    }
    multiset<int> st;
    for(int i=n-1;i>=0;i--){
        st.insert(b[i]);
        if(st.size() >= x[i]){
            st.erase(st.begin());
        }
    }
    ll res = 0;
    for(auto p:st){
        res += p;
    }
    cout << res << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 5188
final_rank: 2

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;
 
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, int> pii;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
 
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
 
 
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
 
#define MOD 1000000007
 
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}
const double EPS = 1e-8, PI = acos(-1);
const double pi = 3.141592653589793238462643383279;
//ここから編集    
typedef string::const_iterator State;
ll GCD(ll a, ll b){
  return (b==0)?a:GCD(b, a%b);
}
ll LCM(ll a, ll b){
  return a/GCD(a, b) * b;
}
template< int mod >
struct ModInt {
  int x;
 
  ModInt() : x(0) {}
 
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
 
  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }
 
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
 
  ModInt operator-() const { return ModInt(-x); }
 
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
 
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
 
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
 
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
 
  bool operator==(const ModInt &p) const { return x == p.x; }
 
  bool operator!=(const ModInt &p) const { return x != p.x; }
 
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
 
  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
 
  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }
 
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }
 
  static int get_mod() { return mod; }
};
 
using modint = ModInt< 998244353 >;
template< typename T >
struct Combination {
  vector< T > _fact, _rfact, _inv;
 
  Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {
    _fact[0] = _rfact[sz] = _inv[0] = 1;
    for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;
    _rfact[sz] /= _fact[sz];
    for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);
    for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];
  }
 
  inline T fact(int k) const { return _fact[k]; }
 
  inline T rfact(int k) const { return _rfact[k]; }
 
  inline T inv(int k) const { return _inv[k]; }
 
  T P(int n, int r) const {
    if(r < 0 || n < r) return 0;
    return fact(n) * rfact(n - r);
  }
 
  T C(int p, int q) const {
    if(q < 0 || p < q) return 0;
    return fact(p) * rfact(q) * rfact(p - q);
  }
 
  T H(int n, int r) const {
    if(n < 0 || r < 0) return (0);
    return r == 0 ? 1 : C(n + r - 1, r);
  }
};
 
ll modpow(ll x, ll n, ll mod) {
  ll res = 1;
  x %= mod;
  if(x == 0) return 0;
  while(n) {
    if(n&1) res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
} 
inline long long mod(long long a, long long m) {
    return (a % m + m) % m;
}
template<typename T> 
struct BIT{
  int N;
  std::vector<T> node;
  BIT(){}
  BIT(int n){
    N = n;
    node.resize(N+10);
  }
  void build(int n) {
    N = n;
    node.resize(N+10);
  }
  /* a: 1-indexed */
  void add(int a, T x){
    for(int i=a; i<(int)node.size(); i += i & -i) node[i] += x;
  }

  /* [1, a] */
  T sum(int a){
    T res = 0;
    for(int x=a; x>0; x -= x & -x) res += node[x];
    return res;
  }

  /* [l, r] */
  T rangesum(int l, int r){
    return sum(r) - sum(l-1);
  }

  /* 
    a1+a2+...+aw >= valとなるような最小のwを返す
    @verify https://codeforces.com/contest/992/problem/E
  */
  int lower_bound(T val) {
    if(val < 0) return 0;

    int res = 0;
    int n = 1; 
    while (n < N) n *= 2;

    T tv=0;
    for (int k = n; k > 0; k /= 2) {
      if(res + k < N && node[res + k] < val){
        val -= node[res+k];
        res += k;
      }
    }
    return res+1; 
  }
};
struct UnionFind{
  std::vector<int> par;
  std::vector<int> siz;

  UnionFind(int sz_): par(sz_), siz(sz_) {
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  void init(int sz_){
    par.resize(sz_);
    siz.resize(sz_);
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  int root(int x){
    if(x == par[x]) return x;
    return par[x] = root(par[x]);
  }

  bool merge(int x, int y){
    x = root(x), y = root(y);
    if(x == y) return false;
    if(siz[x] < siz[y]) std::swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(int x, int y){
    return root(x) == root(y);
  }

  int size(int x){
    return siz[root(x)];
  }
};
struct RollingHash{

    using ull = unsigned long long;
    const ull mod = (1ULL << 61) - 1;
    const ull MASK30 = (1ULL << 30) - 1;
    const ull MASK31 = (1ULL << 31) - 1;

    const ull MASK61 = mod;

    ull base;
    int n;
    vector<ull> hash, pow;

    RollingHash(const string &s)
    {
        random_device rnd;
        mt19937_64 mt(rnd());
        base = 1001;
        
        n = (int)s.size();
        hash.assign(n+1, 0);
        pow.assign(n+1, 1);
        
        for(int i=0; i<n; i++){
            hash[i+1] = calc_mod(mul(hash[i], base) + s[i]);
            pow[i+1] = calc_mod(mul(pow[i], base));
        }
    }

    ull calc_mod(ull x){
        ull xu = x >> 61;
        ull xd = x & MASK61;
        ull res = xu + xd;
        if(res >= mod) res -= mod;
        return res;
    }

    ull mul(ull a, ull b){
        ull au = a >> 31;
        ull ad = a & MASK31;
        ull bu = b >> 31;
        ull bd = b & MASK31;
        ull mid = ad * bu + au * bd;
        ull midu = mid >> 30;
        ull midd = mid & MASK30;
        return calc_mod(au * bu * 2 + midu + (midd << 31) + ad * bd);
    }

    //[l,r)のハッシュ値
    inline ull get(int l, int r){
        ull res = calc_mod(hash[r] + mod*3-mul(hash[l], pow[r-l]));
        return res;
    }
    //string tのハッシュ値
    inline ull get(string t){
        ull res = 0;
        for(int i=0; i<t.size(); i++){
            res = calc_mod(mul(res, base)+t[i]);
        }
        return res;
    }
    //string s中のtの数をカウント
    inline int count(string t) {
        if(t.size() > n) return 0;
        auto hs = get(t);
        int res = 0;
        for(int i=0; i<n-t.size()+1; i++){
            if(get(i, i+t.size()) == hs) res++; 
        }
        return res;
    }

    /* 
        concat 
        @verify https://codeforces.com/problemset/problem/514/C
    */
    inline ull concat(ull h1, ull h2, int h2len){
      return calc_mod(h2 + mul(h1, pow[h2len]));
    }

    // LCPを求める S[a:] T[b:]
    inline int LCP(int a, int b){
        int len = min((int)hash.size()-a, (int)hash.size()-b);
        
        int lb = -1, ub = len;
        while(ub-lb>1){
            int mid = (lb+ub)/2;

            if(get(a, a+mid) == get(b, b+mid)) lb = mid;
            else ub = mid;
        }
        return lb;
    }
};

int main() {  
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  
  int n, m; cin >> n >> m;
  vector<pair<int, int>> vp;
  REP(i,n) {
    int a, b; cin >> a >> b;
    vp.push_back({a, b});
  }
  vector<int> c(m), d(m);
  REP(i,m) {
    cin >> c[i] >> d[i];
  }

  priority_queue<int, vector<int>, greater<int>> pq;
  int ptr = n-1;
  for(int i=m-1; i>=0; i--) {
    while(ptr >= 0 && c[i] <= vp[ptr].first) {
      pq.push(vp[ptr].second);
      ptr--;
    }
    while(d[i] <= pq.size()) pq.pop();
  }
  while(ptr >= 0) {
    pq.push(vp[ptr].second);
    ptr--;
  }
  ll ans = 0;
  while(pq.size()) {
    ans += pq.top(); pq.pop();
  }
  cout << ans << endl;
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 4664
final_rank: 1

code_snippet: #include <cstdio>
#include <algorithm>
#include <utility>
#include <queue>

using namespace std;

typedef long long int ll;

int N,M;

const int DATA_SIZE=100000;
const ll BIGNUM = 2000000000;

// 値段*美味しさ
pair<ll, ll> sweets[DATA_SIZE];
// 値段*個数
pair<ll,ll> friends[DATA_SIZE+1];

struct Sweet_ptr_comp{

    // 美味しさを下降順
    int operator()(pair<ll, ll> *a, pair<ll, ll> *b)
    {
        return a->second<=b->second;
    }
};

//
typedef priority_queue<pair<ll, ll> *, vector<pair<ll, ll> *>, Sweet_ptr_comp> MaxQueue;

int  main(){

    scanf("%d%d",&N,&M);
    for (int i=0;i<N;i++) 
        scanf("%lld%lld", &sweets[i].first, &sweets[i].second);

    ll prev=BIGNUM;
    for (int i=0;i<M;i++) {
        scanf("%lld%lld", &friends[i].first, &friends[i].second);
        friends[i].second=min(prev,friends[i].second);
        prev = friends[i].second;
    }

    //　仮想の友達
    friends[M]=pair<ll,ll>(BIGNUM,1);

    // indexは探索したお菓子のインデクス
    int index=0;
    ll ans=0;

    // 価値の小さいお菓子をいっぱい持つ
    while (index<N&&sweets[index].first<friends[0].first)
        ans+=sweets[index++].second;
    
    
    // printf("%lld\n",ans);

    MaxQueue Q;
    // 全ての友達にたいする
    for (int i=0;i<M;i++){


        // 値段が今の友達と次の友達の間のお菓子を最大ヒープに追加
        while (
            index<N
            &&sweets[index].first>=friends[i].first
            &&sweets[index].first<friends[i+1].first)
             Q.push(&sweets[index++]);

        // 美味しさの高い奴から
        for (int j=friends[i].second;j>friends[i+1].second&&!Q.empty();j--){

            // printf("%lld %lld\n",Q.top()->first,Q.top()->second);
            ans+=Q.top()->second;
            Q.pop();
        }

    }

    printf("%lld\n",ans);

    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 6548
final_rank: 5

code_snippet: #include <cstdio>
#include <algorithm>
#include <utility>
#include <queue>

using namespace std;

typedef long long int ll;

int N,M;

const int DATA_SIZE=100000;
const ll BIGNUM = 2000000000;

// 値段*美味しさ
pair<ll, ll> sweets[DATA_SIZE];
// 値段*個数
pair<ll,ll> friends[DATA_SIZE+1];

struct Sweet_ptr_comp{

    // 美味しさを下降順
    int operator()(pair<ll, ll> *a, pair<ll, ll> *b)
    {
        return a->second<=b->second;
    }
};

// 
using MaxQueue=priority_queue<pair<ll,ll>*,vector<pair<ll,ll>*>,Sweet_ptr_comp>;


int main(){

    scanf("%d%d",&N,&M);
    for (int i=0;i<N;i++) 
        scanf("%lld%lld", &sweets[i].first, &sweets[i].second);

    ll prev=BIGNUM;
    for (int i=0;i<M;i++) {
        scanf("%lld%lld", &friends[i].first, &friends[i].second);
        friends[i].second=min(prev,friends[i].second);
        prev = friends[i].second;
    }

    //　仮想の友達
    friends[M]=pair<ll,ll>(BIGNUM,1);

    // indexは探索したお菓子のインデクス
    int index=0;
    ll ans=0;

    // 価値の小さいお菓子をいっぱい持つ
    while (index<N&&sweets[index].first<friends[0].first)
        ans+=sweets[index++].second;
    
    
    // printf("%lld\n",ans);

    MaxQueue Q;
    // 全ての友達にたいする
    for (int i=0;i<M;i++){


        // 値段が今の友達と次の友達の間のお菓子を最大ヒープに追加
        while (
            index<N
            &&sweets[index].first>=friends[i].first
            &&sweets[index].first<friends[i+1].first)
             Q.push(&sweets[index++]);

        // 美味しさの高い奴から
        for (int j=friends[i].second;j>friends[i+1].second&&!Q.empty();j--){

            // printf("%lld %lld\n",Q.top()->first,Q.top()->second);
            ans+=Q.top()->second;
            Q.pop();
        }

    }

    printf("%lld\n",ans);

    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 6576
final_rank: 6

code_snippet: #include<iostream>
#include<queue>
using namespace std;
int N,M;
int A[1<<17],B[1<<17],C[1<<17],D[1<<17];
main()
{
	cin>>N>>M;
	for(int i=0;i<N;i++)cin>>A[i]>>B[i];
	for(int i=0;i<M;i++)cin>>C[i]>>D[i];
	int L=N-1,R=M-1;
	priority_queue<int>P;
	while(0<=L||0<=R)
	{
		int X=0<=L?A[L]:-1;
		int Y=0<=R?C[R]:-1;
		if(X>=Y)
		{
			P.push(-B[L]);
			L--;
		}
		else
		{
			int d=D[R];
			while(P.size()>=d)P.pop();
			R--;
		}
	}
	long ans=0;
	while(!P.empty())ans-=P.top(),P.pop();
	cout<<ans<<endl;
}
accuracy: 1
time_ms: 100
memory_kb: 4752
final_rank: 9

code_snippet: #ifdef LOCAL
    #define _GLIBCXX_DEBUG
    #define __clock__
#else
    #pragma GCC optimize("Ofast")
    // #define NDEBUG
#endif
#define __precision__ 10
#define iostream_untie true
#define debug_stream std::cerr

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>

#define all(v) std::begin(v), std::end(v)
#define rall(v) std::rbegin(v), std::rend(v)
#define odd(n) ((n) & 1)
#define even(n) (not __odd(n))
#define __popcount(n) __builtin_popcountll(n)
#define __clz32(n) __builtin_clz(n)
#define __clz64(n) __builtin_clzll(n)
#define __ctz32(n) __builtin_ctz(n)
#define __ctz64(n) __builtin_ctzll(n)

using i32 = int_least32_t; using i64 = int_least64_t; using u32 = uint_least32_t; using u64 = uint_least64_t;
using pii = std::pair<i32, i32>; using pll = std::pair<i64, i64>;
template <class T> using heap = std::priority_queue<T>;
template <class T> using rheap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class K, class T> using hashmap = std::unordered_map<K, T>;
template <class T> using hashset = std::unordered_set<T>;

namespace execution
{
    using namespace std::chrono;
    system_clock::time_point start_time, end_time;
    long long get_elapsed_time() { end_time = system_clock::now(); return duration_cast<milliseconds>(end_time - start_time).count(); }
    void print_elapsed_time() { std::cerr << "\n----- Exec time : " << get_elapsed_time() << " ms -----\n\n"; }
    struct setupper
    {
        setupper()
        {
            if(iostream_untie) std::ios::sync_with_stdio(false), std::cin.tie(nullptr);
            std::cout << std::fixed << std::setprecision(__precision__);
    #ifdef stderr_path
            if(freopen(stderr_path, "a", stderr))
            {
                std::cerr << std::fixed << std::setprecision(__precision__);
            }
    #endif
    #ifdef stdout_path
            if(not freopen(stdout_path, "w", stdout))
            {
                freopen("CON", "w", stdout);
                std::cerr << "Failed to open the stdout file\n\n";
            }
            std::cout << "";
    #endif
    #ifdef stdin_path
            if(not freopen(stdin_path, "r", stdin))
            {
                freopen("CON", "r", stdin);
                std::cerr << "Failed to open the stdin file\n\n";
            }
    #endif
    #ifdef LOCAL
            std::cerr << "----- stderr at LOCAL -----\n\n";
            atexit(print_elapsed_time);
    #else
            fclose(stderr);
    #endif
    #ifdef __clock__
            start_time = system_clock::now();
    #endif
        }
    } __setupper;
    class myclock_t
    {
        system_clock::time_point built_pt, last_pt; int built_ln, last_ln;
        std::string built_func, last_func; bool is_built;
    public:
        myclock_t() : is_built(false) {}
        void build(int crt_ln, const std::string &crt_func)
        {
            is_built = true, last_pt = built_pt = system_clock::now(),  last_ln = built_ln = crt_ln, last_func = built_func = crt_func;
        }
        void set(int crt_ln, const std::string &crt_func)
        {
            if(is_built) last_pt = system_clock::now(), last_ln = crt_ln, last_func = crt_func;
            else debug_stream << "[ " << crt_ln << " : " << crt_func << " ] " << "myclock_t::set failed (yet to be built!)\n";
        }
        void get(int crt_ln, const std::string &crt_func)
        {
            if(is_built)
            {
                system_clock::time_point crt_pt(system_clock::now());
                long long diff = duration_cast<milliseconds>(crt_pt - last_pt).count();
                debug_stream << diff << " ms elapsed from" << " [ " << last_ln << " : " << last_func << " ]";
                if(last_ln == built_ln) debug_stream << " (when built)";
                debug_stream << " to" << " [ " << crt_ln << " : " << crt_func << " ]" << "\n";
                last_pt = built_pt, last_ln = built_ln, last_func = built_func;
            }
            else
            {
                debug_stream << "[ " << crt_ln << " : " << crt_func << " ] " << "myclock_t::get failed (yet to be built!)\n";
            }
        }
    };
} // namespace execution

#ifdef __clock__
    execution::myclock_t __myclock;
    #define build_clock() __myclock.build(__LINE__, __func__)
    #define set_clock() __myclock.set(__LINE__, __func__)
    #define get_clock() __myclock.get(__LINE__, __func__)
#else
    #define build_clock() ((void)0)
    #define set_clock() ((void)0)
    #define get_clock() ((void)0)
#endif

namespace std
{
    template <class P> void rsort(P __first, P __last) { sort(__first, __last, greater<>()); }
    template <class T> size_t hash_combine(size_t seed, T const &key) { return seed ^ (hash<T>()(key) + 0x9e3779b9 + (seed << 6) + (seed >> 2)); }
    template <class T, class U> struct hash<pair<T, U>> { size_t operator()(pair<T, U> const &pr) const { return hash_combine(hash_combine(0, pr.first), pr.second); } };
    template <class tuple_t, size_t index = tuple_size<tuple_t>::value - 1> struct tuple_hash_calc { static size_t apply(size_t seed, tuple_t const &t) { return hash_combine(tuple_hash_calc<tuple_t, index - 1>::apply(seed, t), get<index>(t)); } };
    template <class tuple_t> struct tuple_hash_calc<tuple_t, 0> { static size_t apply(size_t seed, tuple_t const &t) { return hash_combine(seed, get<0>(t)); } };
    template <class... T> struct hash<tuple<T...>> { size_t operator()(tuple<T...> const &t) const { return tuple_hash_calc<tuple<T...>>::apply(0, t); } };
    template <class T, class U> istream &operator>>(std::istream &s, pair<T, U> &p) { return s >> p.first >> p.second; }
    template <class T, class U> ostream &operator<<(std::ostream &s, const pair<T, U> &p) { return s << p.first << " " << p.second; }
    template <class T> istream &operator>>(istream &s, vector<T> &v) { for(T &e : v) s >> e; return s; }
    template <class T> ostream &operator<<(ostream &s, const vector<T> &v)  { bool is_front = true; for(const T &e : v) { if(not is_front) s << ' '; else is_front = false; s << e; } return s; }
    template <class tuple_t, size_t index> struct tupleos { static ostream &apply(ostream &s, const tuple_t &t) { tupleos<tuple_t, index - 1>::apply(s, t); return s << " " << get<index>(t); } };
    template <class tuple_t> struct tupleos<tuple_t, 0> { static ostream &apply(ostream &s, const tuple_t &t) { return s << get<0>(t); } };
    template <class... T> ostream &operator<<(ostream &s, const tuple<T...> &t)  { return tupleos<tuple<T...>, tuple_size<tuple<T...>>::value - 1>::apply(s, t); }
    template <> ostream &operator<<(ostream &s, const tuple<> &t) { return s; }
    string revstr(string str) { reverse(str.begin(), str.end()); return str; }
} // namespace std

#ifdef LOCAL
    #define dump(...)                                                              \
        debug_stream << "[ " << __LINE__ << " : " << __FUNCTION__ << " ]\n",       \
            dump_func(#__VA_ARGS__, __VA_ARGS__)
    template <class T> void dump_func(const char *ptr, const T &x)
    {
        debug_stream << '\t';
        for(char c = *ptr; c != '\0'; c = *++ptr) if(c != ' ') debug_stream << c;
        debug_stream << " : " << x << '\n';
    }
    template <class T, class... rest_t> void dump_func(const char *ptr, const T &x, rest_t... rest)
    {
        debug_stream << '\t';
        for(char c = *ptr; c != ','; c = *++ptr) if(c != ' ') debug_stream << c;
        debug_stream << " : " << x << ",\n"; dump_func(++ptr, rest...);
    }
#else
    #define dump(...) ((void)0)
#endif
template <class P> void read_range(P __first, P __second) { for(P i = __first; i != __second; ++i) std::cin >> *i; }
template <class P> void write_range(P __first, P __second) { for(P i = __first; i != __second; std::cout << (++i == __second ? '\n' : ' ')) std::cout << *i; }

// substitue y for x if x > y.
template <class T> inline bool sbmin(T &x, const T &y) { return x > y ? x = y, true : false; }
// substitue y for x if x < y.
template <class T> inline bool sbmax(T &x, const T &y) { return x < y ? x = y, true : false; }
// binary search.
i64 bin(const std::function<bool(i64)> &pred, i64 ok, i64 ng)
{
    while(std::abs(ok - ng) > 1) { i64 mid = (ok + ng) / 2; (pred(mid) ? ok : ng) = mid; }
    return ok;
}
double bin(const std::function<bool(double)> &pred, double ok, double ng, const double eps)
{
    while(std::abs(ok - ng) > eps) { double mid = (ok + ng) / 2; (pred(mid) ? ok : ng) = mid; }
    return ok;
}
// be careful that val is type-sensitive.
template <class T, class A, size_t N> void init(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); }
// reset all bits.
template <class A> void reset(A &array) { memset(array, 0, sizeof(array)); }


/* The main code follows. */

using namespace std;

main()
{
    void __solve();
    u32 t = 1;
#ifdef LOCAL
    t = 1;
#endif
    // t = -1;
    // cin >> t;
    while(t--)
    {
        __solve();
    }
}

void __solve()
{
    int n,m; cin>>n>>m;
    vector<pii> sna(n),fri(m);
    cin>>sna>>fri;
    reverse(all(sna));
    rheap<int> hp;
    sna.emplace_back(0,0);
    for(auto s : sna)
    {
        while(!fri.empty() && s.first<fri.back().first)
        {
            while(fri.back().second<=hp.size()) hp.pop();
            fri.pop_back();
        }
        hp.emplace(s.second);
    }
    i64 ans=0;
    while(!hp.empty())
    {
        ans+=hp.top(); hp.pop();
    }
    std::cout << ans << '\n';
}
accuracy: 1
time_ms: 30
memory_kb: 5224
final_rank: 3

code_snippet: /*  -*- coding: utf-8 -*-
 *
 * 2929.cc: 
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_M = 100000;
const int MAX_E2 = 1 << 18; // = 262144
const int INF = 1 << 30;

/* typedef */

typedef long long ll;
typedef pair<int,int> pii;

template <typename T, const int MAX_E2>
struct SegTreeMin {
  int n, e2;
  T nodes[MAX_E2], dls[MAX_E2], inf;
  SegTreeMin() {}

  void init(int _n, T _inf) {
    n = _n, inf = _inf;
    for (e2 = 1; e2 < n; e2 <<= 1);
    fill(nodes, nodes + MAX_E2, inf);
    fill(dls, dls + MAX_E2, 0);
  }

  T &get(int i) { return nodes[e2 - 1 + i]; }
  void set(int i, T v) { get(i) = v; }

  void setall() {
    for (int j = e2 - 2; j >= 0; j--)
      nodes[j] = min<T>(nodes[j * 2 + 1], nodes[j * 2 + 2]);
  }

  void _update(int k) {
    if (dls[k] != 0) {
      int k0 = k * 2 + 1, k1 = k0 + 1;
      nodes[k0] += dls[k], nodes[k1] += dls[k];
      dls[k0] += dls[k], dls[k1] += dls[k];
      dls[k] = 0;
    }
  }
  
  void add_range(int r0, int r1, T v, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return;
    if (r0 <= i0 && i1 <= r1) {
      nodes[k] += v;
      dls[k] += v;
      return;
    }

    _update(k);

    int im = (i0 + i1) / 2;
    int k0 = k * 2 + 1, k1 = k0 + 1;
    add_range(r0, r1, v, k0, i0, im);
    add_range(r0, r1, v, k1, im, i1);
    nodes[k] = min<T>(nodes[k0], nodes[k1]);
  }
  void add_range(int r0, int r1, T v) { add_range(r0, r1, v, 0, 0, e2); }
  
  T min_range(int r0, int r1, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return INF;
    if (r0 <= i0 && i1 <= r1) return nodes[k];

    _update(k);

    int im = (i0 + i1) / 2;
    T v0 = min_range(r0, r1, k * 2 + 1, i0, im);
    T v1 = min_range(r0, r1, k * 2 + 2, im, i1);
    return min<T>(v0, v1);
  }
  T min_range(int r0, int r1) { return min_range(r0, r1, 0, 0, e2); }
};

/* global variables */

pii bas[MAX_N], cds[MAX_M + 1];
SegTreeMin<int,MAX_E2> st;

/* subroutines */

bool ltbas(const pii &a, const pii &b) {
  return a.first > b.first || (a.first == b.first && a.second < b.second);
}

void printpiis(int n, pii ps[]) {
  for (int i = 0; i < n; i++) printf("(%d,%d)", ps[i].first, ps[i].second);
  putchar('\n');
}

/* main */

int main() {
  int n, m;
  scanf("%d%d", &n, &m);

  for (int i = 0; i < n; i++)
    scanf("%d%d", &bas[i].second, &bas[i].first);
  sort(bas, bas + n, ltbas);
  //printpiis(n, bas);

  int k = 0;
  for (int i = 0; i < m; i++) {
    int ci, di;
    scanf("%d%d", &ci, &di);
    if (k == 0 || (cds[k - 1].first < ci && cds[k - 1].second > di))
      cds[k++] = pii(ci, di);
    else if (cds[k - 1].first == ci &&
	     cds[k - 1].second < di && cds[k - 1].second > di)
      cds[k - 1] = pii(ci, di);
  }
  //printpiis(k, cds);

  st.init(k, INF);
  for (int i = 0; i < k; i++) st.set(i, cds[i].second - 1);
  st.setall();

  ll bsum = 0;
  for (int i = 0; i < n; i++) {
    int &bi = bas[i].first, &ai = bas[i].second;
    int l = upper_bound(cds, cds + k, pii(ai, INF)) - cds;
    int dl = st.min_range(0, l);

    if (dl > 0) {
      bsum += bi;
      st.add_range(0, l, -1);
    }

    //printf("(%d,%d): l=%d, dl=%d\n", ai, bi, l, dl);
  }

  printf("%lld\n", bsum);
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 6840
final_rank: 7

code_snippet: #include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll LINF = (1LL << 60) - 1LL;
constexpr double eps = 1e-9;
constexpr ll MOD = 1000000007LL;
template <typename T> bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
};
template <typename T> bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
};
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
    for (int i = 0; i < v.size(); i++) {
        os << v[i] << (i + 1 == v.size() ? "\n" : " ");
    }
    return os;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
    t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
    for (auto &e : t) {
        fill_v(e, v);
    }
};
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    vector<int> a(n), b(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    vector<P> v;
    int mi = INF;
    map<int, int> mp;
    for (int i = 0; i < m; i++) {
        int c, d;
        cin >> c >> d;
        chmin(d, mi);
        chmin(mi, d);
        if (mp.count(c)) {
            chmin(mp[c], d);
        } else {
            mp[c] = d;
        }
    }
    for (auto p : mp) {
        v.push_back(P(p.first, p.second));
    }
    int id = v.size();
    id--;
    priority_queue<ll, vector<ll>, greater<ll>> q;
    ll ans = 0;
    for (int i = n - 1; i >= 0; i--) {
        while (id >= 0 && v[id].first > a[i]) {
            id--;
        }
        ans += b[i];
        q.push(b[i]);
        if (id >= 0 && q.size() >= v[id].second) {
            ans -= q.top();
            q.pop();
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 50
memory_kb: 10420
final_rank: 11

code_snippet: #define _USE_MATH_DEFINES

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>

using namespace std;

typedef pair<long long int, long long int> P;

long long int INF = 1e18;
long long int MOD = 1e9 + 7;

int main(){
    int N, M;
    cin >> N >> M;
    vector<P> v;
    for(int i = 0; i < N; i++){
        P p;
        cin >> p.first >> p.second;
        v.push_back(p);
    }
    for(int i = 0; i < M; i++){
        P p;
        cin >> p.first >> p.second;
        p.second *= -1;
        v.push_back(p);
    }
    sort(v.begin(), v.end());
    priority_queue<long long int> que;
    for(int i = v.size() - 1; i >= 0; i--){
        if(v[i].second < 0){
            while(que.size() >= -v[i].second){
                que.pop();
            }
        }else{
            que.push(-v[i].second);
        }
    }
    long long int ans = 0;
    while(!que.empty()){
        ans -= que.top();
        que.pop();
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 7020
final_rank: 13

code_snippet: #include "bits/stdc++.h"

#pragma warning(disable:4996)
using namespace std;

typedef int Value1;
typedef int Value2;
const Value1 Zero1(10000000);
const Value2 Zero2(0);

struct Node {
	Value1 sum;//更新された値. この値を参照する時は評価が完全に完了しているようにする.
	Value2 lazy;	//遅延されている値を保存している
	Node() :sum(Zero1) {
		lazy = Zero2;
	}
};
struct lazy_segtree {
	int N;
	vector<Node>dat;
	lazy_segtree(int n) :N(1) {
		while (N < n) N *= 2;
		dat.resize(2 * N);
		for (int i = N - 1; i >= 1; --i) {
			update_node(i);
		}
	}

	Value2 lazy_connect(const Value2 l, const Value2 r) {
		return l+r;
	}

	void lazy_func(const int k, const int a, const int b) {
		dat[k].sum+=dat[k].lazy;
	}

	Value1 connect(const Value1 l, const Value1 r) {
		return min(l,r);

	}

	// inlineつけないと大変なことになるよ!(遅い)
	inline void lazy_evaluate_node(int k, int a, int b) {
		lazy_func(k, a, b);
		if (k < N) { // 2*k(左の子番号) < 2*N (節点の数) のイメージで. 末端ノードじゃなきゃ伝搬するのと等価.
			dat[2 * k].lazy = lazy_connect(dat[2 * k].lazy, dat[k].lazy);	//次は君が伝搬してね☆って感じ.
			dat[2 * k + 1].lazy = lazy_connect(dat[2 * k + 1].lazy, dat[k].lazy);
		}
		dat[k].lazy = Zero2;
	}

	inline void update_node(int k) { // kの子が既に評価されていることが前提. 末端以外のときしか呼び出さないような位置に書くのでif文要らない.
		dat[k].sum = connect(dat[2 * k].sum, dat[2 * k + 1].sum);

	}

	// update(l,r,v) := [l,r)を更新する. 区間は0-indexed.
	void update(int l, int r, Value2 v, int k = 1, int a = 0, int b = -1) {
		if (b == -1)b = N;
		if (l < 0 || r < 0)assert(false);
		lazy_evaluate_node(k, a, b); 	// とりあえず辿ったノードは都合がいいので伝搬しとけ精神.

		if (b <= l || r <= a) //[a,b)と[l,r)が交差している場合
			return;
		if (l <= a && b <= r) { // [l,r)が[a,b)を完全に含んでいる場合
			dat[k].lazy = lazy_connect(dat[k].lazy, v);
			lazy_evaluate_node(k, a, b); //一回遅延評価しとかないと都合悪いので.
			return;
		}

		int m = (a + b) / 2;
		update(l, r, v, 2 * k, a, m);
		update(l, r, v, 2 * k + 1, m, b);
		update_node(k);
	}
	//get(l,r) := [l,r)に対するクエリの答えを得る. 区間は0-indexed.
	Value1 get(int l, int r, int k = 1, int a = 0, int b = -1) {
		if (b == -1)b = N;

		if (l < 0 || r<0)assert(false);
		lazy_evaluate_node(k, a, b); // とりあえず辿ったノードは都合がいいので伝搬しとけ精神.

		if (b <= l || r <= a) //[a,b)と[l,r)が交差している場合
			return Zero1;

		if (l <= a && b <= r) { // [l,r)が[a,b)を完全に含んでいる場合
			return dat[k].sum;
		}

		int m = (a + b) / 2;
		Value1 vl = get(l, r, 2 * k, a, m);
		Value1 vr = get(l, r, 2 * k + 1, m, b);
		update_node(k);
		return connect(vl, vr);
	}
};
int main() {
	int N,M;cin>>N>>M;
	vector<pair<int,int>>sweets(N);
	for (int i = 0; i < N; ++i) {
		int a,b;cin>>a>>b;
		sweets[i]=make_pair(b,a);
	}
	sort(sweets.begin(),sweets.end(),greater<pair<int,int>>());
	vector<pair<int,int>>friends(M);
	for (int i = 0; i < M; ++i) {
		int a,b;cin>>a>>b;
		friends[i]=make_pair(a,b);
	}
	sort(friends.begin(),friends.end());
	lazy_segtree seg(friends.size());
	for (int i = 0; i < friends.size(); ++i) {
		seg.update(i,i+1,-Zero1+friends[i].second);
	}
	long long int answer=0;
	for (auto sweet : sweets) {
		int value=sweet.first;
		int price=sweet.second;
		auto it=lower_bound(friends.begin(),friends.end(),make_pair(price,int(2e9)));
		seg.update(0,it-friends.begin(),-1);
		int num=seg.get(0,friends.size());
		if (num>=1) {
			answer+=value;
		}
		else {
			seg.update(0, it - friends.begin(), 1);
		}
	}
	cout<<answer<<endl;
	return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 6532
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

int main(){
    int n,m;
    cin >>n >>m;
    vector<int> a(n),b(n);
    rep(i,n) cin >>a[i] >>b[i];
    vector<int> c(m),d(m);
    rep(i,m) cin >>c[i] >>d[i];

    vector<int> x(n+1, n);
    rep(i,m){
        int idx = lower_bound(all(a), c[i]) - a.begin();
        x[idx] = min(x[idx], d[i]);
    }

    rep(i,n) x[i+1] = min(x[i+1], x[i]);

    set<int> s;
    for(int i=n-1; i>=0; --i){
        s.insert(b[i]);
        if(s.size() == x[i]) s.erase(s.begin());
    }

    ll ans = 0;
    for(int i:s) ans += i;
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 4932
final_rank: 17

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 100005

struct Info{
	ll cost,value;
};

struct Data{
	ll base,limit;
};

int N,M;
Info info[NUM];
Data data[NUM];


int main(){

	scanf("%d %d",&N,&M);

	for(int i = 0; i < N; i++){

		scanf("%lld %lld",&info[i].cost,&info[i].value);
	}

	int index = 0;
	ll tmp_base,tmp_limit,pre_base = -1;

	for(int i = 0; i < M; i++){

		scanf("%lld %lld",&tmp_base,&tmp_limit);
		if(tmp_base == pre_base){

			data[index-1].limit = min(data[index-1].limit,tmp_limit); //一番制約がきついものを残す
		}else{

			data[index].base = tmp_base;
			data[index].limit = tmp_limit;

			index++;
		}
	}

	M = index;

	data[M].base = BIG_NUM;
	data[M].limit = BIG_NUM;

	ll ans = 0;
	priority_queue<ll> Q,TMP;

	//最小制約より小さいものは解に計上
	for(index = 0; index < N && info[index].cost < data[0].base; index++){

		ans += info[index].value;
	}

	int current_limit = BIG_NUM;

	for(int i = 0; i < M; i++){

		if(current_limit > data[i].limit){ //制約が厳しくなるなら、既に条件を満たしているものを解に計上

			if(current_limit != BIG_NUM){

				for(int k = 0; k < (current_limit-data[i].limit) && Q.empty() == false; k++){

					ans += Q.top();
					Q.pop();
				}
			}
			current_limit = data[i].limit;
		}

		//data[i].base <= info[index].cost < data[i+1].baseとなるものをPQにpush
		for(;index < N && data[i].base <= info[index].cost && info[index].cost < data[i+1].base; index++){

			Q.push(info[index].value);
		}
	}

	for(int i = 0; i < current_limit-1 && Q.empty() == false; i++){

		ans += Q.top();
		Q.pop();
	}

	printf("%lld\n",ans);

	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 7104
final_rank: 8

code_snippet: #include<bits/stdc++.h>
typedef long long ll;
using namespace std;
ll n,m,a[100010],b[100010],j,k,ans;
priority_queue<ll> Q;
void Ad(ll c,ll d){
    for(;j<n&&a[j]<c;j++)Q.push(b[j]);
    for(;k>d;k--)if(!Q.empty()){ans+=Q.top(); Q.pop();}
}
int main(){
    cin>>n>>m;
    for(int i=0;i<n;i++)cin>>a[i]>>b[i];
    j=0,k=n+1,ans=0;
    for(int i=0;i<m;i++){
	ll c,d;cin>>c>>d;Ad(c,d);
    }
    Ad(2e9,1);
    cout<<ans<<endl;
    return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 5476
final_rank: 12

code_snippet: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll MAX = 100010;

signed main()
{
    ll n, m;
    cin >> n >> m;
    vector<ll> a(n);
    vector<ll> b(n);
    vector<ll> c(m);
    vector<ll> d(m);

    vector<ll> ne_comp;

    for(ll i = 0; i < n; ++i){
        cin >> a[i] >> b[i];
        ne_comp.emplace_back(a[i]);
    }
    for(ll i = 0; i < m; ++i){
        cin >> c[i] >> d[i];
        ne_comp.emplace_back(c[i]);
    }

    sort(ne_comp.begin(), ne_comp.end());

    ne_comp.erase(unique(ne_comp.begin(), ne_comp.end()), ne_comp.end());

    auto ne = [&](ll x){
        return distance(ne_comp.begin(), lower_bound(ne_comp.begin(), ne_comp.end(), x));
    };
    ll t = ne_comp.size();

    vector<ll> rui_bound(t + 1, 1e9);

    for(ll i = 0; i < m; ++i){
        ll ind = ne(c[i]);
        rui_bound[ind] = min(rui_bound[ind], d[i]);
    }

    for(ll i = 0; i <= t; ++i){
        rui_bound[i + 1] = min(rui_bound[i], rui_bound[i + 1]);
    }

    vector<vector<ll>> p(t);
    for(ll i = 0; i < n; ++i){
        ll ind = ne(a[i]);
        p[ind].push_back(b[i]);
    }

    multiset<ll> s;
    ll ret = 0;
    ll ans = 0;
    for(ll i = t - 1; i >= 0; --i){
        for(auto& x : p[i]){
            ans += x;
            s.insert(x);
        }
        while(s.size() >= rui_bound[i]){
            ans -= *s.begin();
            s.erase(s.begin());
        }

        ret = max(ret, ans);
    }
    cout << ret << endl;


}
accuracy: 1
time_ms: 130
memory_kb: 17060
final_rank: 19

code_snippet: #include <bits/stdc++.h>

using namespace std;
using i64 = int_fast64_t;
using db = long double;
using pii = pair<int, int>;
using pli = pair<int_fast64_t, int>;
using pdi = pair<double, int>;
template <class T> using mat = vector<vector<T>>;
template <class T> using minheap = priority_queue<T, vector<T>, greater<T>>;

#define each(i,v) for (auto i = begin(v); i != end(v); ++i)
#define reach(i,v) for (auto i = rbegin(v); i != rend(v); ++i)
#define urep(i,s,t) for (int i = (s); i <= (t); ++i)
#define drep(i,s,t) for (int i = (s); i >= (t); --i)
#define rep(i,n) urep(i, 0, (n)-1)
#define rep1(i,n) urep(i, 1, (n))
#define rrep(i,n) drep(i, (n)-1, 0)
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
#define fir first
#define sec second
#define front fro
#define back bac
#define vct vector
#define u_map unordered_map
#define u_set unordered_set
#define l_bnd lower_bound
#define u_bnd upper_bound
#define rsz resize
#define ers erase
#define emp emplace
#define emf emplace_front
#define emb emplace_back
#define pof pop_front
#define pob pop_back
#define mkp make_pair
#define mkt make_tuple
#define popcnt __builtin_popcount

struct setupper {
    setupper(uint_fast8_t prec) {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        cout << fixed << setprecision(prec);
#ifdef Local
        auto print_atexit = []() { cerr << "Exec time : " << clock() / (double)CLOCKS_PER_SEC * 1000.0 << "ms\n"; };
        atexit(print_atexit);
#endif
    }
} setuppre_obj(10);

namespace std {
    template <class T> void hash_combine(size_t &seed, T const &key) {
        seed ^= hash<T>()(key) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
    }
    template <class T, class U> struct hash<pair<T,U>> {
        size_t operator()(pair<T,U> const &pr) const
        {
            size_t seed = 0;
            hash_combine(seed,pr.first);
            hash_combine(seed,pr.second);
            return seed;
        }
    };
    template <class Tup, size_t index = tuple_size<Tup>::value - 1> struct hashval_calc {
        static void apply(size_t& seed, Tup const& tup) {
            hashval_calc<Tup, index - 1>::apply(seed, tup);
            hash_combine(seed,get<index>(tup));
        }
    };
    template <class Tup> struct hashval_calc<Tup,0> {
        static void apply(size_t& seed, Tup const& tup) {
            hash_combine(seed,get<0>(tup));
        }
    };
    template <class ...T> struct hash<tuple<T...>> {
        size_t operator()(tuple<T...> const& tup) const
        {
            size_t seed = 0;
            hashval_calc<tuple<T...>>::apply(seed,tup);
            return seed;
        }
    };
}

template <class T, class U> ostream& operator << (ostream& s, pair<T,U> p) { return s << p.fir << " " << p.sec; }
template <class T> ostream& operator << (ostream& s, vct<T> v) { each(i,v) { if(i != begin(v)) s << " "; s << *i; } return s; }
#define dump(...) cerr << " [ " << __LINE__ << " : " << __FUNCTION__ << " ] " << #__VA_ARGS__ << " : ";\
dump_func(__VA_ARGS__)
template <class T> void dump_func(T x) { cerr << x << '\n'; }
template <class T,class ...Rest> void dump_func(T x, Rest ... rest) { cerr << x << ","; dump_func(rest...); }
void read() {}
template <class T, class ...Rest> void read(T &x, Rest &... rest) { cin >> x; read(rest...); }
template <class T> void write(T x) { cout << x << '\n'; }
template <class T, class ...Rest> void write(T x, Rest ... rest) { cout << x << ' '; write(rest...); }
void writeln() {}
template <class T, class ...Rest> void writeln(T x, Rest ... rest) { cout << x << '\n'; writeln(rest...); }
#define esc(...) writeln(__VA_ARGS__), exit(0)
const auto add = [](auto &x, auto y) { x += y; };
const auto mul = [](auto &x, auto y) { x *= y; };
const auto lam_min = [](auto x, auto y) { return min(x, y); };
const auto lam_max = [](auto x, auto y) { return max(x, y); };
const auto chmax = [](auto &m, auto x) { if(m < x){ m = x; return true; } return false; };
const auto chmin = [](auto &m, auto x) { if(m > x){ m = x; return true; } return false; };
bool bit(i64 n, uint8_t e) { return (n >> e) & 1; }
i64 mask(i64 n, uint8_t e) { return n & ((1 << e) - 1); }
int ilog(uint64_t x, uint64_t b = 2) { return x ? 1 + ilog(x / b,b) : -1; }
template <class F> i64 binsr(i64 ok, i64 ng, const F &fn) {
    while (abs(ok - ng) > 1) {
        i64 mid = (ok + ng) / 2;
        (fn(mid) ? ok : ng) = mid;
    }
    return ok;
}
template <class A, size_t N, class T> void init(A (&array)[N], const T &val) { fill((T*)array,(T*)(array + N),val); }
template <class T> T cmprs(T &v) {
    T tmp = v, ret = v;
    sort(all(tmp));
    tmp.erase(unique(all(tmp)), end(tmp));
    each(i,ret) *i = l_bnd(all(tmp),*i) - begin(tmp);
    return ret;
}

template <class T> constexpr T inf = numeric_limits<T>::max() / 2 - 1;
constexpr int dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
constexpr int dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
constexpr long double gold = 1.618033988;
constexpr long double eps = 1e-15;
constexpr uint_fast64_t mod = 1e9 + 7;

template <class Monoid> struct LazySegtree {
    typedef function<Monoid(const Monoid&, const Monoid&)> opr_t;
    typedef function<void(Monoid&, const Monoid&)> lazy_opr_t;
    typedef function<void(Monoid&, const Monoid&, uint)> update_opr_t;
    const opr_t opr;
    const lazy_opr_t lazy_opr;
    const update_opr_t update_opr;
    const Monoid idel,lazy_idel;
    vector<Monoid> data,lazy;
    vector<bool> lazyflag;
    int range = 1;

    LazySegtree(int n, Monoid idel_ = inf<int>, const opr_t &opr_ = lam_min, const lazy_opr_t &lazy_opr_ = [](Monoid &x, Monoid y){x += y;}, const update_opr_t &update_opr_ = [](Monoid &x, Monoid y, unsigned int w){x += y;}, Monoid lazy_idel_ = 0) : opr(opr_),lazy_opr(lazy_opr_),update_opr(update_opr_),idel(idel_),lazy_idel(lazy_idel_)
    {
        while(n >= range) range <<= 1;
        data.assign(range << 1,idel);
        lazy.assign(range << 1,lazy_idel);
        lazyflag.assign(range << 1,false);
    }

    template <class P> void copy(P s, P t) {
        for(int i = range; s != t; ++s, ++i) data[i] = *s;
        for(int i = range - 1; i; --i) data[i] = opr(data[i * 2],data[i * 2 + 1]);
    }

    void eval(int k, int l, int r) {
        if(!lazyflag[k]) return;
        update_opr(data[k],lazy[k],r - l);
        if(r - l > 1) {
            lazy_opr(lazy[k * 2],lazy[k]);
            lazy_opr(lazy[k * 2 + 1],lazy[k]);
            lazyflag[k * 2] = lazyflag[k * 2 + 1] = true;
        }
        lazy[k] = lazy_idel;
        lazyflag[k] = false;
    }

    void update(int a, int b, Monoid val, int k = 1, int l = 0, int r = -1) {
        if(r < 0) r = range;
        eval(k,l,r);
        if(b <= l || r <= a) return;
        if(a <= l && r <= b) {
            lazy_opr(lazy[k],val);
            lazyflag[k] = true;
            eval(k,l,r);
        } else {
            update(a,b,val,k * 2,l,l + r >> 1);
            update(a,b,val,k * 2 + 1,l + r >> 1,r);
            data[k] = opr(data[k * 2],data[k * 2 + 1]);
        }
    }

    Monoid query(int a, int b, int k = 1, int l = 0, int r = -1) {
        if(r < 0) r = range;
        if(b <= l || r <= a) return idel;
        eval(k,l,r);
        if(a <= l && r <= b) return data[k];
        return opr(query(a,b,k * 2,l,l + r >> 1),query(a,b,k * 2 + 1,l + r >> 1,r));
    }
};


int n,m;
i64 ans;
int c[1<<17],d[1<<17];
set<pii> chs;

int main() {
    cin>>n>>m;
    rep(i,n) {
        int a,b;
        cin>>a>>b;
        chs.emp(b,a);
    }
    rep(i,m) cin>>c[i]>>d[i],d[i]--;
    LazySegtree<int> lsg(m);
    lsg.copy(d,d+m);
    reach(i,chs) {
        int a=i->sec,b=i->fir;
        int uw=u_bnd(c,c+m,a)-c;
        if(lsg.query(0,uw)>0) {
            lsg.update(0,uw,-1);
            ans+=b;
        }
    }
    esc(ans);
}
accuracy: 1
time_ms: 70
memory_kb: 10780
final_rank: 14

code_snippet: #include <bits/stdc++.h>

using namespace std;
using i64 = int_fast64_t;
using db = long double;
using pii = pair<int, int>;
using pli = pair<int_fast64_t, int>;
using pdi = pair<double, int>;
template <class T> using mat = vector<vector<T>>;
template <class T> using minheap = priority_queue<T, vector<T>, greater<T>>;

#define each(i,v) for (auto i = begin(v); i != end(v); ++i)
#define reach(i,v) for (auto i = rbegin(v); i != rend(v); ++i)
#define urep(i,s,t) for (int i = (s); i <= (t); ++i)
#define drep(i,s,t) for (int i = (s); i >= (t); --i)
#define rep(i,n) urep(i, 0, (n)-1)
#define rep1(i,n) urep(i, 1, (n))
#define rrep(i,n) drep(i, (n)-1, 0)
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
#define fir first
#define sec second
#define front fro
#define back bac
#define vct vector
#define u_map unordered_map
#define u_set unordered_set
#define l_bnd lower_bound
#define u_bnd upper_bound
#define rsz resize
#define ers erase
#define emp emplace
#define emf emplace_front
#define emb emplace_back
#define pof pop_front
#define pob pop_back
#define mkp make_pair
#define mkt make_tuple
#define popcnt __builtin_popcount

struct setupper {
    setupper(uint_fast8_t prec) {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        cout << fixed << setprecision(prec);
#ifdef Local
        auto print_atexit = []() { cerr << "Exec time : " << clock() / (double)CLOCKS_PER_SEC * 1000.0 << "ms\n"; };
        atexit(print_atexit);
#endif
    }
} setuppre_obj(10);

namespace std {
    template <class T> void hash_combine(size_t &seed, T const &key) {
        seed ^= hash<T>()(key) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
    }
    template <class T, class U> struct hash<pair<T,U>> {
        size_t operator()(pair<T,U> const &pr) const
        {
            size_t seed = 0;
            hash_combine(seed,pr.first);
            hash_combine(seed,pr.second);
            return seed;
        }
    };
    template <class Tup, size_t index = tuple_size<Tup>::value - 1> struct hashval_calc {
        static void apply(size_t& seed, Tup const& tup) {
            hashval_calc<Tup, index - 1>::apply(seed, tup);
            hash_combine(seed,get<index>(tup));
        }
    };
    template <class Tup> struct hashval_calc<Tup,0> {
        static void apply(size_t& seed, Tup const& tup) {
            hash_combine(seed,get<0>(tup));
        }
    };
    template <class ...T> struct hash<tuple<T...>> {
        size_t operator()(tuple<T...> const& tup) const
        {
            size_t seed = 0;
            hashval_calc<tuple<T...>>::apply(seed,tup);
            return seed;
        }
    };
}

template <class T, class U> ostream& operator << (ostream& s, pair<T,U> p) { return s << p.fir << " " << p.sec; }
template <class T> ostream& operator << (ostream& s, vct<T> v) { each(i,v) { if(i != begin(v)) s << " "; s << *i; } return s; }
#define dump(...) cerr << " [ " << __LINE__ << " : " << __FUNCTION__ << " ] " << #__VA_ARGS__ << " : ";\
dump_func(__VA_ARGS__)
template <class T> void dump_func(T x) { cerr << x << '\n'; }
template <class T,class ...Rest> void dump_func(T x, Rest ... rest) { cerr << x << ","; dump_func(rest...); }
void read() {}
template <class T, class ...Rest> void read(T &x, Rest &... rest) { cin >> x; read(rest...); }
template <class T> void write(T x) { cout << x << '\n'; }
template <class T, class ...Rest> void write(T x, Rest ... rest) { cout << x << ' '; write(rest...); }
void writeln() {}
template <class T, class ...Rest> void writeln(T x, Rest ... rest) { cout << x << '\n'; writeln(rest...); }
#define esc(...) writeln(__VA_ARGS__), exit(0)
const auto add = [](auto &x, auto y) { x += y; };
const auto mul = [](auto &x, auto y) { x *= y; };
const auto lam_min = [](auto x, auto y) { return min(x, y); };
const auto lam_max = [](auto x, auto y) { return max(x, y); };
const auto chmax = [](auto &m, auto x) { if(m < x){ m = x; return true; } return false; };
const auto chmin = [](auto &m, auto x) { if(m > x){ m = x; return true; } return false; };
bool bit(i64 n, uint8_t e) { return (n >> e) & 1; }
i64 mask(i64 n, uint8_t e) { return n & ((1 << e) - 1); }
int ilog(uint64_t x, uint64_t b = 2) { return x ? 1 + ilog(x / b,b) : -1; }
template <class F> i64 binsr(i64 ok, i64 ng, const F &fn) {
    while (abs(ok - ng) > 1) {
        i64 mid = (ok + ng) / 2;
        (fn(mid) ? ok : ng) = mid;
    }
    return ok;
}
template <class A, size_t N, class T> void init(A (&array)[N], const T &val) { fill((T*)array,(T*)(array + N),val); }
template <class T> T cmprs(T &v) {
    T tmp = v, ret = v;
    sort(all(tmp));
    tmp.erase(unique(all(tmp)), end(tmp));
    each(i,ret) *i = l_bnd(all(tmp),*i) - begin(tmp);
    return ret;
}

template <class T> constexpr T inf = numeric_limits<T>::max() / 2 - 1;
constexpr int dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
constexpr int dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
constexpr long double gold = 1.618033988;
constexpr long double eps = 1e-15;
constexpr uint_fast64_t mod = 1e9 + 7;

template <class Monoid> struct LazySegtree {
    typedef function<Monoid(const Monoid&, const Monoid&)> opr_t;
    typedef function<void(Monoid&, const Monoid&)> lazy_opr_t;
    typedef function<void(Monoid&, const Monoid&, uint)> update_opr_t;
    const opr_t opr;
    const lazy_opr_t lazy_opr;
    const update_opr_t update_opr;
    const Monoid idel,lazy_idel;
    vector<Monoid> data,lazy;
    vector<bool> lazyflag;
    int range = 1;

    LazySegtree(int n, Monoid idel_ = inf<int>, const opr_t &opr_ = lam_min, const lazy_opr_t &lazy_opr_ = [](Monoid &x, Monoid y){x += y;}, const update_opr_t &update_opr_ = [](Monoid &x, Monoid y, unsigned int w){x += y;}, Monoid lazy_idel_ = 0) : opr(opr_),lazy_opr(lazy_opr_),update_opr(update_opr_),idel(idel_),lazy_idel(lazy_idel_)
    {
        while(n >= range) range <<= 1;
        data.assign(range << 1,idel);
        lazy.assign(range << 1,lazy_idel);
        lazyflag.assign(range << 1,false);
    }

    template <class P> void copy(P s, P t) {
        for(int i = range; s != t; ++s, ++i) data[i] = *s;
        for(int i = range - 1; i; --i) data[i] = opr(data[i * 2],data[i * 2 + 1]);
    }

    void eval(int k, int l, int r) {
        if(!lazyflag[k]) return;
        update_opr(data[k],lazy[k],r - l);
        if(r - l > 1) {
            lazy_opr(lazy[k * 2],lazy[k]);
            lazy_opr(lazy[k * 2 + 1],lazy[k]);
            lazyflag[k * 2] = lazyflag[k * 2 + 1] = true;
        }
        lazy[k] = lazy_idel;
        lazyflag[k] = false;
    }

    void update(int a, int b, Monoid val, int k = 1, int l = 0, int r = -1) {
        if(r < 0) r = range;
        eval(k,l,r);
        if(b <= l || r <= a) return;
        if(a <= l && r <= b) {
            lazy_opr(lazy[k],val);
            lazyflag[k] = true;
            eval(k,l,r);
        } else {
            update(a,b,val,k * 2,l,l + r >> 1);
            update(a,b,val,k * 2 + 1,l + r >> 1,r);
            data[k] = opr(data[k * 2],data[k * 2 + 1]);
        }
    }

    Monoid query(int a, int b, int k = 1, int l = 0, int r = -1) {
        if(r < 0) r = range;
        if(b <= l || r <= a) return idel;
        eval(k,l,r);
        if(a <= l && r <= b) return data[k];
        return opr(query(a,b,k * 2,l,l + r >> 1),query(a,b,k * 2 + 1,l + r >> 1,r));
    }
};


int n,m;
i64 ans;
int c[1<<17],d[1<<17];
set<pii> chs;

int main() {
    cin>>n>>m;
    rep(i,n) {
        int a,b;
        cin>>a>>b;
        chs.emp(b,a);
    }
    rep(i,m) cin>>c[i]>>d[i];
    LazySegtree<int> lsg(m);
    vct<int> tmp(m);
    rep(i,m) tmp[i]=--d[i];
    lsg.copy(d,d+m);
    reach(i,chs) {
        int a=i->sec,b=i->fir;
        int uw=u_bnd(c,c+m,a)-c;
        if(lsg.query(0,uw)>0) {
            lsg.update(0,uw,-1);
            ans+=b;
        }
    }
    esc(ans);
}
accuracy: 1
time_ms: 70
memory_kb: 11172
final_rank: 16

code_snippet: #include <bits/stdc++.h>

using namespace std;
using i64 = int_fast64_t;
using db = long double;
using pii = pair<int, int>;
using pli = pair<int_fast64_t, int>;
using pdi = pair<double, int>;
template <class T> using mat = vector<vector<T>>;
template <class T> using minheap = priority_queue<T, vector<T>, greater<T>>;

#define each(i,v) for (auto i = begin(v); i != end(v); ++i)
#define reach(i,v) for (auto i = rbegin(v); i != rend(v); ++i)
#define urep(i,s,t) for (int i = (s); i <= (t); ++i)
#define drep(i,s,t) for (int i = (s); i >= (t); --i)
#define rep(i,n) urep(i, 0, (n)-1)
#define rep1(i,n) urep(i, 1, (n))
#define rrep(i,n) drep(i, (n)-1, 0)
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
#define fir first
#define sec second
#define front fro
#define back bac
#define vct vector
#define u_map unordered_map
#define u_set unordered_set
#define l_bnd lower_bound
#define u_bnd upper_bound
#define rsz resize
#define ers erase
#define emp emplace
#define emf emplace_front
#define emb emplace_back
#define pof pop_front
#define pob pop_back
#define mkp make_pair
#define mkt make_tuple
#define popcnt __builtin_popcount

struct setupper {
    setupper(uint_fast8_t prec) {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        cout << fixed << setprecision(prec);
#ifdef Local
        auto print_atexit = []() { cerr << "Exec time : " << clock() / (double)CLOCKS_PER_SEC * 1000.0 << "ms\n"; };
        atexit(print_atexit);
#endif
    }
} setuppre_obj(10);

namespace std {
    template <class T> void hash_combine(size_t &seed, T const &key) {
        seed ^= hash<T>()(key) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
    }
    template <class T, class U> struct hash<pair<T,U>> {
        size_t operator()(pair<T,U> const &pr) const
        {
            size_t seed = 0;
            hash_combine(seed,pr.first);
            hash_combine(seed,pr.second);
            return seed;
        }
    };
    template <class Tup, size_t index = tuple_size<Tup>::value - 1> struct hashval_calc {
        static void apply(size_t& seed, Tup const& tup) {
            hashval_calc<Tup, index - 1>::apply(seed, tup);
            hash_combine(seed,get<index>(tup));
        }
    };
    template <class Tup> struct hashval_calc<Tup,0> {
        static void apply(size_t& seed, Tup const& tup) {
            hash_combine(seed,get<0>(tup));
        }
    };
    template <class ...T> struct hash<tuple<T...>> {
        size_t operator()(tuple<T...> const& tup) const
        {
            size_t seed = 0;
            hashval_calc<tuple<T...>>::apply(seed,tup);
            return seed;
        }
    };
}

template <class T, class U> ostream& operator << (ostream& s, pair<T,U> p) { return s << p.fir << " " << p.sec; }
template <class T> ostream& operator << (ostream& s, vct<T> v) { each(i,v) { if(i != begin(v)) s << " "; s << *i; } return s; }
#define dump(...) cerr << " [ " << __LINE__ << " : " << __FUNCTION__ << " ] " << #__VA_ARGS__ << " : ";\
dump_func(__VA_ARGS__)
template <class T> void dump_func(T x) { cerr << x << '\n'; }
template <class T,class ...Rest> void dump_func(T x, Rest ... rest) { cerr << x << ","; dump_func(rest...); }
void read() {}
template <class T, class ...Rest> void read(T &x, Rest &... rest) { cin >> x; read(rest...); }
template <class T> void write(T x) { cout << x << '\n'; }
template <class T, class ...Rest> void write(T x, Rest ... rest) { cout << x << ' '; write(rest...); }
void writeln() {}
template <class T, class ...Rest> void writeln(T x, Rest ... rest) { cout << x << '\n'; writeln(rest...); }
#define esc(...) writeln(__VA_ARGS__), exit(0)
const auto add = [](auto &x, auto y) { x += y; };
const auto mul = [](auto &x, auto y) { x *= y; };
const auto lam_min = [](auto x, auto y) { return min(x, y); };
const auto lam_max = [](auto x, auto y) { return max(x, y); };
const auto chmax = [](auto &m, auto x) { if(m < x){ m = x; return true; } return false; };
const auto chmin = [](auto &m, auto x) { if(m > x){ m = x; return true; } return false; };
bool bit(i64 n, uint8_t e) { return (n >> e) & 1; }
i64 mask(i64 n, uint8_t e) { return n & ((1 << e) - 1); }
int ilog(uint64_t x, uint64_t b = 2) { return x ? 1 + ilog(x / b,b) : -1; }
template <class F> i64 binsr(i64 ok, i64 ng, const F &fn) {
    while (abs(ok - ng) > 1) {
        i64 mid = (ok + ng) / 2;
        (fn(mid) ? ok : ng) = mid;
    }
    return ok;
}
template <class A, size_t N, class T> void init(A (&array)[N], const T &val) { fill((T*)array,(T*)(array + N),val); }
template <class T> T cmprs(T &v) {
    T tmp = v, ret = v;
    sort(all(tmp));
    tmp.erase(unique(all(tmp)), end(tmp));
    each(i,ret) *i = l_bnd(all(tmp),*i) - begin(tmp);
    return ret;
}

template <class T> constexpr T inf = numeric_limits<T>::max() / 2 - 1;
constexpr int dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
constexpr int dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
constexpr long double gold = 1.618033988;
constexpr long double eps = 1e-15;
constexpr uint_fast64_t mod = 1e9 + 7;

template <class Monoid> struct LazySegtree {
    typedef function<Monoid(const Monoid&, const Monoid&)> opr_t;
    typedef function<void(Monoid&, const Monoid&)> lazy_opr_t;
    typedef function<void(Monoid&, const Monoid&, uint)> update_opr_t;
    const opr_t opr;
    const lazy_opr_t lazy_opr;
    const update_opr_t update_opr;
    const Monoid idel,lazy_idel;
    vector<Monoid> data,lazy;
    vector<bool> lazyflag;
    int range = 1;

    LazySegtree(int n, Monoid idel_ = inf<int>, const opr_t &opr_ = lam_min, const lazy_opr_t &lazy_opr_ = [](Monoid &x, Monoid y){x += y;}, const update_opr_t &update_opr_ = [](Monoid &x, Monoid y, unsigned int w){x += y;}, Monoid lazy_idel_ = 0) : opr(opr_),lazy_opr(lazy_opr_),update_opr(update_opr_),idel(idel_),lazy_idel(lazy_idel_)
    {
        while(n >= range) range <<= 1;
        data.assign(range << 1,idel);
        lazy.assign(range << 1,lazy_idel);
        lazyflag.assign(range << 1,false);
    }

    void copy(const vector<Monoid> &v) {
        for(int i = 0; i < v.size(); ++i) data[i + range] = v[i];
        for(int i = range - 1; i; --i) data[i] = opr(data[i * 2],data[i * 2 + 1]);
    }

    void eval(int k, int l, int r) {
        if(!lazyflag[k]) return;
        update_opr(data[k],lazy[k],r - l);
        if(r - l > 1) {
            lazy_opr(lazy[k * 2],lazy[k]);
            lazy_opr(lazy[k * 2 + 1],lazy[k]);
            lazyflag[k * 2] = lazyflag[k * 2 + 1] = true;
        }
        lazy[k] = lazy_idel;
        lazyflag[k] = false;
    }

    void update(int a, int b, Monoid val, int k = 1, int l = 0, int r = -1) {
        if(r < 0) r = range;
        eval(k,l,r);
        if(b <= l || r <= a) return;
        if(a <= l && r <= b) {
            lazy_opr(lazy[k],val);
            lazyflag[k] = true;
            eval(k,l,r);
        } else {
            update(a,b,val,k * 2,l,l + r >> 1);
            update(a,b,val,k * 2 + 1,l + r >> 1,r);
            data[k] = opr(data[k * 2],data[k * 2 + 1]);
        }
    }

    Monoid query(int a, int b, int k = 1, int l = 0, int r = -1) {
        if(r < 0) r = range;
        if(b <= l || r <= a) return idel;
        eval(k,l,r);
        if(a <= l && r <= b) return data[k];
        return opr(query(a,b,k * 2,l,l + r >> 1),query(a,b,k * 2 + 1,l + r >> 1,r));
    }
};


int n,m;
i64 ans;
int c[1<<17],d[1<<17];
set<pii> chs;

int main() {
    cin>>n>>m;
    rep(i,n) {
        int a,b;
        cin>>a>>b;
        chs.emp(b,a);
    }
    rep(i,m) cin>>c[i]>>d[i];
    LazySegtree<int> lsg(m);
    vct<int> tmp(m);
    rep(i,m) tmp[i]=d[i]-1;
    lsg.copy(tmp);
    reach(i,chs) {
        int a=i->sec,b=i->fir;
        int uw=u_bnd(c,c+m,a)-c;
        if(lsg.query(0,uw)>0) {
            lsg.update(0,uw,-1);
            ans+=b;
        }
    }
    esc(ans);
}
accuracy: 1
time_ms: 70
memory_kb: 11168
final_rank: 15

code_snippet: # include <iostream>
# include <algorithm>
#include <array>
# include <cassert>
#include <cctype>
#include <climits>
#include <numeric>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <tuple>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <bitset>
# include <complex>
# include <chrono>
# include <random>
# include <limits.h>
# include <unordered_map>
# include <unordered_set>
# include <deque>
# include <cstdio>
# include <cstring>
#include <stdio.h>
#include<time.h>
#include <stdlib.h>
#include <cstdint>
#include <cfenv>
#include<fstream>
//#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD = 1000000000 + 7; //924844033 1000000000 + 7;
constexpr long long INF = numeric_limits<LL>::max();
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr<<#x<<": "<<x<<'\n'
typedef pair<LL, LL> Pll;
typedef pair<double, double> Dll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll { LL first, second, third; };
struct Fll { LL first, second, third, fourth; };
typedef pair<LL, Tll> Ptll;
#define rep(i,rept) for(LL i=0;i<rept;i++)
#define Rrep(i,mf) for(LL i=mf-1;i>=0;i--)
LL h, w, n, m, k, t, s, p, q, last, first, cnt, sum, ans, dp[200000];
Pll a[200000], b[200000];
string str, ss;
bool f;
char c;
int di[4][2] = { { 0,1 },{ 1,0 },{ 0,-1 },{ -1,0 } };
struct Edge { LL to, cost; };
struct edge { LL from, to, cost; };
vector<vector<Edge>>vec;
vector<edge>ed;
vector<LL>v;
map<LL,LL>ma;
set<LL>st;
void YN(bool f) {
	if (f)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
}
void yn(bool f) {
	if (f)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;
}

int main() {
	cin >> n >> m;
	rep(i, n) {
		cin >> a[i].fir >> a[i].sec;
	}
	last = INF;
	rep(i, m) {
		cin >> b[i].fir >> b[i].sec;
		if (last <= b[i].sec) {
			i--, m--;
		}
		else last = b[i].sec;
	}
	rep(i, n) {
		if (a[i].fir < b[0].fir) {
			ans += a[i].sec;
			first = i+1;
		}
		else break;
	}
	b[m].first = INF;
	priority_queue<LL>pq;
	rep(i, m) {
		for (int j = first; j < n; j++) {
			if (b[i + 1].fir > a[j].fir) {
				pq.push(a[j].sec);
				first = j + 1;
			}
			else break;
		}
		for (int j = 0; j < (i+1==m?b[i].sec-1:b[i ].sec - b[i+ 1].sec); j++) {
			if (pq.empty())break;
			ans += pq.top();
			pq.pop();
		}
	}
	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 5504
final_rank: 10