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 <iostream>
#include <stdio.h>
#include <string>
#include <string.h>

using namespace std;

int main (void)
{
	int n,m,a[100],b[100],cou,x;
	cin >> n >> m;
	cou = 0;
	x = 200;
	for (int i = 0; i < m; i++)
	{
		cin >> a[i] >> b[i];
		if (a[i] >= b[i])
		{
			cou++;
		}
		
	}
	
	if (cou >= m - 1)
	{
		cout << '0' << endl;
	}
	else
	{
		for (int i = 0; i < m; i++)
		{
			if (a[i] < b[i] && x > (b[i] - a[i]))
			{
				x = 0;
				x = b[i] - a[i];
			}
		}
	}???

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]){
      if((B[i] - A[i]) % 2 != 0){
        l.push_back(B[i] - A[i] + 1);
      }else{
        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<cstdio>
#include <iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<cstdlib>
#include<list>
#include<set>
#include<numeric>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int A[1010];

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

	for (int i = 0; i < M; i++) {
		int b;
		cin >> A[i] >> b;
	}
	sort(A, A + M);
	reverse(A,A+M);

	int ans = 0,m=0;
	for (int i = 0; i < M; i++) {
		if (A[i] >= N)m++;
		else if(m < M-1){
			ans += N - A[i];
			m++;
		}
	}
	cout << ans << endl;
	return 0;
}

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

using namespace std;


int main(){
	int n, m;
	int in;
	int a[1000];
	cin >> n >> m;
	fill_n(a, 1000, n);
	int ans = 0;
	for(int i = 0; i < m; i++){
		cin >> in;
		a[i] -= in;
		ans += max(0, a[i]);
		cin >> in;
	}
	sort(a, a+m);
	cout << ans - a[m-1] << endl;
	return 0;
}

C++: #include <stdio.h>
int main (){
  int N,M,A[1000],B[1000],i,cost=0,min;
  scanf("%d %d",&N,&M);
  scanf("%d %d",&A[0],&B[0]);
  min= A[0];
  for (i=1 ; i<M ;i++){
    scanf("%d %d",&A[i],&B[i]);
    if (min>A[i]){
      min=A[i];
    }
  }
  for(i=0 ; i<M ; i++){
    scanf("%d %d",&A[i],&B[i]);
    if( A[i]<N ){
      cost=cost+B[i]-N;
    }
  }
  cost=cost-N+min;
  printf("%d\n",cost);
  return 0;
}

C++: // ------ lib ------ //


#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
	int a = 0;
	int b = 0;
	int aw = 0;
	cin >> a;
	cin >> b;
	int c[1000];
	int d[1000];
	for (int i = 0; i < b; i++)
	{
		cin >> c[i];
		cin >> d[i];
	}
	sort(c, c + b);
	for (int i = 1; i < b; i++)
	{
		if (c[i] < a)
		{
			aw += (a - c[i]);
		}
	}
	cout << aw << endl;
	return 0;
}

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

int main(){
    int n, m;
    cin>>n>>m;
    vector<int>vec(m, 0);
    for(int i=0;i<m;i++){
        int a, b;
        cin>>a>>b;
        if(b>n){
            vec[i]=b-n;
        }
    }
    sort(vec.begin(), vec.end());
    int ans = 0;
    for(int i=0;i+1<vec.size();i++){
        ans+=vec[i];
    }
    cout<<ans<<endl;

    return 0;
}

C++: #include<stdio.h>
#include<string.h>
#include<math.h>
int main(void)
{
	int a,s,d,f,g[1001],h=0,j,i,z,x;
	scanf("%d %d",&a,&s);
	for(i=0;i<s;i++){
		scanf("%d %d",&d,&f);
		if(d<a) g[i]=a-d;
		else g[i]=0;
	}
	j=0;
	for(i=0;i<s;i++){
		if(j<g[i]){
			j=g[i];
			x=i;
		}
	}
	for(i=0;i<s;i++) if(i!=x) h+=g[i];
	printf("%d\n",h);
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
  int n, m, i, win, miss, wcard, cost, takeN;
  vector<int> missPoint;

  cin >> n >> m;

  wcard = 0;

  for (i = 0; i < m; i++)
  {
    cin >> win >> miss;
    if (win >= n)
    {
      wcard++;
    }
    else
    {
      missPoint.push_back(miss - n);
    }
  }

  sort(missPoint.begin(), missPoint.end());

  cost = 0;
  takeN = (m - 1) - wcard;
  for (i = 0; i < takeN; i++)
  {
    cost += missPoint[i];
  }

  cout << cost << endl;

  return 0;
}