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;
#define rep(i,n) for (int i = 0; i < (n); i++)
#define repd(i,a,b) for (int i = (a); i < (b); i++)
#define INF LLONG_MAX
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

int main(void)
{
	ll N, M;
	cin >> N >> M;
	vector<pair<ll, ll> > list(M);
	rep(i, M)
	{
		int A, B;
		cin >> A >> B;
		list[i] = make_pair(A, B);
	}

	sort(list.begin(), list.end());
	ll price = 0;
	repd(i, 1, M)
	{
		if(N>list[i].first) price += N - list[i].first;
		else break;
	}
	cout << price << endl;
}

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

int n, m, a[1010], b[1010];
vector<int> v;

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

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

#define rep(i, n) for(int i = 0; i < n; ++i)

using namespace std;

int n, m;

int main(){
  scanf("%d%d", &n, &m);
  int s = 0, M = 0;
  rep(i, m){
    int a, b;
    scanf("%d%d", &a, &b);
    int v = max(n - a, 0);
    s += v;
    M = max(v, M);
  }
  printf("%d\n", s - M);
  return 0;
}

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

int main()
{
	//合計時間

	int N, M;
	cin >> N >> M;
	int a,x;
	vector<int> A;
	int b = 0;
	for (int i = 0; i < 2 * M; i++) {
		cin >> a;
		if (a < N) {
			A.push_back(a);
			break;
		}
		b++;
		cin >> x;
	}
	int sum = 0;
	if (b < M - 1) {
		sort(A.begin(), A.end());
		reverse(A.begin(), A.end());
		for (int i = 0; i < M - 1 - b; i++) {
			sum += N - A[i];
		}
	}
	cout << sum << endl;

	/*
	int sum=0, n;
	for (int i = 0; i < 4; i++) {
	cin >> n;
	sum += n;
	}
	cout << sum / 60 << endl;
	cout << sum % 60 << endl;
	//平均点
	/*
	int P[5];
	int sum=0;
	for (int i = 0; i < 5; i++) {
	cin >> P[i];
	if (P[i] < 40) P[i] = 40;
	sum += P[i];
	}
	cout << sum / 5 << endl;
	*/
	//投票
	/*
	int N, M;
	cin >> N >> M;
	int A[1000], vote[1000];
	for (int i = 0; i < N; i++) {
	cin >> A[i];
	vote[i] = 0;
	}
	int b;
	for (int i = 0; i < M; i++) {
	cin >> b;
	for (int j = 0; j < N; j++) {
	if (A[j] <= b) {
	vote[j]++;
	break;
	}
	}
	}
	int temps = 0, ans;
	for (int i = 0; i < N; i++) {
	if (vote[i] > temps) {
	temps = vote[i];
	ans = i;
	}
	}
	cout << ans + 1 << endl;
	*/

	//2.すごろく	
	/*
	int n;
	cin >> n;
	int A, _A = 1;
	int k = 0;
	int A_count[100];
	A_count[0] = 1;
	for (int i = 0; i < n; i++)
	{
	cin >> A;
	if (A == 1)
	{
	if (A == _A)
	{
	A_count[k]++;
	}
	else
	{
	k++;
	A_count[k] = 2;
	}
	}
	_A = A;
	}
	sort(A_count, A_count + k + 1);
	cout << A_count[k] << endl;
	*/

	//タイムカード
	/*
	int t[2][3];
	int x[3][3];
	for (int n=0; n < 3; n++)
	{
	for (int i=0; i < 2; i++)
	for (int k=0; k < 3; k++)
	cin >> t[i][k];

	for (int m = 2; m >= 0; m--)
	{
	x[n][m] = t[1][m] - t[0][m];
	if (t[0][m] > t[1][m])
	{
	x[n][m] += 60;
	t[1][m - 1]--;
	}
	}
	}
	for (int n= 0; n < 3; n++)
	{
	cout << x[n][0] << ' ' << x[n][1] <<' '<< x[n][2] << endl;
	}
	*/



	return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cstdlib>
using namespace std;
int main() {
	int n, m,g=0,mo=0,pt[1005][2];
	cin >> n >> m;
	for (int i = 0; i < m; i++ ) {
		int a, b;
		cin >> a >> b;
		pt[i][0] = a;
		pt[i][1] = b;
		if (a >= b) {
			g++;
		}
	}
	
	for (int i = 0; i < m; i++) {
		if (g < m - 1) {
			if (pt[i][0] < n) {
				mo += n - pt[i][0];
				g++;
			}
		}

	}
	
	cout << mo << endl;
	return 0;
}

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

using namespace std;
 
int main(){
	
	int N, M;
    cin >> N >> M;
    
    int sum = 0;
    int maxs = 0;
    
    for(int i=0;i<M;i++){
    int a, b;
    cin >> a >> b;
    
    if(a >= N)
	continue;
	
    sum += (N-a);
    maxs = max(maxs, N-a);  
	  
  }
  cout << sum - maxs << "\n";
  return 0;
}

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<P> 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<stdio.h>
int main(void)
{
	int N,M,A[1001],B[1001],x,sum=0,i,j;
	scanf("%d%d",&N,&M);
	for(i=0;i<M;i++){
		scanf("%d%d",&A[i],&B[i]);
	}
	for(i=0;i<M;i++){
		for(j=i+1;j<M;j++){
			if(A[i]<A[j]){
				x=A[i];
				A[i]=A[j];
				A[j]=x;
			}
		}
	}
	for(i=0;i<M-1;i++){
		if(A[i]<N){
			sum+=N-A[i];
		}
	}
	printf("%d\n",sum);
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<set>
using namespace std;
int main() {
	int n, m;
	cin >> n >> m;
	multiset<int>a;
	int sum = 0,co=0;
	for (int z = 0; z < m; z++) {
		int b, c;
		cin >> b >> c;
		if (b >= n)
			co++;
		else {
			a.insert(n - b);
		}
	}
	set<int>::iterator it=a.begin();
	while (co < m - 1) {
		sum += (*it);
		it++;
		co++;
	}
	cout << sum << endl;
}

C++: #include<stdio.h>
#include<iostream>
using namespace std;
int main(void)
{
	int N,M,i,j,A[100],B[100];
	int K[100],sum=0;

	cin >> N >> M ;

	for(i=0;i!=M;i++){
		
		cin >> A[i] >> B[i];
		
		if(A[i]<N)
			K[i] = N-A[i];
		
		else
			K[i] = 0;

	}
	for(i=0;i!=M;i++){
		for(j=i+1;j<M;j++){
			if(K[i]>K[j]){
				sum=K[i];
				K[i]=K[j];
				K[j]=sum;
			}
		}
	}
	sum=0;
	for(i=0;i<M-1;i++){
		sum+=K[i];
	}
	cout << sum;
	}

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

int main()
{
   int N, M, m, A, B = 0;
   vector<int> v;
   
   cin >> N >> M;
   m = M - 1;
   
   for(int i = 0;i < M;i++){
        cin >> A >> B;
        if(A >= N){
            m--;
        }else{
            v.push_back(N - A);
        }
   }
   
   sort(v.begin(), v.end());
   
   int r = 0;
   for(int i = 0;i < m;i++){
       r += v.at(i);
   }
   
   cout << r << endl;
   
   return 0;
}

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

int main()
{
    int n,m;
    cin >> n >> m;
    vector<int> a,b;
    int p,l;
    for(int i=0;i<m;i++){
        cin >> p >> l;
        a.push_back(p);
        b.push_back(l);
    }
    sort(a.begin(),a.end());
    int sum=0;
    for(int i=1;i<m;i++){
        if(a[i]<n) sum+=(n-a[i]);
    }
    cout << sum << endl;
    return 0;
}