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<cstdlib>
#include<queue>
#include<set>
#include<vector>
#include<string>
#include<algorithm>
#include<stack>
#include<map>
#include<cstdio>
#include<fstream>
using namespace std;
#define rep(i,a) for(int i=0;i<a;i++)

#define mp make_pair
#define P pair<int,int>
#define ll __int64
P t[10000];
int n,m;

int main(){
	cin>>n>>m;
	int ans=0;
	bool ok=false;
	int a,b;
	for(int i=0;i<m;i++){
		cin>>a>>b;
		t[i]=mp(b,a);
	}
	sort(t,t+m);
	for(int i=0;i<m-1;i++){
		int tt=t[i].second,f=t[i].first;
		if(tt<f)ans+=(f-tt)/2;
	}
	cout<<ans<<endl;
	return 0;
}

C++: #include<cstdio>
#include<algorithm>
using namespace std;
int main(void)
{
	int n,m,i,j,mx,sum,a,b;
	scanf("%d %d",&n,&m);
	sum=0;
	mx=0;
	for(i=1;i<=m;i++){
		scanf("%d %d",&a,&b);
		if(a<n){
			sum+=n-a;
			mx=max(mx,n-a);
		}
	}
	printf("%d\n",sum-mx);
	return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cstdlib>
using namespace std;
int main() {
	int n, m, now = 0, g = 0, mo = 0, p[1005][2], s[1005] = {0};
	cin >> n >> m;
	for (int i = 0; i < m; i++ ) {
		int a, b;
		cin >> a >> b;
		p[i][0] = a;
		p[i][1] = b;
		if (a >= b) {
			g++;
		}
		else {
			int c = n - a;
			
			for (int j = now; j >= 0; j--) {
				if (s[j-1] > c ) {
					s[j] = s[j-1];
					
				}
				else {
					s[j] = c;
					now++;
					
					break;
				}
				
				
			}
		}
	}
	
	
	for (int i = 0; i < m; i++) {
		if (g < m - 1) {
			mo += s[i];
			cout << mo << endl;
			g++;
		}

	}
	
	cout << mo << endl;
	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>

void solve() {
    int n, m;
    std::cin >> n >> m;

    int max = 0, sum = 0;
    while (m--) {
        int a, b;
        std::cin >> a >> b;

        int c = std::max(0, n - a);
        sum += c;
        max = std::max(max, c);
    }

    std::cout << sum - max << std::endl;
}

int main() {
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
int A[1000],B[1000],C[1000];

int main(){
	fill(&C[0],&C[1000],-1);
	int N,M,cnt=0,k=0,hi=0;
	cin >>N>>M;
	for(int i = 0;i < M;i++){
		cin >> A[i]>>B[i];
		if(A[i]<N){
			C[k]=A[i];
			k++;
		}else{
			cnt++;
		}
	}
	if(cnt >= M-1)cout<<0<<endl;
	else{
		cnt = M-1-cnt;
		sort(&C[0],&C[1000]);
		reverse(&C[0],&C[1000]);
		for(int i = 0;i < cnt;i++){
			hi = hi+N-C[i];
		}
		cout <<hi<<endl;
	}
	

		return 0;
}

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

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

using namespace std;

int main()
{
    int N, M, A, B;
    cin >> N >> M;
    vector<pair<int, int>> v(M);
    for (int i = 0; i < M; i++) {
        cin >> A >> B;
        v[i] = make_pair(A, B);
    }

    sort(v.rbegin(), v.rend());
    
    int res = 0;
    for (int i = 0; i < M - 1; i++) {
        tie(A, B) = v[i];
        if (A < N) {
            res += N - A;
        }
    }
    cout << res << endl;
    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.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 <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

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

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

    sort(all(a),greater<int>());

    int ans=0;
    rep(i,m-1) ans += max(0,n-a[i]);
    cout << ans << endl;
    return 0;
}

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

int main(){
  int N,M,cnt = 0, ans = 0;

  cin >> N >> M;
  
  int *a,*b,*w;

  a = new int[M];
  b = new int[M];
  
  for(int i = 0;i<M;++i){
    cin >> a[i] >> b[i];

    if(a[i] >= N){
      ++cnt;
    }
  }
  
  sort(a,a+N);
  reverse(a, a+N);

  int counter = 0;
  for(int i=cnt+1; i<=M-1;++i){
    if(a[counter] < N){
      ans += N - a[counter];
    }else{
      --i;
    }
    ++counter;
  }

  cout << ans << endl;
			    
  delete[] a;
  delete[] b;
  return 0;
}

C++: #include <bits/stdc++.h>
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(begin(a), end(a));
  int ans = 0;
  for (int i = 1; i < M; ++i) ans += max(0, N - a[i]);
  cout << ans << endl;
  return 0;
}