Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() (int, error) {
	return strconv.Atoi(nextString())
}

func main() {
	scanner.Split(bufio.ScanWords)

	n, _ := nextInt()
	m, _ := nextInt()

	arr := make([][]int, m)
	for i := range arr {
		a, _ := nextInt()
		b, _ := nextInt()
		arr[i] = []int{a, b}
	}

	sort.Slice(arr, func(i, j int) bool { return arr[i][0] > arr[j][0] })
	//	fmt.Println(arr)

	nGot := 0
	spent := 0
	for i := range arr {
		a := arr[i][0]
		b := arr[i][1]
		if a >= n {
			nGot++
		} else if a+b >= n {
			spent += n - a
			nGot++
		}
		if nGot >= m-1 {
			break
		}
	}
	fmt.Println(spent)
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<int ,P> P3;
typedef pair<P ,P> PP;
const ll MOD = ll(1e9+7);
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(1e5 + 5);
const double EPS = 1e-6;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define ALL(v) (v).begin(), (v).end()

int main() {
    int n, m, ans=0;
    vector<int> v;
    cin >> n >> m;
    REP(i,m){
        int a, b;
        cin >> a >> b;
        v.push_back(max(0,n-a));
    }
    SORT(v);
    REP(i,m-1){
        ans += v[i];
    }
    cout << ans << endl;
    return 0;
}

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

using namespace std;

#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define repr(i, j) for(int i = j; i >= 0; --i)
#define INF (1 << 30)
#define MOD 1e9 + 7

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

int N, M;

int main() {
	scanf("%d %d", &N, &M);
	int ans = 0, maxi = 0;
	rep(i, M) {
		int A, B;
		scanf("%d %d", &A, &B);
		if(A < N) {
			ans += N - A;
			maxi = max(N - A, maxi);
		}
	}
	printf("%d\n", ans - maxi);
	return 0;
}

C++: // #pragma GCC target("avx")  // CPU 処理並列化
// #pragma GCC optimize("O3")  // CPU 処理並列化
// #pragma GCC optimize("unroll-loops")  // 条件処理の呼び出しを減らす
// #define BEGIN_STACK_EXTEND(size) void * stack_extend_memory_ = malloc(size);void * stack_extend_origin_memory_;char * stack_extend_dummy_memory_ = (char*)alloca((1+(int)(((long long)stack_extend_memory_)&127))*16);*stack_extend_dummy_memory_ = 0;asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp":"=b"(stack_extend_origin_memory_):"a"((char*)stack_extend_memory_+(size)-1024));
// #define END_STACK_EXTEND asm volatile("mov %%rax, %%rsp"::"a"(stack_extend_origin_memory_));free(stack_extend_memory_);

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<deque>
#include<stack>
#include<string>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#include<stdlib.h>
#include<cassert>
#include<time.h>
#include<bitset>
#include<numeric>
#include<unordered_set>
#include<unordered_map>
#include<complex>
using namespace std;
const long long mod=1000000007;
const long long inf=mod*mod;
const long long d2=(mod+1)/2;
const long double EPS=1e-20;
const double INF=1e+10;
const double PI=acos(-1.0);
const int C_SIZE = 3100000;
const int UF_SIZE = 3100000;
namespace{
	long long fact[C_SIZE];
	long long finv[C_SIZE];
	long long inv[C_SIZE];
	long long Comb(int a,int b){
	 	if(a<b||b<0)return 0;
	 	return fact[a]*finv[b]%mod*finv[a-b]%mod;
	}
	void init_C(int n){
		fact[0]=finv[0]=inv[1]=1;
		for(int i=2;i<n;i++){
			inv[i]=(mod-(mod/i)*inv[mod%i]%mod)%mod;
		}
		for(int i=1;i<n;i++){
			fact[i]=fact[i-1]*i%mod;
			finv[i]=finv[i-1]*inv[i]%mod;
		}
	}
	long long pw(long long a,long long b){
		if(a<0LL)return 0;
		if(b<0LL)return 0;
		long long ret=1;
		while(b){
			if(b%2)ret=ret*a%mod;
			a=a*a%mod;
			b/=2;
		}
		return ret;
	}
	long long pw_mod(long long a,long long b,long long M){
		if(a<0LL)return 0;
		if(b<0LL)return 0;
		long long ret=1;
		while(b){
			if(b%2)ret=ret*a%M;
			a=a*a%M;
			b/=2;
		}
		return ret;
	}
	int pw_mod_int(int a,int b,int M){
		if(a<0)return 0;
		if(b<0)return 0;
		int ret=1;
		while(b){
			if(b%2)ret=(long long)ret*a%M;
			a=(long long)a*a%M;
			b/=2;
		}
		return ret;
	}
	int ABS(int a){return max(a,-a);}
	long long ABS(long long a){return max(a,-a);}
	double ABS(double a){return max(a,-a);}
	int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
	int UF[UF_SIZE];
	void init_UF(int n){
		for(int i=0;i<n;i++)UF[i]=-1;
	}
	int FIND(int a){
		if(UF[a]<0)return a;
		return UF[a]=FIND(UF[a]);
	}
	void UNION(int a,int b){
		a=FIND(a);b=FIND(b);if(a==b)return;
		if(UF[a]>UF[b])swap(a,b);
		UF[a]+=UF[b];UF[b]=a;
	}
}
// ここから編集しろ
int p[1100];
int q[1100];
int t[1100];
int main(){
	int a,b;scanf("%d%d",&a,&b);
	for(int i=0;i<b;i++){
		scanf("%d%d",p+i,q+i);
		t[i]=max(0,a-p[i]);
	}
	std::sort(t,t+b);
	int ret=0;
	for(int i=0;i<b-1;i++)ret+=t[i];printf("%d\n",ret);
}

C++: #include<iostream>
#include<string>
#include<algorithm>	
#include<cmath>
using namespace std;
int main(void) {
	int n, m, a, b, sum = 0, num = 0, ans = 0;  cin >> n >> m;
	int test[1001] = { 0 };
	for (int h = 0; h < m; h++) {
		cin >> a >> b;
		if (a >= n) {
			sum++;
		}
		else {
			test[num] = n - a;
			num++;
		}
	}
	for (int s = 0; s < num - 1; s++) {
		for (int t = s + 1; t < num; t++) {
			if (test[s] > test[t]) {
				int uio = test[s];
				test[s] = test[t];
				test[t] = uio;
			}
		}
	}
	for (int h = 0; h < m - sum - 1; h++) {
		ans += test[h];
	}
	cout << ans << endl;
	cin >> n;
}

C++: #include<iostream>
using namespace std;

void sort( int* a, int n )
{
    for( int i = 0; i < n - 1; i++ )
        for( int j = i + 1; j < n; j++ )
        {
            if( a[i] > a[j] )
            {
                int t = a[i];
                a[i] = a[j];
                a[j] = t;
            }
        }
}

int main()
{
    int n, m;

    cin >> n >> m;

    int w[m], l[m], cnt = 0;
    for( int i = 0; i < m; i++ )
    {
        cin >> w[i] >> l[m];
        if( w[i] >= n )
            cnt++;
    }

    sort( w, m );

    int i = m - 1, sum = 0;
    while( cnt < m - 1 )
    {
        if( w[i] < n )
        {
            sum += n - w[i];
            cnt++;
        }
        i--;
    }

    cout << sum << endl;

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
#define MOD 1000000007
#define INF (1 << 30)
#define REP(i, m, n) for(int i = (int)m; i < (int)n; ++i)
#define rep(i, n) REP(i, 0, n)


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

    int n, m; cin >> n >> m;
    int ans = 0;
    vector<P> v(m);
    rep(i, m) {
        int a, b; cin >> a >> b;
        v[i] = {a, b};
    }
    sort(v.begin(), v.end());
    REP(i, 1, m) {
        if(v[i].first < n) ans += n - v[i].first;
    }
    cout << ans << '\n';
    return 0;
}

C++: # include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
using namespace std;
using LL = long long;
constexpr int MOD = 1000000000 + 7;
constexpr int INF = 2000000000;
const double PI = acos(-1);
 
int main() {
	int n, m;
	std::cin >> n >> m;
	std::vector<int> p;
	int a, b;
	int count = m - 1;
	for (int i = 0; i < m; i++) {
		std::cin >> a >> b;
		if (a < n)p.emplace_back(n - a);
		else count--;
	}
	std::sort(p.begin(), p.end());
	int ans = 0;
	for (int i = 0; i < count; i++) {
		ans += p[i];
	}
	std::cout << ans << std::endl;
}