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"
using namespace std;

int main() {
	int n, m;
	cin >> n >> m;
	int atari, hazure;
	int maxCost = 0, cost = 0;
	for (int i = 0; i < m; i++) {
		cin >> atari >> hazure;
		if (atari < hazure) {
			cost += n - atari;
			if (n - atari > maxCost) {
				maxCost = n - atari;
			}
		}
	}
	int ans = cost - maxCost;
	cout << ans << endl;
}

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);
	mx=0;
	sum=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;
}

C++: 
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
using namespace std;

int main(void)
{
	int n, m;
	scanf("%d %d", &n, &m);
	int total = 0;
	int ignore = 0;
	for (int i = 0; i < m; ++i)
	{
		int a, b;
		scanf("%d %d", &a, &b);
		if (n > a)
		{
			int diff = n - a;
			total += diff;
			ignore = max(ignore, diff);
		}
	}
	printf("%d\n", total - ignore);
	return 0;
}

C++: #include <bits/stdc++.h>
#define range(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
using namespace std;

int main() {
    int n, m;
    cin >> n >> m;
    vector <int> va;
    int cnt = 0;
    rep (i, m) {
        int a, b;
        cin >> a >> b;
        if (a < n) va.push_back(a);
        else       cnt++;
    }

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

    int ans = 0;
    while(cnt++ < m - 1) {
        ans += n - va.back();
        va.pop_back();
    }
    cout << ans << endl;

    return 0;
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
long long rui(int n) {
	if (n == 0) {
		return 1;
	}
	else {
		long long x = rui(n - 1);
		return 2 * x;
	}
}
int main() {
	int n, m,yo;
	cin >> n >> m;
	vector<int> a(m);
	for (int i = 0; i < m; i++) {
		cin >> a[i]>>yo;
	}
	sort(a.begin(), a.end());
	yo = 0;
	for (int i = 1; i < m; i++) {
		yo += n - min(n, a[i]);
	}
	cout << yo << endl;
}

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(A.begin(), A.end(), greater<int>());
  int ans = 0;
  for (int i = 0; i < M - 1; ++i)
    ans += max(N - A[i], 0);
  cout << ans << endl;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main(){
	int n,m,a,b;
	int sum=0,big=0;
	cin>>n>>m;
	for(int u=0;u<m;u++){
		cin>>a>>b;
		sum+=max(0,n-a);
		big=max(max(0,n-a),big);
	}
	cout<<sum-big<<endl;
	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];
			
			g++;
		}

	}
	
	cout << mo << endl;
	return 0;
}

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

using lint = long long;
#define reps(i, a, n) for (lint i = (a); i < (lint)(n); ++i)
#define rep(i, n) reps(i, 0, n)
struct pre_{pre_(){cin.tie(0);ios::sync_with_stdio(0);}} pre__;
// }}}

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

C++: #include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define repr(i, n) for(int i = n; i >= 0; i++)
#define REP(i, m, n) for(int i = m; i < n; i++)
#define SORT(v) sort((v).begin(), (v).end())
#define REV(v) sort((v).begin(), (v).end(), greater<int>())
#define INF 1000000007
typedef long long ll;
int gcd(int a,int b){return b?gcd(b,a%b):a;};
int lcm(int a,int b){return a * b / gcd(a, b);};
int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main() {
  int n, m;
  cin >> n >> m;
  int a[m], b[m];
  int cnt = 0, ans = 0;
  rep(i, m) {
    cin >> a[i] >> b[i];
    if(a[i] < n) cnt += n - a[i];
  }
  sort(a, a + m);
  if(a[0] < n) cnt -= n - a[0];
  cout << cnt << endl;
  return 0;
}

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

using namespace std;

#define ZFor(i,n) for(int i=0;i<n;i++)
#define WFor(i,n) for(int i=1;i<=n;i++)
#define ZWFor(i,n) for(int i=0;i<=n;i++)
#define WZFor(i,n) for(int i=1;i<n;i++)
#define Max -2100000000
#define Min 2100000000
#define F first
#define S second
#define F1 first.first
#define F2 first.second
#define F3 second

typedef pair<int,int> P;
typedef pair<pair<int,int>,int> PP;


int main(void){
	
	int n,m,a[1001],b[1001],cant=0,ans=0,c[1001];
	
	scanf("%d %d",&n,&m);
	
	ZFor(i,m) scanf("%d %d",&a[i],&b[i]);
	
	ZFor(i,m) c[i]=a[i]-b[i];
	
	sort(c,c+m,greater<int>());
	//ZFor(i,m) printf("%d\n",c[i]);
	ZFor(i,m-1){
		if(c[i]<0){
			ans+=(c[i]*-1)/2;
		}
	}
	printf("%d\n",ans);
	
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int n, m, a[10000], b[10000];
vector<int>vec;
int main() {
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		cin >> a[i] >> b[i];
		if (a[i] < b[i])vec.push_back(b[i] - n);
	}
	sort(vec.begin(), vec.end());
	int r = 0;
	for (int i = 0; i < (int)vec.size() - 1; i++) {
		r += vec[i];
	}
	cout << r << endl;
	return 0;
}

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

C++: #include<stdio.h>
int main(void)
{
	int a,b,m,i,n,x,y,z;
	x=0;
	y=0;
	z=0;
	scanf("%d %d",&n,&m);
	for(i=0;i<m;i++){
		scanf("%d %d",&a,&b);
		if(a>=n){
			x=0;
		}
		if(a<n){
			x=n-a;
		}
		if(y<x){
			y=x;
		}
		z=z+x;
	}
	z=z-y;
	printf("%d\n",z);
	
	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;
}