Language: C++

//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>

#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

#include <stdexcept>

using namespace std;

//conversion
//------------------------------------------
inline int toint(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class t> inline string tostring(t x) { ostringstream sout; sout << x; return sout.str(); }

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl
#define debug(x) cerr << #x << " = " << (x) << " (l" << __line__ << ")" << " " << __file__ << endl

// type alias
using ll = long long;
using ull = unsigned long long;

// constant value
const ll mod = 1000000007ll;

template<class ll>
inline ll getCeilExponent(ll a)
{
	if (a <= 0)
	{
		throw invalid_argument("vaule equal to or less than 0 should not be valid");
	}

	ll index = 0;
	--a;
	for (; a != 0; a >>= 1)
	{
		++index;
	}
	return index;
}

template<class ll>
inline ll getFloorExponent(ll a)
{
	if (a <= 0)
	{
		throw invalid_argument("vaule equal to or less than 0 should not be valid");
	}

	ll index = 0;
	a >>= 1;
	for (; a != 0; a >>= 1)
	{
		++index;
	}
	return index;
}

// computational complexity: o(log(max(a, b))) 
template<class ll>
inline ll getGcd(ll a, ll b)
{
	if (b == 0)
	{
		return a;
	}
	return getGcd(b, a % b);
}

// computational complexity: o(log(max(a, b))) 
template<class ll>
inline pair<ll, ll> getBezoutsIdentitySolution(ll a, ll b)
{
	if (b == 0)
	{
		return { 1, 0 };
	}
	auto sol = getBezoutsIdentitySolution(b, a % b);
	return { sol.second, sol.first - (a / b) * sol.second };
}

inline ll getModValue(ll a, ll m)
{
	return (a % m + m) % m;
}

// computational complexity: o(log(max(a, n))) 
inline ll getModInverse(ll a, ll m)
{
	auto sol = getBezoutsIdentitySolution(a, m);
	if (a * sol.first + m * sol.second != 1)
	{
		return -1;
	}
	return getModValue(sol.first, m);
}

ll getModPower(ll base, unsigned t, ll m)
{
	if (t == 0)
	{
		return 1ll;
	}

	auto res = getModPower(base, t >> 1, m);
	res = getModValue(res * res, m);
	if (t % 2 == 1)
	{
		res = getModValue(res * base, m);
	}
	return res;
}

class ModCombCalculator
{
private:
	ll n_;
	ll mod_;
	vector<ll> values_;
public:
	ModCombCalculator(ll n, ll mod) : n_(n), values_(n + 1LL, -1LL), mod_(mod)
	{
		values_[0] = 1LL;
	}
	
	ll calculate(ll r)
	{
		if (r < 0LL || n_ < r)
		{
			return 0LL;
		}

		if (values_[r] != -1)
		{
			return values_[r];
		}

		ll res;
		if (values_[r - 1] != -1)
		{
			res = values_[r - 1];
		}
		else
		{
			res = calculate(r - 1);
		}

		values_[r] = getModValue(
			getModValue(res * (n_ - r + 1LL), mod_) * getModInverse(r, mod_),
		mod_);

		return values_[r];
	}
};

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

	ll N, M;
	cin >> N >>M;

	vector<ll> As(N);
	for (ll i = 0; i < N; ++i)
	{
		cin >> As[i];
	}
	sort(As.begin(), As.end());

	vector<pair<ll, ll>> BCs(M);
	ll B, C;
	for (ll i = 0; i < M; ++i)
	{
		cin >> B >> C;
		BCs[i] = make_pair(B, C);
	}

	sort(BCs.begin(), BCs.end(), [](const auto& l, const auto& r) { return l.second > r.second; });

	ll res = 0LL;
	auto cur_iter = As.begin();
	for (ll i = 0; i < M; ++i)
	{
		auto s = lower_bound(cur_iter, As.end(), BCs[i].second);
		auto t = distance(cur_iter, s);
		auto r = min(t, BCs[i].first);
		cur_iter += r;
		res += BCs[i].second * r;
	}
	res = accumulate(cur_iter, As.end(), res);

	cout << res << endl;

	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:209:29: error: no matching function for call to 'min(long int&, long long int&)'
  209 |                 auto r = min(t, BCs[i].first);
      |                          ~~~^~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/vector:62,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:209:29: note:   deduced conflicting types for parameter 'const _Tp' ('long int' and 'long long int')
  209 |                 auto r = min(t, BCs[i].first);
      |                          ~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:11:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:209:29: note:   mismatched types 'std::initializer_list<_Tp>' and 'long int'
  209 |                 auto r = min(t, BCs[i].first);
      |                          ~~~^~~~~~~~~~~~~~~~~