CSES - Shared codeLink to this code: https://cses.fi/paste/484b20df3ff9e0aa4ada9e/
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#define int long long
using namespace std;

#define ssize(x) (int)(x).size()

signed main() {
	cin.tie(0)->sync_with_stdio(0);
	int p, a, n;
	cin >> p >> a >> n;
	vector<int> prog(n), art(n);
	for (int i = 0; i < n; i++)
		cin >> prog[i] >> art[i];
	auto eval = [&](auto p_cost, auto a_cost) -> tuple<long long, int, int> {
		long long ans = 0, take_p = 0, take_a = 0;
		for (int i = 0; i < n; i++) {
			if (prog[i] - p_cost <= 0 && art[i] - a_cost <= 0)
				continue;
			if (prog[i] - p_cost > art[i] - a_cost) {
				ans += prog[i] - p_cost;
				take_p++;
			} else {
				ans += art[i] - a_cost;
				take_a++;
			}
		}
		return {ans, take_p, take_a};
	};
	long long p_cost = 1 << 30, a_cost = 1 << 30;
	for (int p_dif = 30; p_dif >= 0; p_dif--) {
		a_cost = 1 << 30;
		int p_mid = p_cost - (1ll << p_dif);
		for (int a_dif = 30; a_dif >= 0; a_dif--) {
			int a_mid = a_cost - (1ll << a_dif);
			auto [_, take_p, take_a] = eval(p_mid, a_mid);
			if (take_a <= a)
				a_cost = a_mid;
		}
		auto [_, take_p, take_a] = eval(p_mid, a_cost);
		if (take_p <= p)
			p_cost = p_mid;
	}
	a_cost = 1 << 30;
	for (int a_dif = 30; a_dif >= 0; a_dif--) {
		int a_mid = a_cost - (1ll << a_dif);
		auto [_, take_p, take_a] = eval(p_cost, a_mid);
		if (take_a <= a)
			a_cost = a_mid;
	}
	auto [ans, take_p, take_a] = eval(p_cost, a_cost);
	cout << ans + p * p_cost + a * a_cost << '\n';
	return 0;
}