Problem:
Score : 400 points

Problem Statement

There are N one-off jobs available. If you take the i-th job and complete it, you will earn the reward of B_i after A_i days from the day you do it.

You can take and complete at most one of these jobs in a day.

However, you cannot retake a job that you have already done.

Find the maximum total reward that you can earn no later than M days from today.

You can already start working today.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i \leq 10^5

1 \leq B_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the maximum total reward that you can earn no later than M days from today.

Sample Input 1

3 4
4 3
4 1
2 2

Sample Output 1

5

You can earn the total reward of 5 by taking the jobs as follows:

Take and complete the first job today. You will earn the reward of 3 after four days from today.

Take and complete the third job tomorrow. You will earn the reward of 2 after two days from tomorrow, that is, after three days from today.

Sample Input 2

5 3
1 2
1 3
1 4
2 1
2 3

Sample Output 2

10

Sample Input 3

1 1
2 1

Sample Output 3

0

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vb = vector<bool>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using vpll = vector<pll>;
const ll LINF = 1ll << 55;
const ll INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
const int MAXN = 1e5 + 10;
const ll dx[] = {1, 0, -1, 0};
const ll dy[] = {0, 1, 0, -1};


/// cin/cout overloading
template<typename T>
ostream& operator<< (ostream& out, vector<T>& vec) {
    for (auto it = vec.begin(); it != vec.end(); ++it) {
        out << *it << " ";
    }
    return out;
}

template<typename T>
ostream& operator<< (ostream& out, pair<T, T>& P) {
    out << P.first << " " << P.second;
    return out;
}

template<typename T>
istream& operator>> (istream& in, vector<T>& vec) {
    for (auto it = vec.begin(); it != vec.end(); ++it) {
        in >> *it;
    }
    return in;
}

template<typename T>
istream& operator>> (istream& in, pair<T, T>& P) {
    in >> P.first >> P.second;
    return in;
}

/// 图相关
// ll N, M;

struct Edge {
    int to;
    ll weight;
    int next;
    bool operator<(const Edge& rhs) {
        return weight < rhs.weight;
    }
};

struct Edge2 {
    ll u, v, w;
    Edge2(ll u_, ll v_, ll w_) : u(u_), v(v_), w(w_) {}
    Edge2() : u(0ll), v(0ll), w(0ll) {}
    bool operator<(const Edge2& rhs) {
        return w < rhs.w;
    }
};

// vector<Edge> edges(MAXN << 1);
// vi head(MAXN, -1);
// vector<int> matchingx(MAXN, -1);
// vector<int> matchingy(MAXN, -1);
// vector<bool> check(MAXN);
// vector<ll> dis(MAXN);
// vector<bool> vis(MAXN, false);

// int cnt = 1;

// void addEdge(int from, int to, ll weight) {
//     edges[cnt].to = to;
//     edges[cnt].weight = weight;
//     edges[cnt].next = head[from];
//     head[from] = cnt++;
// }
// 
//bool dfs(int u) {
//    for (int i = head[u]; i != -1; i = edges[i].next) {
//        int v = edges[i].to;
//        if (!check[v]) {
//            check[v] = true;
//            if (matchingy[v] == -1 || dfs(matchingy[v])) {
//                matchingy[v] = u;
//                matchingx[u] = v;
//                return true;
//            }
//        }
//    }
//    return false;
//}
//
//int hungarian() {
//    int ans = 0;
//    fill(matchingx.begin(), matchingx.end(), -1);
//    fill(matchingy.begin(), matchingy.end(), -1);
//    for (int u = 1; u <= N; ++u) {
////        if (matchingx[u] == -1) {
//        {
//            fill(check.begin(), check.end(), false);
//            if (dfs(u)) {
//                ++ans;
//            }
//        }
//    }
//    return ans;
//}
//
//void dijkstra(const ll s) {
//    priority_queue<P, vector<P>, greater<P>> que;
//    fill(dis.begin(), dis.end(), INF);
//    dis[s] = 0;
//    que.push(P(0, s));
//////multiple sources
////    for (auto& x : shops) {
////        dis[x] = 0;
////        que.push(P(0, x));
////    }
//    while (!que.empty()) {
//        P p = que.top();
//        que.pop();
////        cout << "pop " << p.second << endl;
//        int u = p.second;
//        if (dis[u] < p.first) continue;
//        for (int i = head[u]; i != -1; i = edges[i].next) {
//            int v = edges[i].to;
//            if (dis[v] > dis[u] + edges[i].weight) {
//                dis[v] = dis[u] + edges[i].weight;
////                cout << "push " << v << endl;
//                que.push(P(dis[v], v));
//            }
//        }
//    }
//}

//void zeroOneBFS(const int s) {
//    deque<P> que;
//    fill(dis.begin(), dis.end(), INF);
//    dis[s] = 0;
//    que.push_front(P(0, s));
//    while (!que.empty()) {
//        P p = que.front();
//        que.pop_front();
//        int u = p.second;
//        if (dis[u] < p.first) continue;
//        for (int i = head[u]; i != -1; i = edges[i].next) {
//            int v = edges[i].to;
//            if (dis[v] > dis[u] + edges[i].weight) {
//                dis[v] = dis[u] + edges[i].weight;
//                if (edges[i].weight) {
//                    que.push_back(P(dis[v], v));
//                } else {
//                    que.push_front(P(dis[v], v));
//                }
//            }
//        }
//    }
//}

// Union-Find 并查集
class UnionFind {
    vector<ll> par;
public:
    explicit UnionFind(ll n) : par(n, -1) {}

    ll root(ll a) {
        if (par[a] < 0) {
            return a;
        }
        return par[a] = root(par[a]);
    }

    ll size(ll a) {
        return -par[root(a)];
    }

    void unite(ll a, ll b) {
        a = root(a);
        b = root(b);
        if (a != b) {
            if (size(a) < size(b)) {
                swap(a, b);
            }
            par[a] += par[b];
            par[b] = a;
        }
    }
};

ll kruskal(vector<Edge2>& edges2, const ll V) {
    sort(edges2.begin(), edges2.end());
    UnionFind uf(V + 10);
    ll res = 0;
    Edge2 e;
    for (int i = 0; i < edges2.size(); ++i) {
        e = edges2[i];
        if (uf.root(e.u) != uf.root(e.v)) {
            uf.unite(e.u, e.v);
            res += e.w;
        }
    }
    return res;
}

// 线段树
struct SegmentTreeNode {
    ll maxVal;
    ll minVal;
    ll sum;
    ll len;
    ll lazy;
    ll left, right;
    SegmentTreeNode() {}
};

class SegmentTree {
public:
    explicit SegmentTree(size_t size, const vll& nums) : tree(size << 2), nums(nums) {

    }
    void build(ll root, ll left, ll right) {
        tree[root].lazy = 0;
        tree[root].left = left;
        tree[root].right = right;
        tree[root].len = right - left + 1;
        if (left == right) {
            tree[root].maxVal = nums[left];
            tree[root].minVal = nums[left];
            tree[root].sum = nums[left];
        } else {
            ll mid = (right - left) / 2 + left;
            build(root * 2, left, mid);
            build(root * 2 + 1, mid + 1, right);
            tree[root].minVal = min(tree[root * 2].minVal, tree[root * 2 + 1].minVal);
            tree[root].maxVal = max(tree[root * 2].maxVal, tree[root * 2 + 1].maxVal);
            tree[root].sum = tree[root * 2].sum + tree[root * 2 + 1].sum;
        }
    }

    void pushup(ll root) {
        tree[root].minVal = min(tree[root * 2].minVal, tree[root * 2 + 1].minVal);
        tree[root].maxVal = max(tree[root * 2].maxVal, tree[root * 2 + 1].maxVal);
        tree[root].sum = tree[root * 2].sum + tree[root * 2 + 1].sum;
    }
    //// add single node val
    void add(ll root, ll id, ll addVal) {
        if (tree[root].left == tree[root].right) {
            tree[root].maxVal += addVal;
            tree[root].minVal += addVal;
            tree[root].sum += addVal;
            return;
        }
        ll mid = (tree[root].right - tree[root].left) / 2 + tree[root].left;
        if (id <= mid) {
            add(root * 2, id, addVal);
        } else {
            add(root * 2 + 1, id, addVal);
        }
        pushup(root);
    }

    void pushdown(ll root) {
        if (tree[root].lazy) {
            tree[root * 2].lazy += tree[root].lazy;
            tree[root * 2 + 1].lazy += tree[root].lazy;
            tree[root * 2].sum += tree[root * 2].len * tree[root].lazy;
            tree[root * 2 + 1].sum += tree[root * 2 + 1].len * tree[root].lazy;
            tree[root * 2].maxVal += tree[root].lazy;
            tree[root * 2 + 1].maxVal += tree[root].lazy;
            tree[root * 2].minVal += tree[root].lazy;
            tree[root * 2 + 1].minVal += tree[root].lazy;
            tree[root].lazy = 0;
        }
    }
    //// query range sum
    ll querySum(ll root, ll left, ll right) {
        if (tree[root].left >= left && tree[root].right <= right) {
            return tree[root].sum;
        }
        if (tree[root].left > right || tree[root].right < left) {
            return 0;
        }
        if (tree[root].lazy) {
            pushdown(root);
        }
        return querySum(root * 2, left, right) + querySum(root * 2 + 1, left, right);
    }
    //// query range max/min
    ll queryMax(ll root, ll left, ll right) {
        if (tree[root].left >= left && tree[root].right <= right) {
            return tree[root].maxVal;
        }
        if (tree[root].left > right || tree[root].right < left) {
            return -INF;
        }
        if (tree[root].lazy) {
            pushdown(root);
        }
        return max(queryMax(root * 2, left, right), queryMax(root * 2 + 1, left, right));
    }

    ll queryMin(ll root, ll left, ll right) {
        if (tree[root].left >= left && tree[root].right <= right) {
            return tree[root].minVal;
        }
        if (tree[root].left > right || tree[root].right < left) {
            return INF;
        }
        if (tree[root].lazy) {
            pushdown(root);
        }
        return min(queryMin(root * 2, left, right), queryMin(root * 2 + 1, left, right));
    }
    //// add range val
    void update(ll root, ll left, ll right, ll addVal) {
        if (tree[root].left >= left && tree[root].right <= right) {
            tree[root].lazy += addVal;
            tree[root].sum += tree[root].len * addVal;
            tree[root].maxVal += addVal;
            tree[root].minVal += addVal;
            return;
        }
        if (tree[root].left > right || tree[root].right < left) {
            return;
        }
        if (tree[root].lazy) {
            pushdown(root);
        }
        update(root * 2, left, right, addVal);
        update(root * 2 + 1, left, right, addVal);
        pushup(root);
    }

private:
    vector<SegmentTreeNode> tree;
    const vll &nums;
};

/// 组合数

////約数求める //約数
//void divisor(ll n, vector<ll> &ret) {
//    for (ll i = 1; i * i <= n; i++) {
//        if (n % i == 0) {
//            ret.push_back(i);
//            if (i * i != n) ret.push_back(n / i);
//        }
//    }
//    sort(ret.begin(), ret.end());
//}
////階乗
ll factorial(ll n) {
    ll ret = 1;
    for (ll i = 1; i <= n; ++i) {
        ret = (ret * i) % MOD;
    }
    return ret;
}
////モジュラ逆数
ll inv_mod(ll n) {
    ll a = n % MOD, b = MOD - 2, ret = 1;
    while (b > 0) {
        if (b & 1) ret = (ret * a) % MOD;
        a = (a * a) % MOD;
        b >>= 1;
    }
    return ret;
}

ll nPr(ll n, ll r) {
    ll ret = 1;
    for (ll i = n; i >= n - r + 1; --i) {
        ret = ret * (i % MOD) % MOD;
    }
    return ret;
}

ll nCr(ll n, ll r) {
    return nPr(n, r) * inv_mod(factorial(r)) % MOD;
}
//
//vll F(MAXN), Finv(MAXN), inv(MAXN);
//
//ll pow_mod(ll a, ll b, ll p) {
//    ll ret = 1;
//    while (b) {
//        if (b & 1) ret = (ret * a) % p;
//        a = (a * a) % p;
//        b >>= 1;
//    }
//    return ret;
//}
//
//void comb_init() {
//    inv[1] = 1;
//    for (int i = 2; i < MAXN; ++i) {
//        inv[i] = (MOD - MOD / i) * 1ll * inv[MOD % i] % MOD;
//    }
//    F[0] = Finv[0] = 1;
//    for (int i = 1; i < MAXN; ++i) {
//        F[i] = F[i-1] * 1ll * i % MOD;
//        Finv[i] = Finv[i-1] * 1ll * inv[i] % MOD;
//    }
//}
//
//inline ll comb(ll n, ll m) {
//    if (m < 0 || m > n) return 0;
//    return F[n] * 1ll * Finv[n - m] % MOD * Finv[m] % MOD;
// }
//
inline ll add_mod(ll a, ll b) {
    return (a + b) % MOD;
}

inline ll mul_mod(ll a, ll b) {
    return a * b % MOD;
}

inline ll sub_mod(ll a, ll b) {
    return (a + MOD - b) % MOD;
}

/// main函数
int main() {
    int N, M;
    cin >> N >> M;
    vector<vi> loc(M + 1);
    int A, B;
    for (int i = 0; i < N; ++i) {
        cin >> A >> B;
        if (M - A < 0) continue;
        loc[M - A].push_back(B);
    }
    priority_queue<int> pq;
    ll res = 0;
    for (int i = M; i >= 0; --i) {
        for (auto& x : loc[i]) {
            pq.push(x);
        }
        if (!pq.empty()) {
            res += pq.top();
            pq.pop();
        }
    }
    cout << res << endl;
    return 0;
}