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;

#define REP(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define REP3(i, m, n) for(int i = m, i##_len=(n);i < i##_len;i++)
#define FORVEC(i, v) for(int i = 0;i < v.size();i++)

#define INF 1000000000
#define pb(a) push_back(a)
#define vi vector<int>
#define vvi vector<vi>
#define P pair<int,int>
#define all(vec) (vec.begin()),(vec.end())
#define ll long long int;
#define sz(x) ((int)(x).size())
#define in(x) int x;cin>>x;
#define bit(n) (1<<(n))


template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }


int main(){
	int n,m;
	int a,b;
	int ans=0;
	int max=0;
	cin>>n>>m;
	REP(i,m){
		cin>>a>>b;
		if(a<b){
			ans+=b-n;
		}
		if(b-n>max)max=b-n;
	}
	cout<<ans-max<<"\n";
	return 0;
}

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<iostream>//aoj631
using namespace std;
int main(void)
{
	int i,n,m,a,b,c=0,d=0,e=0;
	cin>>n>>m;
	for(i=1;i<=m;i++){
		cin>>a>>b;
		if(a>=n) c=0;
		if(a<n) c=n-a;
		if(d<c) d=c;
		e=e+c;
	}
	e=e-d;
	cout<<e<<endl;
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;

int main(){
  int n,m;
  cin>>n>>m;
  P A[1001];
  for(int i=0;i<m;i++)cin>>A[i].first>>A[i].second;
  sort(A,A+m,greater<P>());
  int ans=0;
  for(int i=0;i<m-1;i++){
    if(A[i].first>=n)continue;
    ans+=(n-A[i].first);
  }
  cout<<ans<<endl;
  return 0;
}

C++: #include<stdio.h>
int main(void){
	int n=0,m=0,a=0,b=0,c=0,sum=0,max=0;
	scanf("%d %d"&n,&m);
	for(int i=0;i<m;i++){
		scanf("%d %d"&a,&b);
		c=n-a;
		if(c>0){
			sum+=c;
		}
		if(c>max){
			max=c;
		}
	}
	printf("%d\n",sum-max);
	return 0;
}

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





int main(){
  int n,m;
  cin>>n>>m;
  int a[1010]={};
  int count=0;
  int b;
  for(int i=0;i<m;i++){
    cin>>a[i]>>b;
    if(a[i]>=n){count++;}
  }
  sort(a,a+m);
  reverse(a,a+m);
  int ans=0;
  for(int i=count;i<m-1;i++){
    ans+=n-a[i];
  }
  cout<<ans<<endl;
  
  
    
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#define all(x) (x).begin(), (x).end()
using namespace std;

int main(void) {
    int N,M;cin>>N>>M;
    vector<int> A(M);
    for (int i = 0; i < M; i++) {
        int b;
        cin>>A[i]>>b;
    }
    sort(all(A));
    reverse(all(A));
    int result = 0;
    for (int i = 0; i < M - 1; i++) {
        if (A[i] < N) result += N - A[i];
    }
    cout << result << endl;
    return 0;
}

C++: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "iomanip"
#include "cmath"
#include "random"

using namespace std;

const long long int MOD = 1000000007;

long long int N, M, K, H, W, L, R;

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

	cin >> N >> K;
	vector<int>v(K);
	for (int i = 0; i < K; i++) {
		cin >> L >> R;
		v[i] = L;
	}
	sort(v.begin(), v.end());
	int ans = 0;
	for (int i = 1; i < K; i++) {
		ans += max((int)N - v[i], 0);
	}
	cout << ans << endl;
	return 0;
}

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

int main(){
 int N,M,A[1000],B[1000],ok=0,temp,ans=0;
 cin>>N>>M;
 for(int i=0;i<M;i++){
 	cin>>A[i]>>B[i];
	if(A[i]>=N)ok++;
 }
 for(int i=0;i<M-1;i++){
 	for(int j=0;j<M-1;j++){
		if(B[i]>B[i+1]){
			temp=B[i+1];
			B[i+1]=B[i];
			B[i]=temp;
			temp=A[i+1];
			A[i+1]=A[i];
			A[i]=temp;
		}
	}
 }
 for(int i=0;ok<M-1;i++){
 	if(A[i]>=N)continue;
	ok++;
 	ans=ans+B[i]-N;
 }
 cout<<ans<<endl;
}

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

#define lint long long
#define P pair<int, int>
#define LLP pair<long long, long long>
#define REP(i, x, n) for(int i = (x), i##_len = (int)(n) ; i < i##_len ; ++i)
#define rep(i, n) for(int i = 0, i##_len = (int)(n) ; i < i##_len ; ++i)
#define repr(i, n) for(int i = (int)(n) - 1 ; i >= 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())

const int IINF = 1e9 + 100;
const long long LLINF = 2e18 + 129;
const long long MOD = 1e9 + 7;
const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};
const double EPS = 1e-8;

template<typename T>
bool chmin(T &a, T b){
    if(a > b){
        a = b;
        return true;
    }else{
        return false;
    }
}

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

    int n, m;
    cin >> n >> m;

    vector<int> a(m), b(m);
    rep(i, m){
        cin >> a[i] >> b[i];
    }

    int ans = IINF;
    rep(i, m){
        int t = 0;
        rep(j, m){
            if(i == j){
                continue;
            }
            t += max(b[j] - n, 0);
        }
        chmin(ans, t);
    }

    cout << ans << endl;

    return 0;
}

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

int main()
{
	int n, m;
	int a, b;

	int outn=0, out[1000 + 10];

	int ans = 0;

	int i, j;

	int t;

	cin >> n >> m;

	for (i = 0; i < m; i++)
	{
		cin >> a >> b;

		if (a < n)
		{
			out[outn] = n - a;
			outn++;
		}
	}

	if (outn > 1)
	{
		for (i = 0; i < outn - 1; i++)
		{
			for (j = i + 1; j < outn; j++)
			{
				if (out[i] > out[j])
				{
					t = out[i];
					out[i] = out[j];
					out[j] = t;
				}
			}
			ans += out[i];
		}
	}

	cout << ans << endl;

	return 0;

}

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

int main(){
	int N,M,a,b,mc=0,tc=0;
	cin>>N>>M;
	for(int i=0;i<M;i++){
		cin>>a>>b;
		if(a<N){
			mc=max(mc,N-a);
			tc+=N-a;
		}
	}
	tc-=mc;
	cout<<tc<<endl;
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
    int N,M;
    cin>>N>>M;
    int sum = 0, nmax = 0;
    for(int i = 0; i < M; i++){
        int A,B;
        cin >> A >> B;
        int okane =  max(N -  A,0);
        if (okane > nmax) nmax=okane;
        sum += okane;
            
    }
            cout << sum - nmax  << endl;
    
    
    
    return 0;
}