Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###


"""
単純な全探索は二重ループでできそうだが、
L,R≤2×109 とかなので、このままだとダメ。
そこで工夫を考えることになる。
例えば L=10, R=3000 とかだったら、
x=2019 とかにすれば、xy は 2019 で割り切れる。
「2019 個以上の整数が連続すると、その中に必ず 2019 の倍数がある」
よってR−L≥2019 のときは答えが 0
それ以外のときは、全探索できる！候補数は多く見積もっても (R−L)^2 程度
"""

import sys
read = sys.stdin.buffer.read # 全読み込み
readline = sys.stdin.buffer.readline # 一行読み込み
readlines = sys.stdin.buffer.readlines # 一行ずつ全読み込み
sys.setrecursionlimit(10**7) # 再帰限界変更（デフォルトは1000）


def main():
    l,r=map(int,readline().rstrip().split())
    
    min_2019=2019

    if l-r>=2019:
        print(0)

    else:
        for i in range(l,r+1):
            for j in range(i+1,r+1):
                min_2019=min(min_2019,(i*j)%2019)
        
        print(min_2019)

if __name__ == '__main__':
    main()

### FIXED CODE ###


l, r = map(int,input().split())
 
ans = 2019
for i in range(l, r):
    for j in range(i+1, r+1):
        tmp = i*j % 2019
        if ans > tmp:
            ans = tmp
        if ans == 0:
            print(ans)
            exit(0)
print(ans)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(4000)

n = int(input())
p = [float(x) for x in input().split()]

dp_list = [[0]*(n+1) for i in range(n)]

def dp(i, j):
  if dp_list[i][j] == 0:
    if i == 0:
      if j == 0:
        dp_list[i][j] = 1-p[i]
      if j == 1:
        dp_list[i][j] = p[i]
    elif j-i == 1:
      dp_list[i][j] = dp(i-1, j-1)*p[i]
    elif j == 0:
       dp_list[i][j] = dp(i-1, j)*(1-p[i])
    else:
      dp_list[i][j] = dp(i-1, j)*(1-p[i]) + dp(i-1, j-1)*p[i]
  return dp_list[i][j]

ans = 0
for i in range(n//2+1, n+1):
  ans += dp(n-1, i)
print(ans)

### FIXED CODE ###

import sys
sys.setrecursionlimit(4000)

n = int(input())
p = [float(x) for x in input().split()]

dp = [[0]*(n+1) for i in range(n)]

dp[0][0] = 1-p[0]
dp[0][1] = p[0]

for i in range(n-1):
  for j in range(i+2):
    dp[i+1][j] += dp[i][j]*(1-p[i+1])
    dp[i+1][j+1] += dp[i][j]*p[i+1]

ans = 0
for i in range(n//2+1, n+1):
  ans += dp[n-1][i]
print(ans)
### BUGGY CODE ###

N = int(input())
ans = set(["3","5","7"])
cnt = 0

for i in range(N+1):
    list = [i for i in str(i)]
    s = set(list)
    sorted(s)
    if s == ans:
        cnt += 1

print(cnt)


### FIXED CODE ###

N = int(input())
ans = set(["3","5","7"])
list = ["3","5","7"]
x_list = ["3","5","7"]
cnt = 0
f = 0
i = 0

while f == 0:
    for j in range(3):
        num = x_list[i] + list[j]
        if int(num) > N:
            f = 1
            break
        x_list.append(num) 
    i += 1

for i in x_list:
    ans_list = [i for i in str(i)]
    s = set(ans_list)
    if s == ans:
        cnt += 1

print(cnt)
### BUGGY CODE ###

# from scipy.special import comb
from scipy.misc import comb
n, k = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
a = sorted(a)
max_a = 0
for i in range(k - 1, n):
    max_a += a[i] * comb(i, k - 1, exact=True)
min_a = 0
for i in range(n - k + 1):
    min_a += a[i] * comb(n - i - 1, k - 1, exact=True)
print(int((max_a - min_a) % 1000000007))

### FIXED CODE ###

n, k = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
a = sorted(a)
fact = [1]
for i in range(1, n + 1):
    fact.append(fact[-1] * i % 1000000007)
max_a = 0
for i in range(k - 1, n):
    max_a += a[i] * fact[i] * pow(fact[i - k + 1], 1000000005, 1000000007) * pow(fact[k - 1], 1000000005, 1000000007) % 1000000007
a = a[::-1]
min_a = 0
for i in range(k - 1, n):
    min_a += a[i] * fact[i] * pow(fact[i - k + 1], 1000000005, 1000000007) * pow(fact[k - 1], 1000000005, 1000000007) % 1000000007
print(int((max_a - min_a) % 1000000007))
### BUGGY CODE ###

import sys
from itertools import permutations
n, c = [int(i) for i in sys.stdin.readline().split()]
D = dict()
C = dict()
MOD_dict = dict()
INF = 10 ** 18
for i in range(c):
    inp = [int(i) for i in sys.stdin.readline().split()]
    D[i] = inp
for i in range(n):
    inp = [int(i) - 1 for i in sys.stdin.readline().split()]
    C[i] = inp
    MOD_dict[i] = []
    for j in range(n):
        MOD_dict[i].append((i + j) % 3)
best = INF
for perm in permutations(range(c), 3):
    cost = 0
    for i in range(n):
        for j in range(n):
            pre = C[i][j]
            post = perm[MOD_dict[i][j]]
            cost += D[pre][post]
    best = min(cost, best)
print(best)

### FIXED CODE ###

import sys
from itertools import permutations
n, c = [int(i) for i in sys.stdin.readline().split()]
D = dict()
C = dict()
MOD_dict = {i : [0 for j in range(30)] for i in range(3)}
INF = 10 ** 18
for i in range(c):
    inp = [int(i) for i in sys.stdin.readline().split()]
    D[i] = inp
for i in range(n):
    inp = [int(i) - 1 for i in sys.stdin.readline().split()]
    C[i] = inp
    for j, k in enumerate(inp):
        MOD_dict[(i+j) % 3][k] += 1
best = INF
for perm in permutations(range(c), 3):
    cost = 0
    for i in range(c):
        cost += MOD_dict[0][i] * D[i][perm[0]] +  MOD_dict[1][i] * D[i][perm[1]] + \
                MOD_dict[2][i] * D[i][perm[2]]
    best = min(cost, best)
print(best)
### BUGGY CODE ###

N, M = map(int, input().split())
P = []
Y = []
#var = []
for i in range(M):
    p, y = map(int, input().split())
    #if p not in P:
    #    var.append(p)
    P.append(p)
    Y.append(y)

L = list(zip(range(M), P, Y))     

Order = [None]*M

for v in set(P):
    l = filter(lambda x:x[1] == v, L)
    l_ = sorted(l, key = lambda x:x[2])
    
    for n in l_:
        Order[n[0]] = l_.index(n)+1
        
for (p, od) in zip(P, Order):
    print('{0:0>6d}{1:0>6d}'.format(p, od))

### FIXED CODE ###

N, M = map(int, input().split())
L = []
P = [0]*(N+1)
for i in range(M):
    p, y = map(int, input().split())
    L.append([i, p, y, 0])

L = sorted(L, key = lambda x:x[2])

for l in L:
    P[l[1]] += 1
    l[3] = P[l[1]]    
        
for l in sorted(L, key = lambda x:x[0]):
    print('{0:0>6d}{1:0>6d}'.format(l[1], l[3]))
### BUGGY CODE ###

N = int(input())
A = [int(input()) for i in range(N)]

M = 998244353

S = sum(A)
dp = [0 for _ in range(S + 1)]
dp[0] = 1
for a in A:
    for i in range(S, a - 1, -1):
        dp[i] = (2 * dp[i] + dp[i - a]) % M
    for i in range(a - 1, -1, -1):
        dp[i] = (2 * dp[i]) % M
dp2 = [0 for _ in range(S + 1)]
dp2[0] = 1
for a in A:
    for i in range(S, a - 1, -1):
        dp2[i] = (dp2[i] + dp2[i - a]) % M
s = 0
for i in range((S + 1) // 2, S + 1):
    s = (s + dp[i]) % M
ans = (3**N - 3 * s) % M
if S % 2 == 0:
    ans = (3**N - 3 * s + 3 * dp2[S // 2]) % M
print(ans)


### FIXED CODE ###

N = int(input())
A = [int(input()) for i in range(N)]

M = 998244353

S = sum(A)
dp = [0 for _ in range(S + 1)]
dp[0] = 1
for a in A:
    for i in range(S, a - 1, -1):
        dp[i] = (2 * dp[i] + dp[i - a]) % M
    for i in range(a - 1, -1, -1):
        dp[i] = (2 * dp[i]) % M
dp2 = [0 for _ in range(S + 1)]
dp2[0] = 1
for a in A:
    for i in range(S, a - 1, -1):
        dp2[i] = (dp2[i] + dp2[i - a]) % M
s = 0
for i in range((S + 1) // 2, S + 1):
    s = (s + dp[i]) % M
ans = (3**N - 3 * s) % M
if S % 2 == 0:
    ans = (3**N - 3 * s + 3 * dp2[S // 2]) % M
print(ans)
### BUGGY CODE ###

N,M=map(int,input().split())
mod=10**9+7
a=[]
for i in range(M):
    a.append(int(input()))


DP=[1]*(N+1)

if 1 in a:
    DP[1]=0

for i in range(2,N+1):
    if i in a:
        DP[i]=0
        if DP[i-1]==0:
            print(0)
            exit()
    else:
        DP[i]=DP[i-1]+DP[i-2]
        DP[i]=DP[i]%mod

print(DP[N])


### FIXED CODE ###

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

chk=[True]*(10**5+1)
for i in range(M):
    a=int(input())
    chk[a]=False

mod=10**9+7

if N==1:
    print(1)
    exit()

f=[0]*(N+1)
f[0]=1
if chk[1]:
    f[1]=1
else:
    f[1]=0


for i in range(2,N+1):
    if chk[i-2]:
        f[i]+=f[i-2]
    if chk[i-1]:
        f[i]+=f[i-1]
    f[i]=f[i]%mod

print(f[N])
### BUGGY CODE ###

import copy
from collections import deque

def main():
    N = int(input())
    A = [deque(map(int, input().split())) for i in range(N)]
    
    ans = 0
    finish = False
    base_used = [True] + [False] * N
    count = 0
    
    while not finish and count != N:
        finish = True
        ans += 1
        used = copy.deepcopy(base_used)
        
        for idx, u in enumerate(used):
            if not u:
                a = A[idx-1][0]
                if not used[a] and A[a-1][0] == idx:
                    for x in [idx, a]:
                        used[x] = True
                        A[x-1].popleft()

                        if not A[x-1]:
                            base_used[x] = True
                            count += 1
                    finish = False     
    
    if N != count:
        ans = -1
    print(ans)
    
if __name__ == '__main__':
    main()

### FIXED CODE ###

import sys

input = sys.stdin.readline

def main():
    N = int(input())
    
    Id = {}
    
    cur = 0
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            Id[(i,j)] = cur
            cur += 1
            
    G = [[] for i in range(N*(N-1)//2)]
    size = [0] * (N*(N-1)//2)
    
    for i in range(1, N+1):
        for idx, j in enumerate(list(map(int, input().split()))):
            if i < j: k = Id[(i, j)]
            else: k = Id[(j, i)]
            if idx: 
                G[par].append(k)
                size[k] += 1
            par = k
            
    g = [idx for idx, s in enumerate(size) if s == 0]
    
    ans = 0
    while g:
        ans += 1
        n_g = []
        
        for v in g:
            length = len(G[v])
            if not length: continue
            
            size[G[v][0]] -= 1
            if not size[G[v][0]]: n_g.append(G[v][0])
    
            if length == 2:
                size[G[v][1]] -= 1
                if not size[G[v][1]]: n_g.append(G[v][1])
        g = n_g
        
    for s in size:
        if s != 0:
            print(-1)
            sys.exit()
    print(ans)
            
if __name__ == '__main__':
    main()
### BUGGY CODE ###

def main():
    import sys
    input = sys.stdin.buffer.readline
    H, W, D = (int(i) for i in input().split())
    A = [[int(i) for i in input().split()] for j in range(H)]

    dic = {i: (-1, -1) for i in range(1, H*W + 1)}
    for h in range(H):
        for w in range(W):
            dic[A[h][w]] = (h+1, w+1)

    Q = int(input())
    for query in range(Q):
        le, ri = (int(i) for i in input().split())
        cur = 0
        while le != ri:
            i, j = dic[le]
            x, y = dic[le + D]
            cur += abs(x - i) + abs(y - j)
            le = le + D
        print(cur)


if __name__ == '__main__':
    main()


### FIXED CODE ###

def main():
    H, W, D = (int(i) for i in input().split())
    A = [[int(i) for i in input().split()] for j in range(H)]
    num_to_xy = {}
    d_sum = [[] for _ in range(D)]
    for h in range(H):
        for w in range(W):
            num_to_xy[A[h][w]] = (h+1, w+1)
            d_sum[A[h][w] % D].append((A[h][w]))
    for d in range(D):
        d_sum[d].sort()
        cur = []
        y, x = -1, -1
        j, i = -1, -1
        for i, a in enumerate(d_sum[d]):
            if i == 0:
                y, x = num_to_xy[a]
                continue
            j, i = num_to_xy[a]
            cur.append(abs(x - i) + abs(y - j))
            y, x = j, i
        s = [0] * (len(cur) + 1)
        for i in range(0, len(cur)):
            s[i+1] = s[i] + cur[i]
        d_sum[d] = s
    Q = int(input())
    for q in range(Q):
        le, ri = (int(i) for i in input().split())
        if le % D != 0:
            print(d_sum[le % D][(ri - le % D)//D] -
                  d_sum[le % D][(le - le % D)//D])
        else:
            print(d_sum[le % D][(ri - le % D)//D-1] -
                  d_sum[le % D][(le - le % D)//D-1])


if __name__ == '__main__':
    main()
### BUGGY CODE ###

a, b, c, k=input().split()
a=int(a)
b=int(b)
c=int(c)
k=int(k)
ans=0

if a>0 and k>0 and a<=k:
  k=k-a
  ans=ans+a
elif a>0 and k>0 and a>k:
  ans=ans+k
  k=0

while b>0 and k>0:
  b+=-1
  k+=-1

while c>0 and k>0:
  ans+=-1
  c+=-1
  k+=-1
  
print(ans)

### FIXED CODE ###

a, b, c, k=input().split()
a=int(a)
b=int(b)
c=int(c)
k=int(k)
ans=0

if a>0 and k>0 and a<=k:
  k=k-a
  ans=ans+a
elif a>0 and k>0 and a>k:
  ans=ans+k
  k=0

if b>0 and k>0 and b<=k:
  k=k-b
elif b>0 and k>0 and b>k:
  k=0
  
if c>0 and k>0 and c<=k:
  k=k-c
  ans=ans-c
elif c>0 and c>0 and c>k:
  ans=ans-k
  k=0

  
print(ans)
### BUGGY CODE ###

X, K, D = map(int, input().split())
for i in range(K):
    if abs(X) >= D:
        X = abs(X)-D
    elif abs(X) < D and (K-i)%2==1:
        X = abs(X)-D
        break
    elif abs(X) < D and (K-i)%2==0:
        X = abs(X)
        break
print(abs(X))

### FIXED CODE ###

X, K, D = map(int, input().split())
X = abs(X)
dis = X
if X >=D:
    dis = X - D*(min(abs(X)//D,K))
    K = K - min(abs(X)//D,K)
    if K%2==0:
        print(dis)
    else:
        print(abs(dis-D))
else:
    if K%2==0:
        print(dis)
    else:
        print(abs(dis-D))
### BUGGY CODE ###

# -*- coding: utf-8 -*-
from collections import deque


class LinkedList:
    def __init__(self, n):
        self.n = n
        self.ll = [[] for _ in range(self.n)]

    def link(self, u, v, direction=False):
        self.ll[u].append(v)
        if not direction:
            self.ll[v].append(u)

    def check(self, start, goal):
        states = [0] * self.n
        queue = deque([start])

        while queue:
            u = queue.popleft()
            # if states[u]:
            #     continue
            # else:
            states[u] = 1

            for v in self.ll[u]:
                if v == goal:
                    print('yes')
                    return

                if states[v]:
                    continue
                else:
                    queue.append(v)

        else:
            print('no')
            return


if __name__ == '__main__':
    n, m = map(int, input().split())
    ll = LinkedList(n)

    for _ in range(m):
        u, v = map(int, input().split())
        ll.link(u, v)

    q = int(input())
    for _ in range(q):
        u, v = map(int, input().split())
        ll.check(u, v)



### FIXED CODE ###

# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**6)


class LinkedList:
    def __init__(self, n):
        self.n = n
        self.ll = [[] for _ in range(self.n)]

    def link(self, u, v, direction=False):
        self.ll[u].append(v)
        if not direction:
            self.ll[v].append(u)

    def dfs(self):
        self.cc = [None] * self.n

        color = 1
        for u in range(self.n):
            if not self.cc[u]:
                self.dfs_visit(u, color)
                color += 1

    def dfs_visit(self, u, color):
        self.cc[u] = color
        for v in self.ll[u]:
            if not self.cc[v]:
                self.dfs_visit(v, color)

    def check(self, u, v):
        if self.cc[u] == self.cc[v]:
            print('yes')
        else:
            print('no')


if __name__ == '__main__':
    n, m = map(int, input().split())
    ll = LinkedList(n)

    for _ in range(m):
        u, v = map(int, input().split())
        ll.link(u, v)
    ll.dfs()

    q = int(input())
    for _ in range(q):
        u, v = map(int, input().split())
        ll.check(u, v)
### BUGGY CODE ###

n,q = map(int,input().split())
peer = [[] for _ in range(n)]
for _ in range(n-1):
  a,b = map(int,input().split())
  a -= 1
  b -= 1
  peer[a].append(b)
  peer[b].append(a)
child = [[] for _ in range(n)]
now = [0]
while now != []:
  last = now
  l = len(last)
  now = []
  for i in range(l):
    child[last[i]] = peer[last[i]]
    now += peer[last[i]]
    p = peer[last[i]]
    ll = len(p)
    for ii in range(ll):
      peer[p[ii]].remove(last[i])
mom = ['?' for _ in range(n)]
for i in range(n):
  for x in child[i]:
    mom[x] = i
cnt = [0 for _ in range(n)]
for _ in range(q):
  p,x = map(int,input().split())
  p -= 1
  cnt[p] += x
#print(peer)
#print(child)
#print(mom)
#print(cnt)
ans = cnt
now = [0]
while now != []:
  last = now
  l = len(last)
  now = []
  for i in range(l):
    now += child[last[i]]
    try:
      ans[last[i]] += ans[mom[last[i]]]
    except:
      pass
print(' '.join(list(map(str,ans))))

### FIXED CODE ###

#親ー子の順に書かれてるか否かめっちゃ重要なのになあ
n,q = map(int,input().split())

peer = [[] for _ in range(n)]
for i in range(n-1):
  a,b = (map(int,input().split()))
  a -= 1
  b -= 1
  peer[a].append(b)
  peer[b].append(a)

pq = [0 for _ in range(n)]
for i in range(q):
  p,x = map(int,input().split())
  p -= 1
  pq[p] += x

now = [0]
ans = pq
now = [0]
mom = [0 for _ in range(n)]
mom[0] = 1
while now != []:
  last = now
  now = []
  for x in last:
    for y in peer[x]:
      if mom[y] == 0:
        now.append(y)
        mom[y] = 1
        ans[y] += ans[x]

print(' '.join(list(map(str,ans))))
### BUGGY CODE ###

import math
A, B = map(int, input().split())

def gcd(a, b):
	if b == 0:
		return(a)
	else :
		return gcd(b, a % b)
		
a = gcd(A, B)
ctr = 1
gusu = 0
lst = [2]

for i in range(3, a + 1):
	sosu = 1
	for j in range(2, int(math.sqrt(i) + 1)):
		if i % j == 0:
			sosu = 0
			break
	if sosu == 1:
		lst.append(i)

for i in lst:
	if not (gusu == 1 and i % 2 == 0):
		do = 0
		while a % i == 0:
			a /= i
			do = 1
			if a == 1:
				break
		if do:
			ctr += 1
		if do and i == 2:
			gusu = 1
		if a == 1:
			break
		
print(ctr)

### FIXED CODE ###

import math
A, B = map(int, input().split())

def gcd(a, b):
	if b == 0: return(a)
	else : return gcd(b, a % b)
		
G = gcd(A, B)
ctr = 1
for i in range(2, int(math.sqrt(G)) + 1):
    if G == 1: 
        break
    if G % i == 0: 
        ctr += 1
    while G % i == 0:
        G /= i
        
if G == 1:        
    print(ctr)
else:
    print(ctr + 1)
### BUGGY CODE ###

r,g,b,n=map(int,input().split())
ans=0
box=[r,g,b]
box.sort(key=lambda x:-x)
# print(box)
maxCnt=[n//box[i]+1 for i in range(3)]
# print(maxCnt)
cnt=[0,0,0]
for cnt0 in range(maxCnt[0]):
    for cnt1 in range(maxCnt[1]):
        reg=n-cnt0*box[0]-cnt1*box[1]
        if reg<0:
            break
        if reg%box[2]==0:
            ans+=1
print(ans)


### FIXED CODE ###

r,g,b,n=map(int,input().split())
boxSize=[r,g,b]
boxSize.sort(key=lambda x:-x)
# print(boxSize)
maxCnt=[n//boxSize[i]+1 for i in range(3)]
# print(maxCnt)
ans=0
if boxSize[1]==1:
    for cnt0 in range(maxCnt[0]):
        ans+=n-cnt0*boxSize[0]+1
else:
    for cnt0 in range(maxCnt[0]):
        for cnt1 in range(maxCnt[1]):
            reg=n-cnt0*boxSize[0]-cnt1*boxSize[1]
            if reg<0:
                break
            if reg%boxSize[2]==0:
                ans+=1
print(ans)
### BUGGY CODE ###

A,B,M = map(int,input().split())
A_li = list(map(int,input().split()))
B_li = list(map(int,input().split()))
X = set()
Y = set()
inf = float("inf")
posibility = inf

for m in range(M):
    x,y,c=map(int,input().split())
    tmp_posibility=A_li[x-1]+B_li[y-1]-c
    posibility=min(posibility,tmp_posibility)
    X.add(x)
    Y.add(y)

import itertools 
a_li = set(A_li)-X
b_li = set(B_li)-Y

for a,b in itertools.product(a_li,b_li):
    tmp_posibility = a+b
    posibility = min(posibility,tmp_posibility)



print(posibility)

### FIXED CODE ###

A,B,M = map(int,input().split())
A_li = list(map(int,input().split()))
B_li = list(map(int,input().split()))

inf = float("inf")
posibility = inf

for m in range(M):
    x,y,c=map(int,input().split())
    tmp_posibility=A_li[x-1]+B_li[y-1]-c
    posibility=min(posibility,tmp_posibility)
 

no_c = min(A_li)+min(B_li)
decition = min(posibility,no_c)

print(decition)
### BUGGY CODE ###

# ABC061_C
N, K = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]

lst = []

for i in range(N):
    lst += [AB[i][0]]*AB[i][1]

lst = sorted(lst)
print(lst[K-1])


### FIXED CODE ###

# ABC061_C
N, K = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
AB = sorted(AB)

for i in range(N):
    K -= AB[i][1]
    if K <= 0:
        print(AB[i][0])
        break
### BUGGY CODE ###

n = int(input()) 
l = list(map(int, input().split()))
[print(l.count(i)) for i in range(1, n+1)]
    

### FIXED CODE ###

#!/usr/bin/env python3
import collections
n = int(input()) 
l = sorted(list(map(int, input().split())))
c = collections.Counter(l)
[print(c[i]) for i in range(1, n+1)]
### BUGGY CODE ###

A, B, C, K = map(int, input().split())
point = 0
judge = False
if A == K:
  point = A
  judge = True
elif A == 0 and B == K:
  point = 0
  judge = True
elif A == 0 and B == 0 and C == K:
  point = -K
  judge = True
if not judge:
  if A > 0 and K > 0:
    while A > 0 and K > 0:
      K -= 1
      A -= 1
      point += 1
  if B > 0 and K > 0:
    while B > 0 and K > 0:
      K -= 1
      B -= 1
      point += 0
  if C > 0 and K > 0:
    while C > 0 and K > 0:
      K -= 1
      C -= 1
      point -= 1
print(point)

  

### FIXED CODE ###

a,b,c,k=map(int,input().split());count=0
if k<=a: print(k)
elif k<=a+b: print(a)
else: print(a-(k-a-b))
### BUGGY CODE ###

from collections import deque
N, M = map(int, input().split())
A = deque(sorted([int(x) for x in input().split()]))
X = []
for _ in range(M):
    b, c = map(int, input().split())
    X.extend([c] * b)
X.sort()

for i in range(len(X) if len(X) < len(A) else len(A)):
    a = A.popleft()
    c = X.pop()
    if a < c:
        A.append(c)
    else:
        A.append(a)
        print(sum(A))
        quit()
print(sum(A))

### FIXED CODE ###

from sys import stdin
from collections import deque
from operator import itemgetter

N, M = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) for x in stdin.readline().rstrip().split()]
A.sort()
A = deque(A)
X = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(M)]
X.sort(key=itemgetter(1), reverse=True)
D = []
for b, c in X:
    if len(D) < N:
        D.extend([c] * b)
    else:
        break
D = deque(D)

for i in range(len(D) if len(D) < len(A) else len(A)):
    a = A.popleft()
    c = D.popleft()
    if a < c:
        A.append(c)
    else:
        A.append(a)
        print(sum(A))
        quit()
print(sum(A))
### BUGGY CODE ###

import sys
sys.setrecursionlimit(1000000)
n,q = map(int, input().split())
a_b = [list(map(int, input().split())) for _ in range(n-1)]
p_x = [list(map(int, input().split())) for _ in range(q)]

v = [[] for i in range(n)]
for e in a_b:
    v[e[0]-1].append(e[1]-1)
    v[e[1]-1].append(e[0]-1)
score = [0 for i in range(n)]
for e in p_x:
    score[e[0]-1]+=e[1]

def dfs(p):
    for c in v[p]:
        if not c in visited:
            visited.append(c)
            score[c]+=score[p]
            if len(visited)==n:
                print(*score)
            else:
                dfs(c)
visited = [0]                
dfs(0)

### FIXED CODE ###

import sys
from collections import deque
sys.setrecursionlimit(1000000)

n,q = map(int, input().split())
a_b = [list(map(int, sys.stdin.readline().split())) for _ in range(n-1)]
p_x = [list(map(int, sys.stdin.readline().split())) for _ in range(q)]

v = [deque([]) for i in range(n)]
for e in a_b:
    v[e[0]-1]+=[e[1]-1]
    v[e[1]-1]+=[e[0]-1]
score = [0 for i in range(n)]
for e in p_x:
    score[e[0]-1]+=e[1]
def dfs(p, bp=-1):
    while v[p]:
        c =v[p].popleft()
        if c ==bp:
            continue
        #print(p, c, bp)
        score[c]+=score[p]
        dfs(c, bp=p)
visited = [0]                
dfs(0)
print(*score)