Language: C++

//#include "SaveFuncs.cpp"

#include <iostream>
#include <array>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <iomanip>	//cout << fixed << setprecision(桁数);
#include <stdexcept>

using namespace std;
using ll = long long;

#define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; i++)
#define rep1(i, n) for(int i=1, i##_len=(n); i<i##_len; i++)
#define rrep(i, n) for(int i=(n)-1; i >= 0; i--)
#define rrep1(i, n) for(int i=(n); i > 0; i--)
#define all(x) (x).begin(), (x).end()
#define pair(a, b) make_pair(a, b);
//#define int long long
constexpr int INF = 2000000100;
constexpr int DIV = 1000000007;	// 10e9+7
template<class T> bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> T divUp(T a, T b) { if (a % b == 0) { return a / b; } return a / b + 1; }
std::string strMulti(std::string t, int n) { std::string out = ""; for (int i = 0; i < n; i++) { out += t; } return out; }
template<class T> T math_P(T m, T n) { T ret = 1; for (T i = m; i > m - n; i--) { ret *= i; } return ret; }
template<class T> T math_C(T m, T n) { T ret = mathP(m, n); for (T i = 2; i <= n; i++) { ret /= i; } return ret; }
bool compare_by_b(pair<int, int> a, pair<int, int> b) { if (a.second != b.second) { return a.second < b.second; } return a.first < b.first; }

/*****/

int Main()
{
	int N, M;
	cin >> N >> M;
	vector<pair<int, int>> A(N);
	rep(i, N)
	{
		int a, b;
		cin >> a >> b;
		A.at(i) = pair(a, b);
	}

	sort(all(A), compare_by_b);
	reverse(all(A));

	coutendl();
	rep(i, N)
		debug_headCout(A.at(i).first, A.at(i).second);
	coutendl();

	vector<int> P(M, -1), PEnd(M + 1);
	int remain = M;
	int ret = 0;

	rep(i, M)
	{
		PEnd.at(i) = i;
	}

	rep(i, N)
	{
		if (remain == 0)
			break;

		if (A.at(i).first >= M + 1)
			continue;

		bool ok = true;
		for (int j = PEnd.at(A.at(i).first - 1); j < M; j++)
		{
			if (P.at(j) == -1)
			{
				ret += A.at(i).second;
				P.at(j) = i;
				remain--;
				PEnd.at(A.at(i).first) = j + 1;
				ok = false;
				break;
			}
		}
		if (ok)
		{
			PEnd.at(A.at(i).first) = M;
		}
	}

	rep(i, M)
	{
		debug_headCout(i, P.at(i));
		if (P.at(i) != -1)
			debug_tabHeadCout(1, "A", A.at(P.at(i)).second);
	}

	cout << ret;

	return 0;
}

/*****/

int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);

	Main();

	return 0;
}


Compiler output:
a.cc: In function 'int Main()':
a.cc:52:9: error: 'coutendl' was not declared in this scope; did you mean 'roundl'?
   52 |         coutendl();
      |         ^~~~~~~~
      |         roundl
a.cc:54:17: error: 'debug_headCout' was not declared in this scope
   54 |                 debug_headCout(A.at(i).first, A.at(i).second);
      |                 ^~~~~~~~~~~~~~
a.cc:95:17: error: 'debug_headCout' was not declared in this scope
   95 |                 debug_headCout(i, P.at(i));
      |                 ^~~~~~~~~~~~~~
a.cc:97:25: error: 'debug_tabHeadCout' was not declared in this scope
   97 |                         debug_tabHeadCout(1, "A", A.at(P.at(i)).second);
      |                         ^~~~~~~~~~~~~~~~~