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(void){
	int i,N,M,A[1000],B[1000],hcnt=0,cnt=0,money=0;
	cin>>N>>M;
	for(i=0;i<M;i++){
		cin>>A[i]>>B[i];
		if(A[i]>N){
			hcnt++;
			//cnt++;
		}
	}
	if(hcnt>=M-1){
		cout<<0<<endl;
		return 0;
	}
	i=0;
	while(hcnt<=M-1){//for(i=0;<M-1;i++){
		if(A[i]==N){
			hcnt++;
			i++;
		}
		else if(A[i]>N){
			i++;
		}
		else if(A[i]<N){
			if(cnt<N-A[i]){//今までの最大でかかるお金より多ければ
			money+=cnt;
			cnt=(N-A[i]);
			}
			else{
				money+=(N-A[i]);
			}
			hcnt++;
			i++;
		}
	}
	cout<<money<<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;
	vector<int> A;
	int b=0;
	for (int i = 0; i < 2*M; i+=2) {
		cin >> a;
		if (a < N) {
			A.push_back(a);
			break;
		}
		b++;
	}
	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<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
using namespace std;

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

    vector<int> res;
    int a, b;
    for(int i = 0; i < m; i++){
        cin >> a >> b;
        res.push_back(max(0, n-a));
    }

    sort(res.begin(), res.end());

    cout << accumulate(res.begin(), res.end(), 0) - res[m-1] << endl;

    return 0;
}

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

int main(void){
    int N, M;
    int A, B;
    int max = 0, sum = 0;
    
    cin >> N >> M;
    for(int i = 0; i < M; i++){
        cin >> A >> B;
        if(N > A){
            sum += N - A;
            if(N - A > max) max = N - A;
        }
    }
    
    cout << (sum - max) << endl;
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#define REP(i, n) for(int i = 0;i < n;i++)
#define ll long long
#define INF 114514
using namespace std;




int main(){
	int n, m, i, ans = 0;
	cin >> n >> m;
	vector<int>v(m);
	for (i = 0; i < m; i++) {
		int a, b;
		cin >> a >> b;
		v[i] = n  - a;
		if (v[i] < 0) v[i] = 0;
		ans += v[i];
	}
	sort(v.begin(), v.end());
	cout << ans - v[m-1] << 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;
#define INF 1<<30
#define LINF 1LL<<60

int main(void) {
    cin.tie(0); ios::sync_with_stdio(false);
    ll N,M; cin >> N >> M;
    vector<pll> AB(M);
    for(int i = 0; i < M;i++){
        cin >> AB[i].first >> AB[i].second;
    }
    sort(AB.begin(),AB.end());
    reverse(AB.begin(), AB.end());
    ll res = 0;
    for(int i = 0; i < M-1;i++){
        if(AB[i].first >= N)continue;
        else{
            res += N - AB[i].first;
        }
    }
    cout << res << endl;
    return 0;
}

C++: #include <iostream>
using namespace std;
int  main(void)
{
	int N,M,a,b,c[1000]={0},d,e,f,g;
	cin>>N>>M;
	for (a=0;a<M;a++)
	{
		cin>>b>>d;
		c[a]=b;
	}
	sort(c,c+1000);
	for (a=999;a>=0;a--)
	{
		if (c[a]>=N/2)
		{
			e++;
			if (e>=M-1)
			{
				break;
			}
		}
	}
	cout<<e<<endl;
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int M,N,i,p=0,ans=0,t=5;
main(){
  cin>>N>>M;
  int A[M],B[M];
  for(i=0;i<M;i++){
    cin>>A[i]>>B[i];
  }
  sort(A, A+M, greater<>());
  for(i=0;i<M;i++){
    if(p<M-1){
      if(A[i]>=N){
        p++;
      }else{
        ans+=N-A[i];
        p++;
      }
    }
  }
  cout<<ans<<endl;
  return 0;
}

C++: #include<stdio.h>
#include<iostream>
using namespace std;
int main(){
int n,m,t[1000],f[1000],i,j,g=0,tn,u,o;
cin>>n>>m;
for(i=0;i<m;i++){
scanf("%d %d",&t[i],&f[i]);
if(t[i]>=n){
o++;
t[i]=0;
}
}
for(i=0;i<m;i++){
for(j=i+1;j<m;j++){
if(t[i]<t[j]){
tn=t[i];
t[i]=t[j];
t[j]=tn;
}
}
}
u=m-1-o;
for(i=0;i<u;i++){
g+=n-t[i];
}
cout<<g<<endl;
return 0;
}

C++: #include <stdio.h>
#include <vector>
#include <algorithm>

using namespace std;

int main(void){
	int n, mm, m, i, t, c, tintin;
	scanf("%d %d", &n, &mm);

	vector<int> point;

	m = mm-1;

	c = 0;

	for(i = 0;i < mm;++i){
		scanf("%d %d", &t, &tintin);
		if(t >= n){
			--m;
		}else{
			point.push_back(t);
		}
	}

	sort(point.begin(), point.end());
	reverse(point.begin(), point.end());

	i = 0;
	while(m > 0){
		c += point[i];
		++i;
		--m;
	}

	printf("%d\n", c);

	return 0;
}

C++: //q2.cpp

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;


#define mod 1000000007 //1e9+7
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 10000


int main(){
  int n,m,a,b,s[SIZE];
    
  scanf("%d%d",&n,&m);
    
  for(int i=0;i<m;i++){
    scanf("%d%d",&a,&b);

    s[i] = max(n-a,0);
  }
  
  sort(s,s+m);

  int ans = 0;

  for(int i=0;i<m-1;i++){
    ans += s[i];
    debug(s[i]);
  }

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

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

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

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

  int mini = a[0];
  for (int i = 1; i < m; i++) {
    mini = min(a[i], mini);
  }

  if (mini < n) count -= n - mini;

  cout << count << endl;

  return 0;
}