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)
}

Python: # -*- coding: utf-8 -*-
"""
Point Card
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0631

"""
import sys


def solve(cards, n, m):
    ans = 0
    for _ in range(1, m):
        atari, _ = cards.pop()
        ans += max(0, n - atari)
    return ans


def main(args):
    n, m = map(int, input().split())
    cards = sorted([[int(i) for i in input().split()] for _ in range(m)])
    ans = solve(cards, n, m)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Python: n,m=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(m)]
less=[]
cnt=0
expense=0
for i in range(m):
    if ab[i][0]>=n:
        cnt+=1
    else:
        less.append(ab[i][1]-n)
if cnt<m-1:
    less.sort()
    expense=sum(less[:m-1-cnt])
print(expense)

Python: n,m=map(int,input().split())
count=0
s=[]
for i in range(m):
    a,b=map(int,input().split())
    if n>a:
        count+=1
        s.append(n-a)
if count<m-1:
    z=max(s)
    s.remove(z)
    print(sum(s))
else:
    print("0")

Python: # coding: utf-8

N, M = list(map(int, input().split(' ')))
cards = [int(input().split(' ')[0]) for i in range(M)]
cards = sorted(cards)

cost = 0
for i in range(1, M):
    if cards[i] >= N: break
    cost += N - cards[i]

print(cost)

Python: n, m = map(int, input().split())
cards = [list(map(int, input().split())) for _ in range(m)]
cards = sorted(cards, key=lambda x: x[0])[::-1]
cost = 0
for card in cards[:-1]:
    if card[0] < n:
        cost += n - card[0]

print(cost)

Python: n,m=map(int,input().split())
p=[list(map(int,input().split()))for i in range(m)]
c=[n-a if n>a else 0for a,b in p]
print(sum(c)-max(c))

Python: N, M = map(int, input().split())
C = [next(map(int, input().split())) for i in range(M)]
C.sort(reverse = True)
ans = 0
for i in range(M-1):
    ans += max(0, N - C[i])
print(ans)

Python: n,m=map(int,input().split())
p=[list(map(int,input().split()))for i in range(m)]
c=[n-a if n>a else 0for a,b in p]
print(sum(c)-max(c))

Python: n,m = map(int,input().split())

a = [0 for i in range(m)]
b = [0 for i in range(m)]


for i in range(m):
    a[i],b[i] = map(int,input().split())

a.sort()
c = 0

for i in range(m-1):
    if n <= a[i+1]:
        break
    else:
        c += n-a[i+1]

print(c)

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

vector<int>v;

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

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

using namespace std;

int main()
{
    vector<int> cost;
    int A,B;
    int N,M;
    int ans=0;

    cin >> N >> M;

    for(int i=0; i<M; ++i) {
        cin >> A >> B;
        cost.push_back(N-A);
    }

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

    for(int i=0; i<M-1; ++i) {
        ans+=(cost[i]>0?cost[i]:0);
    }
    cout << ans << endl;
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define lp(i,n) for(int i=0;i<n;i++)
#define lpin(a,i,n) for(int i=0;i<n;i++){cin>>a[i];}

int main(){
  int n,m;
  cin>>n>>m;
  int count=0,max=0,mc=0,a[10000];
  lp(i,m){
    int x,y;
    cin>>x>>y;
    if(x>=n) a[i]=0;
    else{
      a[i]=n-x;
      if(y>max){
	max=y;
	mc=i;
      }
    }
  }
  int ans=0;
  lp(i,m){
    if(mc==i) 1;
    else{ans+=a[i];}
  }
  cout<<ans<<endl;
	  

  return 0;
}

C++: #include<iostream>
#include<sstream>
#include<fstream>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<functional>
#include<utility>
#include<bitset>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cstdio>

using namespace std;

#define REP(i,n) for(int i=0;i<int(n);i++)
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
typedef long long ll;
typedef pair<int,int> P;

int main(void){
	          int i;

	          int N,M;
	          cin >> N >> M;

	          vector<int> v;
	          for(i=0;i<M;i++){
	        	  int A,B;
	        	  cin >> A >> B;
	        	  v.push_back(A);
	          }

	          sort(v.begin(),v.end());
	          reverse(v.begin(),v.end());

	          int ans=0,k=0;
	          for(i=0;i<M;i++){
	        	  if(v[i]>=N) k++;
	        	  else{
	        		  ans+=N-v[i];
	        		  k++;
	        	  }
	        	  if(k==M-1) break;
	          }

	          cout << ans << endl;


	          return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#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 fi first
#define se second
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> pip;
const ll INF = 1e9+100;
const ll MOD = 1e9+7;
const double EPS = 1e-10;
const bool debug = 0;
//------------------------------//

int N, M;
int v[1123];

int main() {
	cin >> N >> M;
	
	REP(i, M) {
		int a, b;
		scanf("%d %d", &a, &b);
		v[i] = a;
	}
	
	sort(v, v + M, greater<int>());
	
	int ans = 0;
	REP(i, M - 1) {
		ans += max(N - v[i], 0);
	}
	cout << ans << 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]) count++;
    if(A[i] < B[i]) l.push_back(B[i] - A[i]);
    }
  }
  l.sort();

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

  return 0;
}