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;
using vb=vector<bool>;
using vvb=vector<vb>;
using vd=vector<double>;
using vvd=vector<vd>;
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll>;
using tlll=tuple<ll,ll,ll>;
using vs=vector<string>;

#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define rep(i,n) range(i,0,n)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define range(i,a,n) for(int i=(a);i<(n);i++)

#define LINF ((ll)1ll<<60)
#define INF ((int)1<<30)
#define EPS (1e-9)
#define MOD (1000000007ll)
#define fcout(a) cout<<setprecision(a)<<fixed
#define fs first
#define sc second
#define PI 3.1415926535897932384

int dx[]={1,0,-1,0,1,-1,-1,1},dy[]={0,1,0,-1,1,1,-1,-1};

template<class T>bool chmax(T&a,T b){if(a<b){a=b; return true;}return false;}
template<class T>bool chmin(T&a,T b){if(a>b){a=b; return true;}return false;}
template<class S>S acm(vector<S>&a){return accumulate(all(a),S());}
template<class S>S max(vector<S>&a){return *max_element(all(a));}
template<class S>S min(vector<S>&a){return *min_element(all(a));}
void YN(bool b){cout<<(b?"YES":"NO")<<"\n";}
void Yn(bool b){cout<<(b?"Yes":"No")<<"\n";}
void yn(bool b){cout<<(b?"yes":"no")<<"\n";}

ll max(int a,ll b){return max((ll)a,b);} ll max(ll a,int b){return max(a,(ll)b);}
template<class T>void puta(T&&t){cout<<t<<"\n";}
template<class H,class...T>void puta(H&&h,T&&...t){cout<<h<<' ';puta(t...);}
template<class S,class T>ostream&operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class S>auto&operator<<(ostream&os,vector<S>t){bool a=1; for(auto s:t){os<<(a?"":" ")<<s;a=0;} return os;}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	ll n,m,mn=100000,ans=0;
	ans=0;
	cin>>n>>m;
	vl a(m),b(m);
	rep(i,m){
		cin>>a[i]>>b[i];
		chmin(mn,a[i]);
		ans+=max(0,n-a[i]);
	}
	ans-=max(0,n-mn);
	cout<<ans<<endl;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF (1LL<<59)

#define int ll

signed main(){
    int n,m;
    cin>>n>>m;
    vector<int> less(m);
    rep(i,m){
        int a,b;
        cin>>a>>b;
        if(a>=n)less[i]=0;
        else less[i] = n-a;
    }
    sort(all(less));
    int sum = 0;
    rep(i,m-1){
        sum+=less[i];
    }
    cout<<sum<<endl;
}

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


int N,M,x,y;

int main(){
	int sum = 0, mx = 0;
	scanf("%d%d",&N,&M);
	for(int i = 0; i < M; i++){
		scanf("%d%d",&x,&y);
		if(x < N){
			sum += N - x;
			if(N - x > mx){
				mx = N -x;
			}
		}
	}
	printf("%d\n",sum - mx);
	return 0;
}

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

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x,n) bitset<n>(x)
#define PI 3.14159265358979323846

typedef long long ll;
typedef pair<string,int> P;
typedef pair<int,P> PP;

//-----------------------------------------------------------------------------

int n,m,cnt,ma,sum;

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

	cin>>n>>m;
	REP(i,m) {
		int a,b;cin>>a>>b;
		if(a<n) ma=max(ma,(n-a)),sum+=(n-a);
	}
	cout<<sum-ma<<endl;

	return 0;
}

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

int main(){
  int N,M;
  cin >> N >> M;

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

  list<int> l;
  int count = 0;
  for(int i = 0; i < M; i++){
    if(A[i] >= B[i] * 2) count++;
    if(A[i] < B[i]) l.push_back(B[i] - A[i]);
  }
  l.sort();

  int ans = 0;
  for(int i = 0; ans < M - 1 - count; i++){
    ans += l.front() / 2;
    l.pop_front();
  }
  cout << ans;

  return 0;
}

C++: #include <iostream>
using namespace std;
int main(void){
    // Your code here!
    int n, m, a, h, i, ans=0, max=0, t, shot = 0;
    cin >> n >> m;
    for (i = 0; i < m; i++) {
        cin >> a >> h;
        if (n > (a + h)) {
            shot++;
        } else {
            if(a < n) {
                ans += t = n - a;
                if (t > max) {
                    max = t;
                }
            }
        }
    }
    if (shot == 1) {
        cout << ans << endl;

    } else {
        cout << ans - max << endl;
    }
    return 0;
}

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

int main() {
    int N;              // 2N個のマスのうちN個当たりだとok
    int M;              // M - 1個以上のあたりが欲しい
    int A_lt_N[1000];   // 当たりがNより少ない時
    int keihin = 0;     // もらえる景品の数
    int yen = 0;        // 払うお金

    cin >> N >> M;
    for (int i = 0; i < M; i++) {
        int A;         // 当たりの数
        int B;         // はずれの数
        cin >> A >> B;
        if (A < N) {
            A_lt_N[i - keihin] = A;
        } else {
            keihin++;
        }
    }

    int num_array = M - keihin + 1;
    while (keihin < M - 1) {
        int maxi = 0;
        int max_index = -1;
        for (int i = 0; i < num_array - 1; i++) {
            if (A_lt_N[i] >= maxi) {
                maxi = A_lt_N[i];
                max_index = i;
            }
        }
        yen += N - maxi;
        keihin++;
        swap(A_lt_N[max_index], A_lt_N[num_array-2]);
        num_array--;

    }
    cout << yen << '\n';
    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 += n - point[i];
		++i;
		--m;
	}

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

	return 0;
}