CodeEff/ECCO huggingface dataset:
# Original solution
N,M=list(map(int,input().split()))
A=list(map(int,input().split()))
tmp,count,l0=A[0],1,[]
for i in range(1,N):
  if tmp==A[i]:
    count+=1
  else:
    l0.append([tmp,count])
    tmp=A[i]
    count=1
l0.append([tmp,count])
BC=[]
for i in range(M):
  BC.append(list(map(int,input().split())))
BC.sort(key=lambda x:x[1],reverse=True)
cnt=0
l1=[]
for b,c in BC:
  if cnt>N:
    break
  else:
    l1.append([c,b])
    cnt+=b
l=l0+l1
l.sort(key=lambda x:x[0],reverse=True)
d=0
ans=0
while d<N:
  if l[0][1]==0:
    l.remove(l[0])
  ans+=l[0][0]
  d+=1
  l[0][1]-=1
print(ans)

# Optimized solution
N,M=list(map(int,input().split()))
A=list(map(int,input().split()))
BC=[]
for i in range(M):
  BC.append(list(map(int,input().split())))
A.sort(reverse=True)
BC.sort(key=lambda x:x[1],reverse=True)
ans=sum(A)
for i in range(M):
  for _ in range(BC[i][0]):
    if A==[]:
      print(ans)
      exit()
    if A[-1]>BC[i][1]:
      print(ans)
      exit()
    else:
      p=A.pop()
      ans-=p
      ans+=BC[i][1]
      
print(ans)
# Original solution
def main():
    import sys
    input = sys.stdin.readline
    from bisect import bisect_left,bisect
    
    N, M = list(map(int, input().split()))
    A = list(map(int, input().split()))
    S = [list(map(int, input().split())) for i in range(M)]
    A.sort()
    A = A[::-1]
    S = sorted(S, key=lambda x: x[1])[::-1]
    
    T = []
    count = 0
    
    for i in range(M):
        T += [S[i][1]]*S[i][0]
        count += S[i][0]
    
    a = 0
    t = 0
    SUM = 0
    for _ in range(N):
        if t >= count:
            SUM += A[a]
            a += 1
        elif A[a] > T[t]:
            SUM += A[a]
            a += 1
        else:
            SUM += T[t]
            t += 1
    print(SUM)
    
    

main()

# Optimized solution
def main():
    import sys
    input = sys.stdin.readline
    from bisect import bisect_left,bisect
    
    N, M = list(map(int, input().split()))
    A = list(map(int, input().split()))
    S = [list(map(int, input().split())) for i in range(M)]
    A.sort()
    A = A[::-1]
    S = sorted(S, key=lambda x: x[1])[::-1]
    
    T = []
    count = 0
    
    for i in range(M):
        T += [S[i][1]]*S[i][0]
        count += S[i][0]
        if count >= N:
            break
    
    a = 0
    t = 0
    SUM = 0
    for _ in range(N):
        if t >= count:
            SUM += A[a]
            a += 1
        elif A[a] > T[t]:
            SUM += A[a]
            a += 1
        else:
            SUM += T[t]
            t += 1
    print(SUM)
    
    

main()
# Original solution
import bisect

N, M = list(map(int, input().split()))
A = sorted(list(map(int, input().split())))
ans = sum(A)

for _ in range(M):
    b, c = list(map(int, input().split()))
    if c <= A[0]:
        continue
    index = bisect.bisect_left(A, c)
    index = min(index, b)
    for i in range(index):
        ans += c - A[i]
        A[i] = c
    if index > 0:
        A = sorted(A)

print(ans)



# Optimized solution
N, M = list(map(int, input().split()))
A = sorted(list(map(int, input().split())))

bc = [list(map(int, input().split())) for _ in range(M)]

ans = sum(A)

bc = sorted(bc, key=lambda i: i[1], reverse=True)

i = 0
for b, c in bc:
    for _ in range(b):
        if A[i] > c:
            break
        ans += c - A[i]
        i += 1
        if i >= N:
            break
    else:
        continue
    break

print(ans)
# Original solution
import sys
input = sys.stdin.readline


def main():
    n, m = list(map(int, input().split()))
    a = list(map(int, input().split()))
    count = 0
    add = []
    mini = 0

    cb = [None]*m
    for i in range(m):
        b, c = list(map(int, input().split()))
        cb[i] = (c, b)

    cb.sort(reverse=True)

    for c, b in cb:
        count += b
        add += [c]*b
        if count >= n:
            mini = c
            break

    add = add[:n]  # 追加のカードを削るかどうか
    a.sort()  # 昇順
    idx = len(a)//2
    for _ in range(10):
        if a[idx] <= mini:  # aの中央がminiより小さい
            a = a[idx:]
            idx = len(a)//2
        else:
            idx //= 2

    new_a = a + add
    new_a.sort(reverse=True)

    print((sum(new_a[:n])))


if __name__ == '__main__':
    main()


# Optimized solution
import sys
input = sys.stdin.readline


def main():
    n, m = list(map(int, input().split()))
    a = list(map(int, input().split()))

    cb = [None]*m
    for i in range(m):
        b, c = list(map(int, input().split()))
        cb[i] = (c, b)

    cb.sort(reverse=True)
    add = []
    count = 0

    for c, b in cb:
        add += [c]*b
        count += b
        if count >= n:
            break

    a += add
    a.sort(reverse=True)
    print((sum(a[:n])))


if __name__ == '__main__':
    main()
# Original solution
import heapq
N, M = list(map(int, input().split()))
A = list([int(x) for x in input().split()])
heapq.heapify(A)

for i in range(M):
  b, c = list(map(int, input().split()))
  for j in range(b):
    d = heapq.heappushpop(A, c)
    if c == d:
      break
      
print((sum(A)))

# Optimized solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
exc = []

for i in range(M):
  exc.append(list(map(int, input().split())))
exc.sort(key=lambda x: x[1], reverse=True)
e = []
count = 0
for i in range(M):
  e += [exc[i][1]] * exc[i][0]
  count += exc[i][0]
  if count >= N:
    break
cards = sorted(A+e, reverse=True)
print((sum(cards[:N])))
# Original solution
from collections import Counter

n, m = list(map(int, input().split()))
a = Counter(list(map(int, input().split())))
bc = [list(map(int, input().split())) for i in range(m)]
b = {}
ans = 0

for i, j in bc:
    if j in b:
        ref = b[j]
        b[j] = ref + i
    else:
        b[j] = i

d = Counter(a) + Counter(b)
keys = sorted(d)[::-1]

for key in keys:
    qty = d[key]
    if qty < n:
        ans += qty*key
        n -= qty
    else:
        ans += n*key
        break

print(ans)

# Optimized solution
n, m = list(map(int, input().split()))
l = [(a, 1) for a in list(map(int, input().split()))]
for i in range(m):
    b, c = list(map(int, input().split()))
    l.append((c, b))
l = sorted(l)[::-1]
ans = 0

for i, j in l:
    if j < n:
        ans += i*j
        n -= j
    else:
        ans += n*i
        break

print(ans)
# Original solution
from heapq import heappushpop, heapify

n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
heapify(a)
gomi=a[0]

bc=[]
for _ in range(m):
    b,c=list(map(int,input().split()))
    bc.append((c,b))
bc.sort(reverse=True)

for i in bc:
    c,b=i
    if gomi>c:
        break
    for i in range(b):
        heappushpop(a, c)
    gomi=a[0]
print((sum(a)))

# Optimized solution
import heapq
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
heapq.heapify(a)
CB=[]
for _ in range(m):
    b, c = list(map(int, input().split()))
    CB.append((c, b))
CB.sort(reverse=True)

for c, b in CB:
    if a[0] >= c:
        break
    for _ in range(b):
        if a[0] >= c:
            break
        heapq.heappushpop(a, c)

print((sum(a)))
# Original solution
N, M = list(map(int, input().split()))
A = sorted(list(map(int, input().split())))
L = [tuple(map(int, input().split())) for _ in range(M)]

L.sort(key=lambda x: x[1], reverse=True)

flag = True
num = 0
while flag:
    if num >= M:
        break

    for i in range(L[num][0]):
        if L[num][1] > A[0]:
            A[0] = L[num][1]
            A.sort()
        else:
            flag = False
            break
    num += 1

print((sum(A)))



# Optimized solution
N, M = list(map(int, input().split()))
A = sorted(list(map(int, input().split())))
L = [tuple(map(int, input().split())) for _ in range(M)]

L.sort(key=lambda x: x[1], reverse=True)

flag = True
num = 0
now = 0
while flag:
    if num >= M:
        break
    for i in range(L[num][0]):
        if now < N and L[num][1] > A[now]:
            A[now] = L[num][1]
            now += 1
        else:
            flag = False
            break
    num += 1
print((sum(A)))
# Original solution
N,M=list(map(int,input().split()))
A=list(map(int,input().split()))
B=[]
for m in range(M):
    b,c=list(map(int,input().split()))
    B+=[c]*b
A=sorted(A)
B=sorted(B)[::-1]
Max=0
for i in range(N+1):
    New=sum(A[i:])+sum(B[:i])
    if New>Max:
        Max=New
print(Max)

# Optimized solution
def main(A,B,N):
    A=sorted(A)
    B=sorted(B)[::-1]
    K=len(B)
    R=min(N,K)
    for i in range(R):
        if A[i]>B[i]:
            return sum(B[:i]+A[i:])
    return sum(B[:R]+A[R:])

N,M=list(map(int,input().split()))
A=list(map(int,input().split()))
B=[]
for m in range(M):
    b,c=list(map(int,input().split()))
    B+=[c]*b

print((main(A,B,N)))
# Original solution
def main(A,D,N):
    K=min(N,len(D))
    for i in range(K):
        if A[i]>D[i]:
            return sum(D[:i]+A[i:])
    return sum(D[:K]+A[K:])

N,M=list(map(int,input().split()))
A=list(map(int,input().split()))
A.sort()
CB=[]
for m in range(M):
    b,c=list(map(int,input().split()))
    CB.append([c,b])
CB.sort()
CB.reverse()
D=[]
while len(D)<N and CB!=[]:
    D.append(CB[0][0])
    CB[0][1]-=1
    if CB[0][1]==0:
        CB.remove(CB[0])
for cb in CB:
    D+=[cb[0]]*cb[1]
print((main(A,D,N)))

# Optimized solution
N,M=list(map(int,input().split()))
A=list(map(int,input().split()))

BC=[]
for m in range(M):
    b,c=list(map(int,input().split()))
    BC.append([b,c])

A.sort()
BC.sort(key=lambda x:x[1],reverse=True)

D=[]
for [b,c] in BC:
    D+=[c]*b
    if len(D)>N:
        break

if len(D)<N:
    D+=[0]*(N-len(D))

AD=[max(A[i],D[i]) for i in range(N)]
 
print((sum(AD)))
# Original solution
import sys
input=sys.stdin.readline
import heapq
n,q=list(map(int,input().split()))
l=[list(map(int,input().split())) for i in range(n)]
L1=[]
for i in range(n):
  L1.append([l[i][0]-l[i][2],1,l[i][2]])
  L1.append([l[i][1]-l[i][2],-1,l[i][2]])
for i in range(q):
  L1.append([int(eval(input())),2,0])
L1.sort()
L2=[]
heapq.heapify(L2)
L3=[]
L3=set(L3)
ans=[]
for i in range(len(L1)):
  if L1[i][1]==1:
    heapq.heappush(L2,L1[i][2])
    L3.add(L1[i][2])
  elif L1[i][1]==-1:
    L3.remove(L1[i][2])
  else:
    while L2 and L2[0] not in L3:
      heapq.heappop(L2)
    if L2:
      print((L2[0]))
    else:
      print((-1))

# Optimized solution
import sys
input=sys.stdin.readline
import heapq
n,q=list(map(int,input().split()))
l=[list(map(int,input().split())) for i in range(n)]
L1=[]
for i in range(n):
  L1.append((l[i][0]-l[i][2],1,l[i][2]))
  L1.append((l[i][1]-l[i][2],-1,l[i][2]))
for i in range(q):
  L1.append((int(eval(input())),2,0))
L1.sort()
L2=[]
heapq.heapify(L2)
L3=[]
L3=set(L3)
for i in range(len(L1)):
  if L1[i][1]==1:
    heapq.heappush(L2,L1[i][2])
    L3.add(L1[i][2])
  elif L1[i][1]==-1:
    L3.remove(L1[i][2])
  else:
    while L2 and L2[0] not in L3:
      heapq.heappop(L2)
    if L2:
      print((L2[0]))
    else:
      print((-1))
# Original solution
import sys
from heapq import *
input = sys.stdin.readline

def main():
    n, q = list(map(int, input().split()))
    
    key = []
    for i in range(n):
        s, t, x = list(map(int, input().split()))
        l, r = max(0, s-x), max(0, t-x)
        key.append((l, 1, x))
        key.append((r, -1, x))

    key.sort()
    judge = []
    now = set()
    already = dict()
    
    index = 0
    ans = -1
    for i in range(q):
        d = int(eval(input()))
        for i in range(index, 2*n):
            s, j, x = key[i]
            if s > d:
                index = i
                break
            if j == 1:
                heappush(judge, x)
                now = now.union({x})
            else:
                now.discard(x)
                if x in already:
                    already[x] += 1
                else:
                    already[x] = 1
                    
                    
        while judge:
            if judge[0] in already and already[judge[0]] > 0:
                already[judge[0]] -= 1
                heappop(judge)
            else:
                break
        if judge:
            ans = judge[0]
        else:
            ans = -1      
            
        print(ans)

    
if __name__ == "__main__":
    main()

# Optimized solution
import sys
from heapq import *
input = sys.stdin.readline

def main():
    n, q = list(map(int, input().split()))
    
    key = []
    for i in range(n):
        s, t, x = list(map(int, input().split()))
        l, r = max(0, s-x), max(0, t-x)
        key.append((l, 1, x))
        key.append((r, -1, x))
    
    for i in range(q):
        d = int(eval(input()))
        key.append((d, 2))

    key.sort()
    judge = []
    already = dict()
    
    for i in range(2*n+q):
        k = key[i]
        if k[1] == -1:
            if k[2] in already:
                already[k[2]] += 1
            else:
                already[k[2]] = 1
        elif k[1] == 1:
            heappush(judge, k[2])
        else:
            while judge:
                if judge[0] in already and already[judge[0]] > 0:
                    already[judge[0]] -= 1
                    heappop(judge)
                else:
                    break
            if judge:
                print((judge[0]))
            else:
                print((-1))
    

    
if __name__ == "__main__":
    main()
# Original solution
from heapq import heapify, heappush, heappop 

INF = 1001001001

N, Q = list(map(int, input().split()))
STXs = [tuple(map(int, input().split())) for _ in range(N)]
Ds = [int(eval(input())) for _ in range(Q)]

#S-X, T-X, X
future_block = [(S - X, T - X, X) for S, T, X in STXs]
future_block.append((INF, INF, INF)) #sentinel
heapify(future_block)

#T-X, S-X, X
current_block = [(INF, INF, INF)] #sentinel
current_min_pair = (INF, INF, INF)

for D in Ds:
    while future_block[0][0] <= D:
        s_x, t_x, x = heappop(future_block)
        if current_min_pair[2] > x:
            current_min_pair = (t_x, s_x, x)
        heappush(current_block, (t_x, s_x, x))
    
    while current_block[0][0] <= D:
        if current_min_pair == heappop(current_block):
            current_min_pair = min(current_block, key = lambda t: t[2])

    print((-1 if current_min_pair[2] == INF else current_min_pair[2]))


# Optimized solution
from heapq import heapify, heappush, heappop 

INF = 1001001001

N, Q = list(map(int, input().split()))
STXs = [tuple(map(int, input().split())) for _ in range(N)]
Ds = [int(eval(input())) for _ in range(Q)]

#S-X, T-X, X
future_block = [(S - X, T - X, X) for S, T, X in STXs]
future_block.append((INF, INF, INF)) #sentinel
heapify(future_block)

#T-X, S-X, X
current_block = [(INF, INF, INF)] #sentinel
current_min_pair = (INF, INF, INF)

for D in Ds:
    while future_block[0][0] <= D:
        s_x, t_x, x = heappop(future_block)
        if current_min_pair[2] > x:
            current_min_pair = (t_x, s_x, x)
        heappush(current_block, (t_x, s_x, x))
    
    search_min_flg = False
    while current_block[0][0] <= D:
        if current_min_pair == heappop(current_block):
            search_min_flg = True
    if search_min_flg:
        current_min_pair = min(current_block, key = lambda t: t[2])

    print((-1 if current_min_pair[2] == INF else current_min_pair[2]))
# Original solution
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
from collections import Counter
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
from collections import Counter
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
N, Q = list(map(int, input().split()))
events = []
for i in range(N):
    s, t, x = list(map(int, input().split()))
    events.append((s - x, 0, x))
    events.append((t - x, 1, x))

for i in range(Q):
    d = int(eval(input()))
    events.append((d, 2, i))

events = sorted(events)

q = [INF]
cnt = defaultdict(int)
ans = [INF] * Q
for a, b, c in events:
    if b == 0:
        heapq.heappush(q, c)
        cnt[c] += 1
    if b == 1:
        cnt[c] -= 1
    if b == 2:
        while len(q) > 1 and cnt[q[0]] == 0:
            heapq.heappop(q)
        if q[0] == INF:
            ans[c] = -1
        else:
            ans[c] = q[0]
for i in range(Q):
    print((ans[i]))

# Optimized solution
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
from collections import Counter
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
from collections import Counter
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
N, Q = list(map(int, input().split()))
events = []
for i in range(N):
    s, t, x = list(map(int, input().split()))
    events.append((s - x, 0, x))
    events.append((t - x, 1, x))

for i in range(Q):
    d = int(eval(input()))
    events.append((d, 2, i))

events = sorted(events)

q = [INF]
cnt = defaultdict(int)
cnt[INF] = 1
ans = [INF] * Q
for a, b, c in events:
    if b == 0:
        heapq.heappush(q, c)
        cnt[c] += 1
    if b == 1:
        cnt[c] -= 1
    if b == 2:
        while cnt[q[0]] == 0:
            heapq.heappop(q)
        if q[0] == INF:
            ans[c] = -1
        else:
            ans[c] = q[0]
for i in range(Q):
    print((ans[i]))
# Original solution
import heapq
N, Q = (int(i) for i in input().split())
L = [0] * (2 * N + Q)
for i in range(N):
    S, T, X = (int(i) for i in input().split())
    L[2 * i] = (S-X, X, 1)
    L[2 * i + 1] = (T-X, X, -1)
for i in range(Q):
    D = int(eval(input()))
    L[2 * N + i] = (D, 10**9+1, 0)
L.sort()

Xs = set([])
XH = []
heapq.heapify(XH)
for t, X, f in L:
    if f == 1:
        Xs.add(X)
        heapq.heappush(XH, X)
    elif  f == -1:
        Xs.remove(X)
    else:
        if Xs:
            while XH[0] not in Xs:
                X = heapq.heappop(XH)
            print((XH[0]))
        else:
            print("-1")

# Optimized solution
import sys
input = sys.stdin.readline

import heapq
N, Q = (int(i) for i in input().split())
L = [0] * (2 * N + Q)
for i in range(N):
    S, T, X = (int(i) for i in input().split())
    L[2 * i] = (S-X, 1, X)
    L[2 * i + 1] = (T-X, -1, X)
for i in range(Q):
    D = int(eval(input()))
    L[2 * N + i] = (D, 2, 0)
L.sort()

Xs = set([])
XH = []
heapq.heapify(XH)
for t, f, X in L:
    if f == 1:
        Xs.add(X)
        heapq.heappush(XH, X)
    elif  f == -1:
        Xs.remove(X)
    else:
        if Xs:
            while XH[0] not in Xs:
                X = heapq.heappop(XH)
            print((XH[0]))
        else:
            print("-1")
# Original solution
def main():

    import sys
    import heapq

    N, Q = list(map(int, sys.stdin.readline().split()))
    events = []

    for i in range(N):
        s, t, x = list(map(int, sys.stdin.readline().split()))
        events.append((s - x, 1, x))  # 通行止め追加
        events.append((t - x, -1, x))  # 通行止め削除

    events.sort()
    events.append([float('inf'), 'guard', None])

    i = 0
    closing = set()
    hq = []  # 最小値を高速に入手するため
    for q in range(Q):
        d = int(eval(input()))
        while d >= events[i][0]:
            time, event, x = events[i]
            if event == 1:
                closing.add(x)
                heapq.heappush(hq, x)

            elif event == -1:
                closing.remove(x)

            i += 1

        while hq and (hq[0] not in closing):
            heapq.heappop(hq)

        if hq:
            print((hq[0]))
        else:
            print((-1))


if __name__ == '__main__':
    main()
    

# Optimized solution
def main():

    import sys
    import heapq

    N, Q = list(map(int, sys.stdin.readline().split()))
    events = []

    for i in range(N):
        s, t, x = list(map(int, sys.stdin.readline().split()))
        events.append((s - x, 1, x))  # 通行止め追加
        events.append((t - x, -1, x))  # 通行止め削除

    for i in range(Q):
        d = int(eval(input()))
        events.append((d, 2, 0))

    events.sort()
    events.append([float('inf'), 'guard', None])

    i = 0
    closing = set()
    hq = []  # 最小値を高速に入手するため
    while d >= events[i][0]:
        time, event, x = events[i]
        if event == 1:
            closing.add(x)
            heapq.heappush(hq, x)

        elif event == -1:
            closing.remove(x)

        else:
            while hq and (hq[0] not in closing):
                heapq.heappop(hq)

            if hq:
                print((hq[0]))
            else:
                print((-1))

        i += 1


if __name__ == '__main__':
    main()
# Original solution
#D in [S_i-X_i,T_i-X_i) → Xiで止まる
import sys
input = sys.stdin.readline

N,Q = list(map(int,input().split()))
q = []

STX = [[int(x) for x in input().split()] for _ in range(N)]
for s,t,x in STX:
  q.append((s-x,1,x)) # xで死ぬ
  q.append((t-x,0,x)) # xで死ななくなる

for i in range(Q):
  d = int(eval(input()))
  q.append((d,2,i))
answer = [-1]*Q

se = set() # 引っかかってる場所

q.sort()

for a,c,b in q:
  if not c:
    se.remove(b)
  elif c&1:
    se.add(b)
  else:
    if se:
      answer[b] = min(se)

print(('\n'.join(map(str,answer))))


# Optimized solution
#D in [S_i-X_i,T_i-X_i) → Xiで止まる
import sys
input = sys.stdin.readline

N,Q = list(map(int,input().split()))
q = []

STX = [[int(x) for x in input().split()] for _ in range(N)]
for s,t,x in STX:
  q.append((s-x,1,x)) # xで死ぬ
  q.append((t-x,0,x)) # xで死ななくなる

for i in range(Q):
  d = int(eval(input()))
  q.append((d,2,i))
answer = [-1]*Q

se = set() # 引っかかってる場所

q.sort()

min_se = 10**18
flag = False # min_seが正しい

for a,c,b in q:
  if not c:
    se.remove(b)
    if min_se == b:
      flag = False
  elif c&1:
    se.add(b)
    if b < min_se:
      min_se = b
      flag = True
  else:
    if se:
      if not flag:
        min_se = min(se)
        flag = True
      answer[b] = min_se

print(('\n'.join(map(str,answer))))
# Original solution
#D in [S_i-X_i,T_i-X_i) → Xiで止まる
import sys
input = sys.stdin.readline
from heapq import heappop,heappush
from collections import defaultdict

N,Q = list(map(int,input().split()))
task = []

STX = [[int(x) for x in input().split()] for _ in range(N)]
for s,t,x in STX:
  task.append((t-x,0,x)) # xで止まらなくなる
  task.append((s-x,1,x)) # xで止まる

for i in range(Q):
  d = int(eval(input()))
  task.append((d,2,i)) # 止まる位置を答える
answer = [-1]*Q

task.sort()

# 引っかかってる場所の管理
se = [] # heapでソートを保つ
added = defaultdict(int) # 入ってる個数

# 小さい時刻から順に見ていく
for a,b,c in task:
  if not b: # b == 0
    added[c] -= 1
  elif b&1: # b == 1
    heappush(se,c)
    added[c] += 1
  else: # b == 2
    while se and added[se[0]] == 0:
      heappop(se)
    answer[c] = se[0] if se else -1

print(('\n'.join(map(str,answer))))


# Optimized solution
#D in [S_i-X_i,T_i-X_i) → Xiで止まる
import sys
input = sys.stdin.readline
from heapq import heappop,heappush
from collections import defaultdict

N,Q = list(map(int,input().split()))
task = []

STX = [[int(x) for x in input().split()] for _ in range(N)]
for s,t,x in STX:
  task.append((t-x,0,x)) # xで止まらなくなる
  task.append((s-x,1,x)) # xで止まる

for i in range(Q):
  d = int(eval(input()))
  task.append((d,2,i)) # 止まる位置を答える
answer = [-1]*Q

task.sort()

# 引っかかってる場所の管理
se = set()
se_hp = [] # heapでソートを保つ

# 小さい時刻から順に見ていく
for a,b,c in task:
  if not b: # b == 0
    se.remove(c)
  elif b&1: # b == 1
    se.add(c)
    heappush(se_hp,c)
  else: # b == 2
    while se_hp and se_hp[0] not in se:
      heappop(se_hp)
    answer[c] = se_hp[0] if se_hp else -1

print(('\n'.join(map(str,answer))))