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+n);

  int ans = 0;

  for(int i=0;i<m-1;i++){
    ans += s[i];
  }

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

C++: #include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int main(){
    
    
    int a,b,c,d,p,e,n,f,x,y=0,z=0,m,i;
    
    cin>>n>>m;
    for(i=1;i<=m;i++){
        cin>>a>>b;
        if(a<n){
            x=n-a;
            z=z+x;
        }
        
        if(y<(n-a)){
            y=n-a;
            
        }
    }
    cout<<z-y<<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]){
      if((B[i] - A[i]) % 2 != 0){
        l.push_back(B[i] - A[i] + 1);
      }else{
        l.push_back(B[i] - A[i]);
      }
    }
  }
  l.sort();

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

  return 0;
}

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

int main(){
    int N, M, A[1024], B[1024], C[1024];
    
    cin >> N >> M;
    
    for(int i = 0;i < N;i++){
        cin >> A[i];
    }
    
    for(int i = 0;i < M;i++){
        cin >> B[i];
    }
    
    for(int i = 0;i < M;i++){
        for(int j = 0;j < N;j++){
            if(A[j] <= B[i]){
                C[j]++;
                break;
            }
        }
    }
    
    int index, num = -1;
    for(int i = 0;i < N;i++){
        if(C[i] > num){
            num = C[i];
            index = i + 1;
        }
    }
    
    cout << index << "\n";
    
    return 0;
}

C++: #include<iostream>
#include<stdio.h>
#include<string>
#include <algorithm>
#include<cstring>
#include <math.h>
#include <iomanip>
using namespace std;

int main()
{
  int n,m;
  int a,b;
  cin>>n>>m;
  int s=0,t=0;
  for(int i=0;i<m;i++){
    cin>>a>>b;
    if(a<n){
      s+=(n-a);
      t=max(t,n-a);
    }
  }
  cout<<s-t<<endl;
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
	int n,m,k=0,s,sa,sb;
	cin>>n>>m;
	s=n;
	int a[m],b[m]={};
	for(int i=0;i<m;i++){
		cin>>a[i]>>sb;
		if(s>a[i]){
			s=a[i];
			sa=i;
		}
		if(n>a[i]){
			b[i]=n-a[i];
			k+=b[i];
		}
	}
	cout<<k-b[sa]<<endl;
}

C++: #include <iostream>
using namespace std;
int main(){
	int n , m , a[100] , b[100];
	cin >> n >> m;
	for( int i=0 ; i<m ; i++ ){
			cin >> a[i] >> b[i] ;
	}
	for( int i=0 ; i<m ; i++ ){
		for( int j=0 ; j<m ; j++ ){
			if( a[i] > a[j] )
				swap( a[i] , a[j] );
		}
	}
	int ans=0;
	for( int i=0 ; i<m-1 ; i++ ){
		if( a[i] <= n )
			ans += n - a[i];
	}
	cout << ans << endl;
}

C++: #include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < (n); i++)
using namespace std;

int main(){
	int N,M;
	cin >> N >> M;
	int m = -1;
	int ans = 0;
	REP(i, M){
		int a,b;
		cin >> a >> b;
		if(b > a){
			ans += b-(a+b)/2;
			m = max(b-(a+b)/2, m);
		}
	}
	cout << ans-m<<endl;
	return 0;
}

C++: 
//17-2 ポイントカード
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<time.h>
#include<string>
#include<vector>
#include<map>
#include<list>
#include<set>
#include<stack>
#include<cstdlib>
#include<algorithm>
#include<random>
#include<cassert>
using namespace std;
#define LL long long
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define LL_MIN -9223372036854775808
#define LL_MAX 9223372036854775807
#define ROOP() while(true)
int main(){
    int N,M;
    cin >> N >> M;
    int maxmum = 0;
    int ans = 0;
    for(int i=0; i<M; i++){
        int A,B;
        cin >> A >> B;
        if(B>A){
            ans += (B-A)/2;
            maxmum = max(maxmum,(B-A)/2);
        }
    }
    cout << ans - maxmum << endl;
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int cards[1000];

int main() {

    int n, m, g, b, idx = -1, sum = 0;
    scanf("%d %d", &n, &m);
    for (int i = 0; i < m; i++) {
        scanf("%d %d", &g, &b);
        if (g >= n) continue;
        cards[++idx] = g;
    }
    if (idx < 1) printf("%d\n", 0);
    else {
        sort(cards, cards+idx+1, greater<int>());
        for (int i = 0; i < idx; i++) {
            sum += n - cards[i];
        }
        printf("%d\n", sum);
    }
    return 0;
}

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

int main() {
  int n,m;
  cin >> n >> m;
  int a[m],b[m];
  for(int i=0; i<m; i++) cin >> a[i] >> b[i];
  sort(a,a+m,greater<int>());
  int ans=0;
  for(int i=0; i<m-1; i++) ans+=n-min(n,a[i]);
  cout << ans << endl;
  return 0;
}

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

struct C{ 
	int A, B, t;
};

int main(){
	struct C c[2010], temp;
	int N, M, count = 0, minj, ans = 0;

	cin>>N>>M;

	for(int i = 0; i < M; i++){
		cin>>c[i].A>>c[i].B;
		if(c[i].A >= N){
			count++;
			c[i].t = 2 * N;
		} else {
			c[i].t = fabs(N - c[i].A);
		}
		
	}
	
	for(int i = 0; i < M; i++){
		minj = i;
		for(int j = i; j < M; j++){
			if(c[minj].t > c[j].t) minj = j;
		}
		temp = c[i];
		c[i] = c[minj];
		c[minj] = temp;
		
	}


	if(count >= M - 1){
		cout<<"0"<<endl;
	} else {
		for(int i = 0;count < M - 1; i++){
			ans += c[i].t;
			count++;
		}
		cout<<ans<<endl;
	}	


	return 0;
}

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

int main(){
	int n,m;
	cin>>n>>m;
	int a,b,sum=0,ma=0;
	for(int i=1;i<=m;i++){
		cin>>a>>b;
		ma=max(ma,n-a);
		sum+=max(0,n-a);
	}

	cout<<sum-ma<<endl;
	return 0;
}

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

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

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 <iostream>
using namespace std;
int main(){
	int n , m , a[1001] , b[1001];
	cin >> n >> m;
	for( int i=0 ; i<m ; i++ ){
			cin >> a[i] >> b[i] ;
	}
	for( int i=0 ; i<m ; i++ ){
		for( int j=0 ; j<m ; j++ ){
			if( a[i] > a[j] )
				swap( a[i] , a[j] );
		}
	}
	int ans=0;
	for( int i=0 ; i<m-1 ; i++ ){
		if( a[i] <= n )
			ans += n - a[i];
	}
	cout << ans << endl;
}