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 <utility> // pair make_pair
using std::swap; // c++11
using std::pair;
using std::make_pair;
#include <tuple> // tuple(c++11) make_tuple(c++11)
using std::tuple; // get<address>(variable)
using std::make_tuple;
#include <string> // string stoi(c++11) stoll(c++11) to_string(c++11)
using std::string;
using std::stoi;
using std::stoll;
using std::to_string;
#include <queue> // queue priority_queue
using std::queue;
using std::priority_queue;
#include <stack> // stack
using std::stack;
#include <vector> // vector
using std::vector;
#include <map> // map
using std::map;
#include <set> // set
using std::set; // insert erase find count
#include <algorithm> // min max
using std::sort;
using std::lower_bound; // iterator,  >=
using std::upper_bound; // iterator,  >
using std::min;
using std::max;
using std::min_element; // iterator
using std::max_element; // iterator
#include <complex> // complex
using std::complex; // real imag
#include <iostream> // cin cout
using std::cin;
using std::cout;
using std::endl;
#include <iomanip> // setprecision fixed(manipulator)
using std::setprecision;
using std::fixed;
#include <cmath>
#include <cstdio>

#define lli long long int
#define INF (int)1e09
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define EFOR(i, m, n) for (int i = m; i <= n; i++)
#define RFOR(i, m, n) for (int i = m; i > n; i--)
#define REFOR(i, m, n) for (int i = m; i >= n; i--)
#define VSORT(v) sort((v).begin(), (v).end()) // sort vector
#define VGSORT(v) sort((v).begin(), (v).end(), std::greater<int>()) // sort vector
typedef pair<int, int> PI;
typedef tuple<int, int, int> TI;
template <typename T> using Vec = vector<T>; // c++11

int main(void) {
    int n;
    int m;
    cin >> n >> m;
    int temp;
    Vec<int> table(m);
    FOR (i, 0, m) {
        cin >> table[i] >> temp;
    }
    VGSORT(table);
    int i = 0;
    while (1) {
        if (table[i] >= n) {
            i++;
        } else {
            break;
        }
    }

    if (i >= m - 1) {
        cout << 0 << endl;
    } else {
        int count = 0;
        FOR (j, i, m - 1) {
            count += (m - table[i]);
        }
        cout << count << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
  int n,m;
  cin >>n>>m;
  int c[m];
  for(int i=0;i<m;i++){
    int num;
    cin>>c[i]>>num;
  }
  sort(c,c+m);
  int count=0;
  for(int i=1;c[i]<n;i++){
    count+=n-c[i];
  }
  cout <<count<<endl;
  return 0;
}

C++: #include <stdio.h>
#define rep(i, n) for(int i = 0; i < n; ++i)

int main(void) {
  int n, ans = 0, max = 0, m;
  scanf("%d%d", &n, &m);
  rep(i, m) {
    int a, b;
    scanf("%d%d", &a, &b);
    if(a < n) {
      ans += n - a;
      if(max < n - a) max = n - a;
    }
  }
  ans -= max;
  printf("%d\n", ans);
  return 0;
}

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

int main(){
  int n,m;
  cin >> n >> m;
  int a,b;
  int ans=0;
  int card[1001];
  for(int i=0;i<1001;i++){
    card[i]=0;
  }
  for(int i=0;i<m;i++){
    cin >> a >> b;
    if(a>=n){
      card[0]++;
    }else{
      card[n-a]++;
    }
  }
  int flag=0;
  for(int i=1001;i>=0;i--){
    if(flag==0&&card[i]>0){
      flag=1;
      ans-=i;
    }
    ans+=card[i]*i;
  }

  cout << ans << endl;

  return 0;
}

C++: #include <bits/stdc++.h>
 
using namespace std;
 
int main() {
 
    int n,m;
    cin >> n >> m ;
 
    int a, b;
    int mina=2001;
    int counter = 0;
    for (int i = 0; i < m; i++) {
        cin >> a >> b;
        if (a < n) { counter += n - a; }
        mina = min(a, mina);
    }
 
    cout << counter+mina-n << endl;
}

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

int main() {
  int n, m;
  cin >> n >> m;
  int a[m], b[m];
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
  }

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

  int count = 0;
  for (int i = 0; i < m - 1; i++) {
    if (a[i] < n) count += n - a[i];
  }

  cout << count << endl;

  return 0;
}

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

int main() {
	int n, m;
	cin >> n >> m;

	int i = 0;

	int answer = 0;
	int judge;
	int lastjudge = 0;

	while (i < m) {

		int a, b;
		cin >> a >> b;

		if (a < b) {

			judge = n - a;

			while (a < n){
				a++;
				answer++;
			}
			
				if (judge > lastjudge) {
					lastjudge = judge;
				}		
		}
		i++;
	}

	cout << answer - lastjudge << endl;
    return 0;
}

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

int main(){
  int N,M;
  cin >> N >> M;

  int A[1000],B[1000];
  for(int i = 0; i < M; i++){
    cin >> A[i] >> B[i];
  }

  list<int> l;
  int count = 0;
  for(int i = 0; i < M; i++){
    if(A[i] >= B[i]) count++;
    if(A[i] < B[i]) l.push_back(B[i] - A[i]);
  }
  l.sort();

  int ans = 0;
  while(ans < M - 1 - count){
    ans += l.front() / 2;
    l.pop_front();
  }
  cout << ans;

  return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
int main() {
	int j,cost=0,atari=0,card[100] = {}, purpose, i, N, M, A[100], B[100];
		cin >> N >> M;
		purpose = M - 1;
		for (i = 0; i < M; i++) {
			cin >> A[i] >> B[i];
			if (atari >= purpose)break;
			else if (A[i] >= N) {
				atari++;
			}
		}
			if (atari < purpose) {
				for (i = 0; i < M; i++) {
					if (atari >= purpose)break;
					while (1) {
						if (A[i] > N) {
							atari++;
							break;
						}
						A[i] += 1;
						B[i] -= 1;
						cost++;
					}
				}
			}
		
		
		cout << cost << endl;
	return 0;
}

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

int main(){

	int N, M;
	cin >> N >> M;
	int answer = 0;
	int buf, buff;
	int MAX;

	for(int i = 0; i < M; i++){
		cin >> buf >> buff;
		answer += max(0, buff - N);
		MAX = max(MAX, buff - N);
	}

	if(MAX > 0) answer -= MAX;

	cout << answer << endl;

	return 0;
}

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

int main(){
  int n,m;
  cin >> n >> m;
  int a,b;
  int ans=0;
  int card[1001];
  for(int i=0;i<1001;i++){
    card[i]=0;
  }
  for(int i=0;i<m;i++){
    cin >> a >> b;
    if(a>=n){
      card[0]++;
    }else{
      card[n-a]++;
    }
  }
  int flag=0;
  for(int i=1001;i>=0;i--){
    if(flag==0&&card[i]>0){
      flag=1;
      ans-=i;
    }
    ans+=card[i]*i;
  }

  cout << ans;

  return 0;
}

C++: #include <bits/stdc++.h>
#define fo(i,a) for(i = 0;i < a;i++)
using namespace std;
int main(void)
{
	int n,m,a[1000]={0},b,c=0,p=0,i;
	
	cin >> n >> m ;
	fo(i,m){
		cin >> a[i] >> b ;
	}
	sort(a,a + m,greater<int>()) ;
	fo(i,m){
		if(a[i] >= n){
				p++ ;
			}
		else if(a[i] < n && p < m - 1){
			c += n - a[i] ;
			p++ ;
		}
	}
	cout << c << endl ;
	
	return 0 ;
}

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m);
  for (int i = 0; i < m; i ++) cin >> a[i] >> b[i];

  sort(a.begin(), a.end());
  reverse(a.begin(), a.end());
  int sum = 0;
  for (int i = 0; i < m - 1; i ++) {
    if (a[i] < n) {
      sum += n - a[i];
    }
  }

  cout << sum << endl;
}