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)
}

Python: import sys

def main():
    N,M = map(int,input().split())
    arr = []
    for i in range(M):
        x,y = map(int,input().split())
        arr.append(x)
    arr = sorted(arr,reverse=True)
    ans = 0
    cnt = 0
    for x in arr:
        if cnt >= M-1:
            break
        cnt += 1
        if x < N:
            ans += N - x
    print(ans)
        
if __name__ == "__main__":
    main()

Python: n, m = map(int, input().split())
count = [0 for _ in range(m)]
for i in range(m):
    b = list(map(int, input().split()))[1]
    count[i] = max(0, b - n)
print(sum(count) - max(count))

Python: N,M=map(int,input().split())
AB=list()
for i in range(M):
  AB.append(list(map(int,input().split())))
C=0
AB.sort(reverse=True)
for i in range(M-1):
  C=C+max(0,N-AB[i][0])
print(C)

Python: l=[]
n,m=map(int,input().split())
for i in range(m):
  a,b=map(int,input().split())
  k=0
  if(a<n):
    k=n-a
  l.append(k)
l.sort()
ans=0
for i in range(0,m-1):
  ans+=l[i]
print(ans)

Python: # AOJ 0631: Point Card
# Python3 2018.7.2 bal4u

n, m = map(int, input().split())
ans, mi = 0, n*2
for i in range(m):
	a, b = map(int, input().split())
	if a < n:
		ans += n-a
		mi = min(mi, a)
print(ans if ans <= 0 else ans-(n-mi));

Python: N,M = map(int,input().split())
numbers = []
money = 0
for i in range(M):
    a,b = map(int,input().split())
    numbers.append(a)
numbers.sort(reverse = True)
for j in range(M-1):
    if numbers[j] < N:
        money += N-numbers[j]
print(money)

Python: n,m=map(int,input().split())
lst = [list(map(int,input().split()))[0] for i in range(m)]
lst.sort()
ans=0
i=1
while lst[i] < n :
    ans+=n-lst[i]
    i+=1
print(ans)

Python: N, M = map(int, input().split())
cards = [list(map(int, input().split())) for _ in range(M)]
hit=ans=0
for n, m in sorted(cards, key=lambda x: x[0])[::-1]:
    if n >= N:
        hit += 1
    else:
        ans += N-n
        hit += 1
    if hit == M-1:
        break
print(ans)

Python: N, M = map(int, input().split())
a = []
for _ in range(M):
	n, m = map(int, input().split())
	a.append(n)
a.sort(reverse=True)
result = 0
for i in range(M-1):
	if a[i]<N: result += N-a[i]

print(result)

Python: N , M = map(int , input().split(' '))
A, B = [], []
hit = 0
for i in range(M):
    tmp = input().split(' ')
    A.append(int(tmp[0]))
    B.append(int(tmp[1]))

hit_card = 0
for i in range(M):
    if A[i] >= N:
        hit_card += 1

sorted_A = sorted(A, reverse=True)
need_money = 0

if hit_card < (M - 1):
    need_hit = (M - 1) - hit_card

    for i in range(need_hit):
        need_money += N - sorted_A[i + need_hit]

print(need_money)

Python: n, m = map(int, input().split())
cost = []
for _ in range(m):
  a, b = map(int, input().split())
  if a >= n:
    cost.append(0)
  else:
    cost.append(n - a)
print(sum(cost) - max(cost))

Python: N, M = map(int, input().split())
A = [int(input().split()[0]) for i in range(M)]

mi = min(A)
del A[A.index(mi)]

ans = 0
for v in A:
	ans += max(0, N - v)
print(ans)

Python: N,M = map(int,input().split())

keihin = M-1

cards = []

for _ in range(M):
    cards.append(list(map(int,input().split())))
    
ans = 0

sorted_cards = sorted(cards, key=lambda x:x[0],reverse=True)

cnt = 0

while cnt < keihin:
    if sorted_cards[cnt][0] >= N:
        cnt+=1
    else:
        ans += (N-sorted_cards[cnt][0])
        cnt+=1
print(ans)

Python: n,m=map(int,input().split())
p=[list(map(int,input().split()))for i in range(m)]
c=[]
for i in p:
	a,b=i
	if a>=n:o=0
	else:o=n-a
	c.append(o)
c.sort()
c.pop()
print(sum(c))

Python: N , M = map(int , input().split(' '))
A, B = [], []
hit = 0
tmp = 0
for i in range(M):
    tmp = input().split(' ')
    A.append(int(tmp[0]))
    B.append(int(tmp[1]))

hit_card = 0
for i in range(M):
    if A[i] >= N:
        hit_card += 1

sorted_A = sorted(A, reverse=True)
need_money = 0

if hit_card < (M - 1):
    need_hit = (M - 1) - hit_card

    for i in range(need_hit):
        need_money += N - sorted_A[i + need_hit]

print(need_money)

Python: N , M = map(int , input().split(' '))
A, B = [], []
hit = 0
for i in range(M):
    tmp = input().split(' ')
    A.append(int(tmp[0]))
    B.append(int(tmp[1]))

hit_card = 0
for i in range(M):
    if A[i] >= N:
        hit_card += 1

sorted_A = sorted(A, reverse=True)
need_money = 0

if hit_card < (M - 1):
    need_hit = (M - 1) - hit_card

    for i in range(need_hit):
        need_money += N - sorted_A[i + hit_card]

print(need_money)

Python: n,m = map(int,input().split())
x = []
money = 0
for i in range(m):
    a,b = map(int,input().split())
    if a < n:
        x += [a]

x.sort()

if len(x) > 1:
    for j in reversed(range(1,len(x))):
        money += n - x[j]

print(money)

Python: N,M=[int(i) for i in input().split(" ")]

cards=[]
for i in range(M):
    A,B=[int(j) for j in input().split(" ")]
    cards.append((A,B))

cards=sorted(cards)
retval=0
for c in cards[1::]:
    if c[0]<N:
        retval+=N-c[0]
print(retval)

Python: n,m = map(int, input().split())
ab = []
for i in range(m):
    ab.append(list(map(int, input().split())))
    
ab = sorted(ab,key = lambda x:x[1])
del ab[-1]
cnt = 0
for asd in ab:
    if (asd[0]<n):
        cnt += (n-asd[0])
print(cnt)

Python: n,m=map(int,input().split())
p=[list(map(int,input().split()))for i in range(m)]
c=[n-a if n>a else 0for a,b in p]
print(sum(c)-max(c))

Python: n,m=map(int,input().split())
s=x=0
for i in range(m):
    a,b=map(int,input().split())
    if n-a>0:s+=n-a
    if n-a>x:x=n-a
print(s-x)

Python: n,m=[int(s)for s in input().split(" ")]
a=[int(input().split(" ")[0])for i in range(m)]
a.sort()
if a[1]>=n:print(0)
else:
    cost=0
    ac=a[1:]
    for i in ac:
        if i<n:cost+=n-i
        else:
            print(cost)
            break

Python: n, m = map(int, input().split())
a = []
for i in range(m):
    a.append(int(input().split()[0]))
a.sort()
count = 0
for i in range(1, m):
    if a[i] < n:
        count += (n-a[i])
print(count)

Python: N, M = map(int, input().split())
A_list = []
for i in range(M):
    A, B = map(int, input().split())
    if B > N:
          A_list.append(A)
list_num = len(A_list)
if list_num <= 1:
    print(0)
else:
    sort_list = sorted(A_list, reverse=True)
    add_list = sort_list[0:(list_num-1)]
    print_num = (list_num - 1) * N - sum(add_list)
    print(print_num)

Python: n, m = map(int, input().split())

point = []
for _ in range(m):
    atari, hazure = map(int, input().split())
    point.append([atari, hazure])

point.sort(reverse=True)

ans = 0
for [atari, _] in point[:m-1]:
    if atari >= n:
        continue
    else:
        ans += n - atari

print(ans)

Python: n,m=map(int,input().split())
p=[]
for i in range(m):
    p.append(list(map(int,input().split())))
p.sort()
p.pop(0)
ans=0
for i in p:
    if i[0]<n:ans+=i[1]-n
    else:break
print(ans)

Python: #B
N,M = map(int,input().split())
AB = [list(map(int,input().split())) for i in range(M)]

AB.sort(reverse=True)
ans = 0
for i in range(M-1):
    ans+=max(0,N-AB[i][0])

print(ans)