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;

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(count < M - 1){
    count++;
    ans += l.front() / 2;
    l.pop_front();
  }
  cout << ans << endl;

  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int n,m,a,b,s=0,t=0;
  cin >> n >> m;
  for(int i=0; i<m; i++){
	cin >> a >> b;
	  if(a<n){
	  	t += n-a;
		s = max(s,n-a);
	  }
  }
  t -= s;
  cout << t << 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();
  }
  if(M - 1 - count <= 0){
    cout << '0';
  }else {
    cout << ans;
  }

  return 0;
}

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

int main()
{
	//合計時間

	int N, M;
	cin >> N >> M;
	int a;
	int A[5000];
	int b = 0;
	int x = 0;
	for (int i = 0; i < 2 * M; i++) {
		cin >> a;
		if (a < N) {
			A[x]=a;
			x++;
			break;
		}
		b++;
		cin >> a;
	}
	int sum = 0;
	if (b < M - 1) {
		sort(A, A+x);
		reverse(A, A+x);
		for (int i = 0; i < M - 1 - b; i++) {
			sum += N - A[i];
		}
	}
	cout << sum << endl;

	/*
	int sum=0, n;
	for (int i = 0; i < 4; i++) {
	cin >> n;
	sum += n;
	}
	cout << sum / 60 << endl;
	cout << sum % 60 << endl;
	//平均点
	/*
	int P[5];
	int sum=0;
	for (int i = 0; i < 5; i++) {
	cin >> P[i];
	if (P[i] < 40) P[i] = 40;
	sum += P[i];
	}
	cout << sum / 5 << endl;
	*/
	//投票
	/*
	int N, M;
	cin >> N >> M;
	int A[1000], vote[1000];
	for (int i = 0; i < N; i++) {
	cin >> A[i];
	vote[i] = 0;
	}
	int b;
	for (int i = 0; i < M; i++) {
	cin >> b;
	for (int j = 0; j < N; j++) {
	if (A[j] <= b) {
	vote[j]++;
	break;
	}
	}
	}
	int temps = 0, ans;
	for (int i = 0; i < N; i++) {
	if (vote[i] > temps) {
	temps = vote[i];
	ans = i;
	}
	}
	cout << ans + 1 << endl;
	*/

	//2.すごろく	
	/*
	int n;
	cin >> n;
	int A, _A = 1;
	int k = 0;
	int A_count[100];
	A_count[0] = 1;
	for (int i = 0; i < n; i++)
	{
	cin >> A;
	if (A == 1)
	{
	if (A == _A)
	{
	A_count[k]++;
	}
	else
	{
	k++;
	A_count[k] = 2;
	}
	}
	_A = A;
	}
	sort(A_count, A_count + k + 1);
	cout << A_count[k] << endl;
	*/

	//タイムカード
	/*
	int t[2][3];
	int x[3][3];
	for (int n=0; n < 3; n++)
	{
	for (int i=0; i < 2; i++)
	for (int k=0; k < 3; k++)
	cin >> t[i][k];

	for (int m = 2; m >= 0; m--)
	{
	x[n][m] = t[1][m] - t[0][m];
	if (t[0][m] > t[1][m])
	{
	x[n][m] += 60;
	t[1][m - 1]--;
	}
	}
	}
	for (int n= 0; n < 3; n++)
	{
	cout << x[n][0] << ' ' << x[n][1] <<' '<< x[n][2] << endl;
	}
	*/



	return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:https://onlinejudge.u-aizu.ac.jp/challenges/search/volumes/0631>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

ll solve(){
    ll res = 0;
    ll N,M; cin >> N >> M;
    vector<ll> cost(M);
    for(int i = 0; i < M;i++){
        ll A,B; cin >> A >> B;
        cost[i] = max(0LL,N-A);
    }
    sort(cost.begin(),cost.end());
    res = accumulate(cost.begin(),cost.end(),0LL) - *cost.rbegin();
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    cout << solve() << endl;
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(void){
    int n,m,a,b,count=0;
    std::deque<int> deq;
    cin>>n>>m;
    for (int i=0;i<m;i++) {
        cin>>a>>b;
        if (a<n) {
            deq.emplace_back(b-n);
        } else {
            count++;
        }
    }
    sort(deq.begin(),deq.end());
    int sum=0;
    if (count!=m-1) {
        for (int j=0;j<deq.size()-1;j++) {
            sum+=deq[j];
        }
    }
    cout<<sum<<endl;
}

C++: #include<iostream>
#include<algorithm>
#include<set>
using namespace std;
int main() {
	int n, m;
	cin >> n >> m;
	set<int>a;
	int sum = 0,co=0;
	for (int z = 0; z < m; z++) {
		int b, c;
		cin >> b >> c;
		if (b >= n)
			co++;
		else {
			a.insert(n - b);
		}
	}
	set<int>::iterator it;
	while (co < m - 1) {
		it = a.begin();
		sum += (*it);
		a.erase(it);
		co++;
	}
	cout << sum << endl;
}

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

int Arr_Sort_rev( const void* A , const void* B )
{
    return *((const int*)B) - *((const int*)A) ;
}

int main()
{
    int mass = 0 ;
    int card = 0 ;
    int win[1001] = {0} ;
    int lose[1001] = {0} ;
    int Count = 0 ;
    int cost = 0 ;

    cin >> mass >> card ;
    for ( int i = 0 ; i < card ; i++ )
    {
        cin >> win[i] >> lose[i] ;
    }

    qsort( win , 1001 , sizeof(win[0]) , Arr_Sort_rev ) ;
    sort( lose , lose+card ) ;
    for ( int i = 0 ; i < card ; i++ )
    {
        if ( win[i] < mass )
        {
            while( win[i] < mass )
            {
                win[i]++ ;
                cost++ ;
            }
            Count++ ;
        }
        else Count++ ;
        if ( Count >= card-1 ) break ;
    }

    cout << cost << endl ;

    return 0 ;
}