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++: //https://onlinejudge.u-aizu.ac.jp/challenges/sources/JOI/Prelim/0631?year=2017

#include <bits/stdc++.h>

using namespace std;

int main() {
    int n, m;
    cin >> n >> m;

    vector<int> vt;
    for (int i = 0; i < m; ++i) {
        int a, b;
        cin >> a >> b;
        vt.emplace_back(max(n - a, 0));
    }

    sort(vt.begin(), vt.end());
    cout << accumulate(vt.begin(), vt.begin() + m - 1, 0) << endl;
    return 0;
}

/*
 * カードが全部でm枚あるうちのm-1枚を「当たり」にしたい。
 * 1枚は無視してよい。
 *
 */

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
#include <deque>
#include <iomanip>
#include <limits>
#include <fstream>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
typedef pair<int, int> P;
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> v(m);
  FOR(i,0,m){
      int a, b;
      cin >> a >> b;
      v[i] = P(a, b);
  }
  int ans = 0;
  sort(v.begin(), v.end());
  FOR(i,1,m){
      if(v[i].first < n){
          ans += n - v[i].first;
      }
  }
  cout << ans << endl;
}

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

int main(){
  int N,M,cnt = 0, ans = 0;

  cin >> N >> M;
  
  int *a,*b;

  a = new int[M];
  b = new int[M];
  
  for(int i = 0;i<M;++i){
    cin >> a[i] >> b[i];

    if(a[i] >= N){
      ++cnt;
    }
  }
  
  sort(a,a+M);
  reverse(a, a+M);

  int counter = 0;
  for(int i=cnt+1; i<=M-1;++i){
    if(a[counter] < N){
      ans += N - a[counter];
    }else{
      --i;
    }
    ++counter;
  }

  cout << ans << endl;
			    
  delete[] a;
  delete[] b;
  return 0;
}

C++: //============================================================================
// Name        : AOL.cpp
// Author      : Akari Fujii
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <cctype>
using namespace std;

int main() {

int n;
int m;
int a;
int b;
int e=0;
int o=0;
int d;
cin>>n>>m;

	for(int i=1; i<m+1; i++){
		cin>>a;
		cin>>b;
		d=b-a;
		d=d/2;
		if(d>0){
		e+=d;
		}
		if(o<d){
			o=d;
		}

	}
cout<<e-o<<endl;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int  main(void)
{
	int N,M,a,b,c[1000]={0},d,e=0,f=0,g;
	cin>>N>>M;
	for (a=0;a<M;a++){
		cin>>b>>d;
		c[a]=b;
	}
	sort(c,c+1000);
	for (a=999;a>=0;a--){
		if (c[a]>=N/2){
			e++;
		}
		if (c[a]<N/2){
			for (;;){
				c[a]=c[a]+1;
				f++;
				if (c[a]>=N/2){
					e++;
					break;
				}
			}
		}
		if (e>=M-1){
			break;
		}
	}		
	cout<<f<<endl;
	return 0;
}

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


int main() {
	int N, M; cin >> N >> M;
	vector<int>vs;
	for (int i = 0; i < M; ++i) {
		int a, b; cin >> a >> b;
		vs.push_back(max(0, b - N));
	}
	sort(vs.begin(), vs.end());
	int ans = accumulate(vs.begin(), vs.end() - 1, 0);
	cout << ans << endl;
	return 0;
}

C++: #include <cmath>
#include <string>
#include <vector>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <functional>
#pragma warning(disable : 4996)
using namespace std;
int n, m, a[1009], b[1009];
int main() {
	cin >> n >> m;
	for (int i = 0; i < m; i++) cin >> a[i] >> b[i];
	sort(a, a + m, greater<int>());
	int ret = 0;
	for (int i = 0; i < m - 1; i++) {
		if (a[i] < n) ret += n - a[i];
	}
	cout << ret << endl;
	return 0;
}

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

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

  int cnt = 0;
  int minA = 2001;
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;

    cnt += max(N - A, 0);
    minA = min(A, minA);
  }
  cnt -= max(N - minA, 0);

  cout << cnt << endl;

  return 0;
}

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

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

//#define int long long
#define DBG 0
#define dump(o) if(DBG){cerr<<#o<<" "<<(o)<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);

signed main() {
	int N, M; cin >> N >> M;
	int ans = 0;
	vector<int> a(M), b(M); rep(i, 0, M) {
		cin >> a[i] >> b[i];
		ans += max(0, N - a[i]);
	}
	sort(all(a));
	dumpc(a);
	dump(ans);
	ans -= max(0, N - a[0]);
	cout << ans << endl;
	return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<functional>
using namespace std;
int main()
{
	int n,m,a[1000],b,i,j,x,c;
	scanf("%d %d",&n,&m);
	for(i=0;i<m;i++){
		scanf("%d %d",&a[i],&b);
	}
	sort(a,a+m,greater<int>());
	c=0;
	for(i=0;i<m-1;i++){
		if(a[i]<n){
			c+=n-a[i];
		}
	}
	printf("%d\n",c);
	return 0;
}

C++: #include <iostream>
using namespace std;
#define rep(i,q) for(int i = 0; q > i; i++)
int main(){
  int n,m;
  cin >> n >> m;
  int a[m];
  int ok[m];
  int max = 0;
  rep(i,m){
    int tmp,tmp2;
    cin >> tmp >> tmp2 ;
    if(tmp >= tmp2){
      ok[i] = 0;
    } else {
      ok[i] = n - tmp;
      if(ok[i] > max) max = ok[i];
    }
  }
  int sm = 0;
  rep(i,m){
    if(ok[i] != max) sm += ok[i];
  }
  cout << sm << endl;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main() {
	int N, M;
	cin >> N >> M;
	int nmax = 0, count = 0;
	int a, b;
	for (int i = 0; i < M; ++i) {
		cin >> a >> b;
		if (a < N) {
			count += N - a;
			nmax = max(nmax, N - a);
		}
	}
	cout << count - nmax << endl;
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main()
{
    int n, m;
    cin >> n >> m;
    int A[m], B[m];
    int k = 0;
    for(int i = 0; i < m; i++)
    {
        cin >> A[i] >> B[i];
    }
    sort(A, A+m);
    for(int j = 1; j < m; j++)
    {
        k = k + max(0, n - A[j]);
    }
    cout << k << endl;
    
}