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<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cstdlib>
using namespace std;
int main() {
	int n, m, now = 0, g = 0, mo = 0, p[1005][2], s[1005] = {0};
	cin >> n >> m;
	for (int i = 0; i < m; i++ ) {
		int a, b;
		cin >> a >> b;
		p[i][0] = a;
		p[i][1] = b;
		if (a >= b) {
			g++;
		}
		else {
			int c = n - a;
			
			for (int j = now; j >= 0; j--) {
				if (s[j-1] > c ) {
					s[j] = s[j-1];
					
				}
				else {
					s[j] = c;
					now++;
					
					break;
				}
				
				
			}
		}
	}
	
	
	for (int i = 0; i < m; i++) {
		if (g < m - 1) {
			mo += s[i];
			
			g++;
		}

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

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

int main(){
    int N, M, count, ans;
    count = 0;
    ans = 0;
    cin >> N >> M;
    int tmp1=0, tmp2=0;

    for (int i = 0; i < M; i++){
        int a, b;
        cin >> a >> b;
        if (a < N){
            tmp1 += (N-a);
            tmp2 = max(tmp2, (N-a));
        }
    }

    cout << tmp1 - tmp2 << endl;
    return 0;
}

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

int main()
{
	int n, m;
	int cards[1000];

	cin >> n >> m;
	for (int i = 0; i < m; i++)
	{
		int a, b;
		cin >> a >> b;
		cards[i] = a;
	}
	sort(begin(cards), end(cards), greater<int>());

	int ans = 0;
	for (int i = 0; i < m - 1; i++)
	{
		if (cards[i] >= n) continue;
		ans += (n - cards[i]);
	}
	cout << ans << endl;

	return 0;
}

C++: #include<iostream>
using namespace std;
#define INF 10000;
int main(){
	int n,m,a[1001],b[1001],atari=0,min=10000,ans=0,memo=0;
	bool bo=false;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		cin>>a[i]>>b[i];
		if(a[i]>=n){
			a[i]=INF;
			b[i]=INF;
			atari++;
			if(atari==m-1){
				bo=true;
			}
		}
	}
	while(1){
		for(int i=0;i<m;i++){
			if(bo){
				break;
			}
			if(min>n-a[i]&& a[i]<n){
				min=n-a[i];
				memo=i;
			}
		}
		if(bo){
			break;
		}
		ans+=n-a[memo];
		atari++;
		if(atari==m-1){
			break;
		}
		min=INF;
		b[memo]=INF;
		a[memo]=INF;
		memo=0;
	}
	cout<<ans<<endl;
	return 0;
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
 
using namespace std;
 
struct Info{
    Info(){
        good = bad = -1;
    }
    int good,bad;
};
 
int main(){
 
    int N,M,count = 0,index = 0,sum = 0;
 
    scanf("%d %d",&N,&M);
    Info info[M];
 
    int left,right;
 
    for(int loop = 0; loop < M; loop++){
        scanf("%d %d",&left,&right);
        if(left >= N){
            count++;
        }else{
            info[index].good = left;
            info[index].bad = right;
            index++;
        }
    }
 
    if(count < M-1){
        for(int i = 1; i <= index-1; i++){
            for(int k = index-1; k >= i; k--){
                if(info[k].good > info[k-1].good)swap(info[k],info[k-1]);
            }
        }
 
        for(int i = 0; i < index; i++){
            sum += (N-info[i].good);
            count++;
            if(count == M-1)break;
        }
    }
 
    printf("%d\n",sum);
 
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <math.h>
#include <bitset>
#define INF 2000000000
using namespace std;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
#define ll long long
#define PL pair<long long, long long>
#define P pair<int,int>
#define mk make_pair
#define en endl;
#define WHITE 0
#define BLACK 2
#define GRAY 1
#define RE return 0;
#define HERE cout<<"    "<<"HERE"<<en

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

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

int main()
{
	//合計時間

	int N, M;
	cin >> N >> M;
	int a;
	vector<int> A;
	int b=0;
	for (int i = 0; i < 2*M; i+=2) {
		cin >> a;
		if (a < N) {
			A.push_back(a);
			break;
		}
		b++;
	}
	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++: //PointCard
#include <iostream>
#include <queue>

using namespace std;

int main() {
    int N, M, a, b;
    priority_queue<int> PQ;
    cin >> N >> M;

    int cnt = 0, cost = 0;
    for(int i = 0; i < M; i++) {
        cin >> a >> b;
        if(a >= N) cnt++;
        else PQ.push(a);
    }

    while(cnt < M - 1) {
        cost += N - PQ.top();
        PQ.pop();
        cnt++;
    }

    cout << cost << endl;
}

C++: #include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

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

	int n,m; cin >> n >> m;
	P a[1005];
	for (int i = 0;i < m;i++){
		int t1,t2; cin >> t1 >> t2;
		a[i] = P(t1,t2);
	}

	sort(a,a+m);

	int ans = 0;

	for (int i = 1;i < m;i++){
		ans += max(0,n-a[i].first);
	}
	
	cout << ans << endl;

	return 0;

}