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++: // 2018/12/20 Tazoe

#include <iostream>
#include <algorithm>
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];
	}

	sort(A, A+M);

	int sum = 0;

	for(int i=1; i<M; i++){
		if(A[i]>=N)
			break;

		sum += (N-A[i]);
	}

	cout << sum << endl;

	return 0;
}

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

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

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

  cout << sum - ma << endl;
}

C++: #include<stdio.h>
int main(){
	int a,n,m,k[1001],d,d2;
	scanf("%d %d",&n,&m);
	for(a=0;a<m;a++){
		scanf("%d %d",&d,&d2);
		k[a]=n-d;
		if(k[a]<0){
			k[a]=0;
		}
	}
	for(d=0;d<m;d++){
		for(d2=d+1;d2<m;d2++){
			if(k[d]>k[d2]){
				a=k[d];
				k[d]=k[d2];
				k[d2]=a;
			}
		}
	}
	a=0;
	for(d=0;d<m-1;d++){
		a+=k[d];
	}
	printf("%d\n",a);
	return 0;
}

C++: 
#include <iostream>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <vector>
using namespace std;
int n;
int m;
int c[1000];
int d[1000];
int e;
int main(){
	cin >> n >> m;
	for (int i = 0; i < m; i++){
		cin >> c[i];
		cin >> d[i];
	}
	sort (c, c+m);
	for (int i = 1; i < m; i++){
		if (c[i] < n){
			e += n - c[i];
		}
	}
	cout << e << endl;

	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(void)
{
	int N,M,max1=0,all2=0;
	scanf("%d %d",&N,&M);
	int A[M]={0},B[M]={0};
	for(int i=0;i<M;i++)
	{
		scanf("%d %d",&A[i],&B[i]);
		if(A[i]>=N){}
		else if(max1<N-A[i])
		{
			all2+=max1;
			max1=(N-A[i]);
		}
		else
		{
			all2+=(N-A[i]);
		}
	}
	printf("%d\n",all2);
	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 <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(int (i) = (s);(i) <= (e);(i)++)
#define all(x) x.begin(),x.end()

int N,M;

vector<i64> A,B;

int main(){
  cin >> N >> M;
  A.resize(M);
  B.resize(M);
  rep(i,0,M - 1) cin >> A[i] >> B[i];
  vector<i64> vec;
  rep(i,0,M - 1){
    vec.push_back(B[i]);
  }

  sort(all(vec));
  int ans = 0;
  rep(i,0,M - 2){
    ans +=  max(0LL,vec[i] - N);
  }

  cout << ans << endl;
}

C++: #include<stdio.h>
int main(void)
{
	int i,j,k,box,n,m,a[1000],b[1000],w[1000],ans,cnt,max;
	scanf("%d %d",&n,&m);
	for(i=0;i<m;i++){
		scanf("%d %d",&a[i],&b[i]);
	}
	max=0;
	ans=0;
	cnt=0;
	for(i=0;i<m;i++){
		w[i]=b[i]-n;
		if(w[i]>0){
			cnt++;
			if(max<=w[i]){
				max=w[i];
			}
		ans+=w[i];
		}
	}
	if(cnt>1){
		printf("%d\n",ans-max);
	}
	else{
		printf("0\n");
	}
	return 0;
}

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

int main() {
	int N, M, ans = 0;
	cin >> N >> M;
	vector<pair<int, int>> AB(M);
	for (pair<int, int>& i : AB) cin >> i.first >> i.second;
	sort(AB.begin(), AB.end(), greater<pair<int, int>>());
	for (int i = 0; i < M - 1; ++i) ans += max(0, AB[i].second - N);
	cout << ans << endl;
}

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

int main()
{
	//合計時間

	int N, M;
	cin >> N >> M;
	int a;
	int A[5000];
	int b = 0;
	int x = 0;
	for (int i = 0; i <  M; i++) {
		cin >> a;
		if (a < N) {
			A[x]=a;
			x++;
		}
		else
		{
			b++;
		} 
		cin >> a;
	}
	int sum = 0;
	if (b < M - 1) {
		sort(A, A+x);
		reverse(A, A+x);
		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>
using namespace std;

int main()
{
  int N,M;
  cin >> N >> M;
  int A = 0,B = 0,C = 0;
  int max = 0;
  int m = 0;
  for(int i = 0; i < M; i++){
      cin >> A >> B;
      if(A < N){
          C = B - N;
          if(C > max){
              max = C;
          }
        m += C;
      }
  }
  int o = m - max;
  cout << o << endl;
  return 0;
      
}

C++: #include<stdio.h>
int main(void)
{
	int n,m,a[1001],b[1001],i,j,p,k=0;
	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]){
				p=a[i];
				a[i]=a[j];
				a[j]=p;
			}
		}
	}
	for(i=0;i<m-1;i++){
		if(a[i]<n) k+=n-a[i];
	}	
	printf("%d\n",k);
	return 0;
}

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

int main() {
	int N, M, CardNUM=0,ans=0;
	int A[1001], B[1001];
	vector<int> Price;
	cin >> N >> M;
	for (int i = 0; i < M; i++) {
		cin >> A[i] >> B[i];
	}

	for (int i = 0; i < M; i++) {
		if (A[i] >= N) {
			CardNUM++;
		}
		else if (A[i] < N) {
			Price.push_back(N - A[i]);
		}
	}

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

	for (int i = 0; i < M - CardNUM-1; i++) {
		if (i >= 0) {
			ans += Price.at(i);
		}
	}

	cout << ans << endl;

	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int main(){
int n,m,sum=0;
int a[1001],b[1001];
cin >> n >> m;
for(int i=0;i<m;i++){
cin >> a[i] >> b[i];
}
sort(a,a+m);
for(int i=1;i<m;i++){
if(n<=a[i]){
break;
}
sum+=n-a[i];
}
cout << sum << endl;
return 0;
}