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++: //============================================================================
// Name        : JOI.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;
int A[1001],B[1001];
int N,M;
int D;

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

	int T=0;
	int S[1001]={};
	for(int i=0;i<M;i++){
		if(A[i]>=N){
			S[i]=0;
		}
		if(A[i]<N){
			S[i]=N-A[i];
			T+=S[i];
		}
	}

	int R=0;
	for (int i=0;i<M;i++){
		if (R<S[i])R=S[i];
	}
	cout<<T-R<<endl;
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<string>
#include<sstream>
#include<cmath>
#include<numeric>
#include<map>
#include<stack>
#include<queue>
#include<cassert>
using namespace std;
int inf = 1000000000;

int main(void) {

  int n, m;
  cin >> n >> m;
  vector< pair<int, int> > p(m);
  int cnt = 0;
  for(int i=0; i<m; i++){
    cin >> p[i].first >> p[i].second;
    if( p[i].first >= n ) cnt++;
  }

  if( cnt >= m-1 ) cout << 0 << endl;
  else{
    int ans = 0;
    sort(p.begin(), p.end());
    for(int i=0; i<m; i++){
      if( p[m-1-i].first >= n ) continue;
      ans += max(0, n-p[m-1-i].first);
      cnt++;
      if( cnt >= m - 1 ) break;
    }
    cout << ans << endl;
  }

  return 0;
}


// EOF

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

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

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

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

C++: #include <iostream>
#include<algorithm>
#include<math.h>
using namespace std;
int main()
{
    int N,M,sum = 0,nmax = 0;
    cin>>N>>M;
    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;
}

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

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

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

using namespace std;

int main()
{
  int N, M;
  cin >> N >> M;
  int ret = 0, out = 0;
  for(int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    ret += max(0, (B - A) / 2);
    out = max(out, (B - A) / 2);
  }
  cout << ret - out << endl;
}

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

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

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

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

#define FF first
#define SS second
template<class S, class T>
istream& operator>>(istream& is, pair<S,T>& p){
  return is >> p.FF >> p.SS;
}
template<class S, class T>
ostream& operator<<(ostream& os, const pair<S,T>& p){
  return os << p.FF << " " << p.SS;
}
template<class T>
void maxi(T& x, T y){
  if(x < y) x = y;
}
template<class T>
void mini(T& x, T y){
  if(x > y) x = y;
}


const double EPS = 1e-10;
const double PI  = acos(-1.0);
const LL MOD = 1e9+7;

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

  int N, M;
  cin >> N >> M;
  VI xs(M);
  REP(i,M){
	int a, b;
	cin >> a >> b;
	xs[i] = max(0, N - a);
  }
  SORT(xs);

  cout << accumulate(ALL(xs), 0) - xs.back() << 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=0;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;
}