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 <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e16;
const ll mod = 1000000007;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

template<class T> inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template<class T> inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}
ll factorial(ll n) {
  if (n == 1) {
    return 1;
  }
  ll ans = factorial(n-1);
  return ans*n;
}
ll gcd(ll a, ll b) {
  if (b == 0) return a;
  return gcd(b, a%b);
}
ll lcm(ll a, ll b) {
  return (a/gcd(a, b))*b;
}
bool is_prime(ll n) {
  for (ll i = 2; i*i <= n; i++) {
    if (n%i == 0) return false;
  }
  return n != 1;
}
ll mod_pow(ll x, ll n, ll mod) {
  if (n == 0) return 1;
  ll res = mod_pow(x*x%mod, n/2, mod);
  if (n & 1) res = res * x % mod;
  return res;
}

int main() {
    ll n, m;
    cin >> n >> m;
    priority_queue <ll> pq;
    ll tmp = 0;
    rep(i, m) {
        ll a, b;
        cin >> a >> b;
        if (a >= n) {
            tmp++;
        } else {
            pq.push(a);
        }
    }
    if (tmp >= m-1) {
        cout << 0 << endl;
    } else {
        ll res = 0;
        rep(i, m-1-tmp) {
            ll j = pq.top(); pq.pop();
            res += n-j;
        }
        cout << res << endl;
    }
}

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++: #include <bits/stdc++.h>

using namespace std;

int n, m;
vector <int> a;

int main(){
	scanf("%d%d", &n, &m);
	
	int cnt = 0;
	
	for(int i = 0; i < m; i++){
		int x, y;
		scanf("%d%d", &x, &y);
		
		if(x >= n) cnt++;
		else
		a.push_back(n - x);
	}
	
	if(cnt >= m - 1)
	return 0 * printf("0\n");
	
	sort(a.begin(), a.end());
	
	int res = 0;
	int diff = m - 1 - cnt;
	
	for(int i = 0; i < diff; i++)
	res += a[i];
	
	printf("%d\n", res);
}

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 << flush;
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<string>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<climits>
#include<set>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef pair<ll, ll> pll;
//int dx[4] = { 1,0,0,-1 };
//int dy[4] = { 0,1,-1,0 };
#define MOD 1000000007
#define ARRAY_MAX 100005

const int INF = 1e9 + 7;


int main() {


	ios::sync_with_stdio(false);
	cin.tie(0);

	int n, m;
	cin >> n >> m;
	vector<pii> a(m);
	for (int i = 0; i < m; i++)
	{
		cin >> a[i].first >> a[i].second;
	}
	sort(a.begin(), a.end(), greater<pii>());
	
	int cnt = 0;
	int ans = 0;
	for (int i = 0; i < m; i++) {
		if (a[i].first >= n)
		{
			cnt++;
		}
		else
		{
			ans += (n - a[i].first);
			cnt++;
		}
		if (cnt == m - 1) {
			break;
		}
	}

	cout << ans << endl;


	
	return 0;
}

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

using namespace std;

const int M = 1000, N = 1000;
int a[N] = { 0 };

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

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

int main(void){
	int n,m,a,b,mx=0,sum=0,add,i;
	cin >> n >> m;
	for(i=0; i<m; i++){
		cin >> a >> b;
		add = max(b-n,0);
		sum += add;
		mx = max(mx,add);
	}
	cout << (sum-mx) << endl;
	return 0;
}

C++: #include <bits/stdc++.h>
  
using namespace std;
  
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define pb push_back
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()
#define fi first
#define se second
  
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;

//#define DEBUG_MODE
#ifdef DEBUG_MODE
#define dump(x) cout << #x << " : " << x << endl
#define LINE cout << "line : " << __LINE__ << endl
#define dumpV(v) cout << #v << " : ["; for(auto& t : v) cout << t << ", "; cout<<"]" << endl;
#define STOP assert(false);
#else
#define dump(x) ;
#define LINE 	;
#define dumpV(v);
#define STOP 	;
#endif

namespace std{
  template<class S,class T>
  ostream &operator <<(ostream& out,const pair<S,T>& a){
    out<<'('<<a.fi<<", "<<a.se<<')';
    return out;
  }
}


int main(){
	int n, m;
	cin >> n >> m;
	vi a(m), b(m), c(m);
	int sum = 0;
	rep(i, m) {
		cin >> a[i] >> b[i];
		c[i] = max(0, n - a[i]);
		sum += c[i];
	}
	int ans = 1e9;
	dump(sum);
	rep(i, m) {
		ans = min(ans, sum - c[i]);
		dump(c[i]);
	}
	cout << ans << endl;
	return 0;
}

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

using namespace std;

int main(void) {
  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(0, n - a[i]);
  }

  cout << ans << endl;

  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
  int n,m;cin>>n>>m;
  int s=0,t=0;
  for(int i=0;i<m;i++){
    int a,b;
    cin>>a>>b;
    if(a<n) s+=(n-a),t=max(t,n-a);
  } 
  cout<<s-t<<endl;
  return 0;
}