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++: //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];
  }

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

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <map>
#include <queue>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using namespace std;

template<class T>
constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;

int main() {
    int n, m;
    cin >> n >> m;
    vector<int> v(m);
    for (auto &&i : v) {
        int a;
        scanf("%d %d", &i, &a);
    }
    ll ans = 0;
    sort(v.begin(), v.end(), greater<>());
    for (int i = 0; i < m-1; ++i) {
        if(v[i] < n) ans += n-v[i];
    }
    cout << ans << "\n";
    return 0;
}

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

using namespace std;

int main(){
	int N,M;
	scanf("%d %d",&N, &M);
	int small = 114514, ans = 0;;
	for(int i = 0; i < M; i++){
		int t, u;
		scanf("%d %d",&t, &u);
		if(t < N){
			ans += N - t;
			small = min(t, small);
		}
	}
	
	if(small != 114514) ans -= N - small;
	printf("%d\n", ans);
	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=0;
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 <iostream>
using namespace std;
int main(){

	int n,m,x,y,z;
	cin >> n >> m;
	x = 0;
	y = 0;
	z = 0;
	for(int i=0;i<m;i++){
		int a,b;
		cin >> a >> b;

		if( a < n ){
                        y = n-a;
			x = x+y
			z = max( z , y );
		}
	}

	cout << x-z << endl;

	return 0;
}

C++: #include<bits/stdc++.h>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
#define rep(i,n)for(ll i=0;i<n;i++)
using namespace std;
typedef long long ll;
typedef pair<int, int>P;

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

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<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
#include<stdio.h>
#include <cstdint>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

int n, m, cnt, ans;
int a[1001], b[1001], c[1001];


int main() {

	cin >> n >> m;
	rep(i, m) {
		cin >> a[i] >> b[i];
	}
	rep(i, m) {
		if (a[i] >= n)cnt++;
	}
	sort(a, a + m);
	reverse(a, a + m);
	rep(i, m) {
		if (cnt < m - 1) {
			if (a[i] < n) {
				while (a[i] < n) {
					ans++;
					a[i]++;
				}
				cnt++;
			}
		}
		else break;
	}

	cout << ans << endl;

	return 0;
}

C++: #include <cstdio>
#include <climits>
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <cmath>
#include <stack>
#include <algorithm>
#include <random>
#include <iomanip>
#include <map>
#include <queue>
#include <functional>
#include <numeric>
#include <chrono>
#include <cstdlib>
using ll = long long;
using namespace std;

const ll MOD = 1e9 + 7;
const double pi = acos(-1);
typedef pair<int, int> P;
#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define EPS 1e-4
#define OUTPUT(i) (cout << (ll)i << endl)
#define ALL(a) ((a).begin(), (a).end())

bool operator<(const pair<ll, ll> &a, const pair<ll, ll> &b)
{

    if (a.first == b.first)
        return a.second < b.second;

    return a.first < b.first;
}

ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
vector<int> v;
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    //cout << fixed << setprecision(15);

    int N, M;
    cin >> N >> M;
    int ans = 0;
    int cnt = 0;
    for (int i = 0; i < M; ++i)
    {
        int a, b;
        cin >> a >> b;
        int t = 0;
        if (a >= N)
            t = 0;
        else
        {
            t = N - a;
        }
        v.push_back(t);
    }
    sort(v.begin(), v.end());

    for (int i = 0; i < M - 1; ++i)
        ans += v[i];
    cout << ans << endl;
    return 0;
}

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

int main()
{
	int N, M;
	cin >> N >> M;
	vector<int> A(M);
	for (int i = 0; i < M; i++) {
		cin >> A[i] >> A[i];
	}
	sort(A.begin(), A.end());
	int res = 0;
	for (int i = 0; i < M - 1; i++) {
		res += max(0, A[i] - N);
	}
	cout << res << endl;
	return 0;
}

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

using namespace std;


int main(){
	int n, m;
	cin >> n >> m;
	int par[100];

	for(int i = 0; i < n; i++) cin >> par[i];

	auto fc = [&](int k){
		for(int i = 0; i < n-1; i++){
			if(par[i]%k > par[i+1]%k) swap(par[i], par[i+1]);
		}
		return;
	};

	for(int i = 1; i <= m; i++) fc(i);

	for(int i = 0; i < n; i++){
		cout << par[i] << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<utility>
#include<queue>
#include<algorithm>
#include<cmath>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long

using namespace std;
int main(){
  int n,m;
  cin>>n>>m;
  int cnt=0;
  vector<int>vi;
  for(int i=0;i<m;i++){
    int a,b;
    cin>>a>>b;
    if(a>=n)
      cnt++;
    else
      vi.pb(a);
  }
  int ans=0;
  sort(vi.begin(),vi.end());
  while(cnt<m-1){
    cnt++;
    ans+=n-vi[vi.size()-1];
    vi.pop_back();
  }
  cout<<ans<<endl;
  return 0;
}