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>
using namespace std;
int main(){
  int a,b,s,i,q,w,m;
  cin>>a>>b;
  for(m=s=i=0;i<b;i++){
    cin>>q>>w;
    s+=max(0,a-q);
    m=max(max(0,a-q),m);
  }
  cout<<s-m<<endl;
}

C++: #include<cstdio>
using namespace std;
int main(void)
{
	int n,m,a[1000],b,i,MIN,sum,nm;
	scanf("%d %d",&n,&m);
	MIN=1001;
	for(i=0;i<m;i++){
		scanf("%d %d",&a[i],&b);
		if(MIN>a[i]){
			nm=i;
			MIN=a[i];
		}
	}
	a[nm]=n;
	sum=0;
	for(i=0;i<m;i++){
		if(a[i]<n){
			sum+=n-a[i];
		}
	}
	printf("%d\n",sum);
	return 0;
}

C++: 
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>
#include<deque>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define LINF (ll)INF*INF
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef vector<pii> vp;
typedef deque<int> di;

int gcd(int a, int b){
    if(b==0) return a;
    return gcd(b,a%b);
}
int lcm(int a, int b){
    return a*b/gcd(a,b);
}

signed main(void) {
    int i,j;
    int n,m;
    cin >> n >> m;
    vi v(m);
    int ans = 0;
    rep(i,m){
        int a,b;
        cin >> a >> b;
        v[i] = max(0LL,(b-a)/2);
    }
    sort(all(v));
    rep(i,m-1)ans += v[i];
    cout << ans << endl;
}

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

int main()
{
	//合計時間

	int N, M;
	cin >> N >> M;
	int a;
	vector<int> A;
	int b=0;
	for (int i = 0; i < 2*M; i++) {
		cin >> a;
		if (a < N) {
			A.push_back(a);
			break;
		}
		b++;
		cin >> a;
	}
	int sum = 0;
	if (b < M - 1) {
		sort(A.begin(), A.end());
		reverse(A.begin(), A.end());
		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<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int main(void)
{
	int a,b,m,i,n,x,y,z;
	x=0;
	y=0;
	z=0;
	cin>>n>>m;
	for(i=0;i<m;i++){
		cin>>a>>b;
		if(a>=n){
			x=0;
		}
		if(a<n){
			x=n-a;
		}
		if(y<x){
			y=x;
		}
		z=z+x;
	}
	z=z-y;
	cout<<z<<endl;
	
	return 0;
}

C++: #include <iostream>
using namespace std;
int main(void){
    int N, M, cost=0;
    cin >> N >> M;
    
    int A[M], B[M];
    int max_loss=0, max_loss_num=-1;
    for(int i=0; i<M; i++){
        cin >> A[i] >> B[i];
        if(A[i] < N){
            if(B[i]-A[i] > max_loss){
                max_loss = B[i]-A[i];
                max_loss_num = i;
            }
        }
    }
    
    if(max_loss_num == -1){
        cout << 0 << endl;
    }
    else{
        for(int i=0; i<M; i++){
            if(i!=max_loss_num){
                if(A[i]<N){
                    cost += N-A[i];
                }
            }
        }
        cout << cost << endl;
    }
    
    return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
 
int main(){

  int n, m, a[1000], b;

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

  sort(a, a+m, greater<int>());

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

  cout<<ans<<endl;
  
  return 0;
}

C++: #include <iostream>
using namespace std;
int main(){
  int N, M;
  cin >> N >> M;
  int a, b, m = 0, ans = 0;
  
  for(int i = 0; i < M; ++i){
    cin >> a >> b;
    ans += max(0,N-a);
    m = max(N-a,m);
  }
  ans -= m;
  cout << ans << endl;
  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(void)
{
	int N,M,max1=0,all2=0;
	scanf("%d %d",&N,&M);
	int A[M]={0},B[M]={0};
	for(int i=0;i<M;i++){
		scanf("%d %d",&A[i],&B[i]);
		if(A[i]>=N){}
		else if(max1<N-A[i]){//今までの最大でかかるお金より多ければ
			all2+=max1;
			max1=(N-A[i]);
		}
		else{
			all2+=(N-A[i]);
		}
	}
	printf("%d\n",all2);
	return 0;
}
//25:02.130クリア

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int n,m,ans=0,k=0,nedan=0;
  cin>>n>>m;
  int a[m],b[m],memo[1000];
  for(int i=0;i<m;i++){
    cin>>a[i]>>b[i];
    if(ans<m-1){
      if(a[i]>=b[i]){
	ans++;
      }else {
	memo[k]=b[i];
	k++;
      }
    }
  }
  sort(memo,memo+k);
  if(ans<m-1){
    for(int i=0;i<k;i++){
      if(ans==m-1){
	break;
      }
      nedan+=memo[i]-n;
      ans++;
    }
  }
  cout<<nedan<<endl;
}

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

using namespace std;

template <typename T>
using vec1 = vector<T>;

template <typename T>
T cin2var()
{
    T val;
    cin >> val;
    return val;
}

void sub()
{
    const auto N(cin2var<int64_t>());
    const auto M(cin2var<int64_t>());

    int64_t ok_count = 0;

    using AB = tuple<int64_t, int64_t>;
    priority_queue<AB> abs;
    for (auto i = 0; i < M; ++i) {
        const auto A(cin2var<int64_t>());
        const auto B(cin2var<int64_t>());
        if (N <= A) {
            // 交換可能
            ++ok_count;
            continue;
        }
        abs.push(make_tuple(A, B));
    }

    int64_t ans = 0;
    while (M - 1 > ok_count) {
        const auto& ab(abs.top());
        ans += N - get<0>(ab);
        ++ok_count;
        abs.pop();
    }
    cout << ans << endl;
}

int main()
{
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    sub();
    return 0;
}

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

int main(){
	int N, M;
	int a[1000];
	cin >> N >> M;
	for(int i = 0; i < M; i++){
		int b, c;
		cin >> b >> c;
		a[i] = max(N - b, 0);
	}
	sort(a, a + M);
	int ans = 0;
	for(int i = 0; i < M - 1; i++) ans += a[i];
	cout << ans << endl;
	return 0;
}