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<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 <bits/stdc++.h>
using namespace std;

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

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

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

int main() {
		int a, b;
		cin >> a >> b;
		int MAX = 0, sum = 0;
		for (int c = 0; c < b; c++) {
			int d, e;
			cin >> d >> e;
			sum += max(0, a - d);
			MAX = max(MAX, a - d);
		}
		cout << sum - MAX << endl;
}

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

int main(){
	int n,m,r=0;
	scanf("%d%d",&n,&m);
	vector<int>v(m);
	for(int i=0;i<m;i++){
		int a,b;
		scanf("%d%d",&a,&b);
		v[i]=max(n-a,0);
	}
	sort(v.begin(),v.end());
	for(int i=0;i<m-1;i++)r+=v[i];
	printf("%d\n",r);
	return 0;
}

C++: #include <iostream>
#include <cmath>
using namespace std;
int main(){
    int n,m,max=0,sum=0;
    cin>>n>>m;
    for(int i=0;i<m;++i){
        int a,b;
        cin>>a>>b;
        if(a-b<0){
            sum+=n-a;
        }
        if(max<n-a){
            max=n-a;
        }
    }
    cout<<sum-max<<endl;
}

C++: #include<stdio.h>
#include<math.h>
#include<algorithm>
#include<cctype>
#include<string.h>
#include <stdlib.h>

#define PI 3.1415926
void swap(int* x, int* y) {
	int a = *x;
	int b = *y;
	*x = b;
	*y = a;
}
void dainyu(int X[], int Y[ ],int n,int m) {
	for (int i = n; i <= m; i++) {
		Y[i] = X[i];
	}
}
void Dscanf(int X[], int n, int m) {
	for (int i = n; i <= m;i++) {
		scanf("%d", &X[i]);
	}
}
void Dprintf(int X[], int n, int m) {
	for (int i = n; i <= m; i++) {
		printf("%d\n", X[i]);
	}
}
void reverse(int X[], int Y[],int n,int m) {
	for (int i = n; i <= m; i++) {
		Y[i] = X[n + m - i];
	}
}

int factorial(int n);
int sum(int n, int*);
int ave(int n, int*);
int min(int n, int*), min(int n, int m);
int max(int n, int*), max(int n, int m);

int main() {
	int N, M, A[4000], B[4000];
	scanf("%d %d", &N, &M);
	for (int i = 1; i <= M; i++) {
		scanf("%d %d", &A[i], &B[i]);
	}
	for (int i = 1; i <= M; i++) {
		if (B[i] == max(M, B)) {
			B[i] = N;
			break;
		}
	}
	int x = 0;
	for (int i = 1; i <= M-1; i++) {
		if (B[i] > N) {
			x -= N - B[i];
		}
	}
	printf("%d\n", x);

	return 0;
}

int factorial(int n) {
	if (n > 0) {
		return n * factorial(n - 1);
	}
	else {
		return 1;
	}
}
int sum(int n, int *all) {
	int total = 0;
	for (int i = 0; i < n; ++i) {
		total += all[i];
	}
	return total;
}
int ave(int n, int *all) {
	int total = 0;
	for (int i = 0; i<n; ++i) {
		total += all[i];
	}
	return total / n;
}
int min(int n, int *all) {

	int min = all[0];
	for (int i = 1; i < n; ++i) {
		if (all[0] > all[i]) {
			min = all[i];
		}
	}
	return min;
}
int max(int n, int *all) {
	int max = all[0];
	for (int i = 1; i<n; ++i) {
		if (all[0]<all[i]) {
			max = all[i];
		}
	}
	return max;
}
int max(int n, int m) {
	if (n >= m) {
		return n;
	}
	else {
		return m;
	}
}
int min(int n, int m) {
	if (n >= m) {
		return m;
	}
	else {
		return n;
	}
}

C++: #include <iostream>
using namespace std;
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <functional> //sort(A,A.end(),greater<int>());???????????????
#include <queue>
#include <stack>
#include <vector>

#define INF 10 << 10

int a[1024];

int main() {
	int n, m;
	cin >> n >> m;
	int cnt = 0;
	for (int i = 0; i < m; ++i) {
		int b;
		cin >> a[i] >> b;
		if (a[i] >= n) {
			cnt++;
		}
	}
	int cost = 0;
	if(cnt < m - 1) {
		cnt = (m - 1) - cnt;
		sort(a, a + m, greater<int>());
		for (int i = 0; i < m; ++i) {
			if (a[i] >= n) { continue; }
			cost += n - a[i];
			cnt--;
			if (cnt == 0) { break; }
		}
	}
	cout << cost << 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+n);
	cout << ans - a[m-1] << endl;
	return 0;
}

C++: #include <stdio.h>
#define rep(i, n) for(int i = 0; i < n; ++i)

int main(void) {
  int n, ans = 0, max = 0;
  scanf("%d", &n);
  rep(i, n) {
    int a, b;
    scanf("%d%d", &a, &b);
    if(a < n) {
      ans += n - a;
      if(max < n - a) max = n - a;
    }
  }
  ans -= max;
  printf("%d\n", ans);
  return 0;
}

C++: #include <iostream>
#include<algorithm>
using namespace std;
int main(){
    int n,m,a[1000],b[1000],ans=0;
    cin>>n>>m;
    for(int i=0;i<m;i++){
        cin>>a[i]>>b[i];
    }
    sort(a,a+m,greater<int>());
    for(int i=0;i<m-1;i++){
        if(a[i]<n)ans=ans+(n-a[i]);
    }
    cout<<ans<<endl;
}