CodeEff/ECCO huggingface dataset:
# Original solution
from bisect import bisect
from time import time

st_time = time()
N = int(eval(input()))
src = list(map(int,input().split()))

cums = [0]
for a in src:
    cums.append(cums[-1] + a)

ans = cums[-1] * 999
c = bisect(cums, cums[-1]//2)
d = 1
while time() - st_time < 1.94:
    if abs(d) >= N: break
    if 2 <= c <= N-2:
        b1 = bisect(cums[:c], cums[c]//2)
        b2 = bisect(cums[c:], (cums[c]+cums[-1])//2) + c
        pq_cands = []
        for i in (b1,b1-1):
            if not 0 < i < c: continue
            p = cums[i]
            q = cums[c] - cums[i]
            pq_cands.append((p,q))
        rs_cands = []
        for i in (b2,b2-1):
            if not c < i < N: continue
            r = cums[i] - cums[c]
            s = cums[-1] - cums[i]
            rs_cands.append((r,s))
        for p,q in pq_cands:
            for r,s in rs_cands:
                mn = min(p,q,r,s)
                mx = max(p,q,r,s)
                ans = min(ans, mx-mn)
    c += d
    d = (abs(d)+1) * [1,-1][d%2]
print(ans)

# Optimized solution
from bisect import bisect
from time import time

st_time = time()
N = int(eval(input()))
src = list(map(int,input().split()))

cums = [0]
for a in src:
    cums.append(cums[-1] + a)

ans = cums[-1] * 999
c = bisect(cums, cums[-1]//2)
d = 1
while time() - st_time < 1.9:
    if abs(d) >= N: break
    if 2 <= c <= N-2:
        b1 = bisect(cums[:c], cums[c]//2)
        b2 = bisect(cums[c:], (cums[c]+cums[-1])//2) + c
        pq_cands = []
        for i in (b1,b1-1):
            if not 0 < i < c: continue
            p = cums[i]
            q = cums[c] - cums[i]
            pq_cands.append((p,q))
        rs_cands = []
        for i in (b2,b2-1):
            if not c < i < N: continue
            r = cums[i] - cums[c]
            s = cums[-1] - cums[i]
            rs_cands.append((r,s))
        for p,q in pq_cands:
            for r,s in rs_cands:
                mn = min(p,q,r,s)
                mx = max(p,q,r,s)
                ans = min(ans, mx-mn)
    c += d
    d = (abs(d)+1) * [1,-1][d%2]
print(ans)
# Original solution
from bisect import bisect
from time import time

st_time = time()
N = int(eval(input()))
src = list(map(int,input().split()))

cums = [0]
for a in src:
    cums.append(cums[-1] + a)

ans = cums[-1] * 999
c = bisect(cums, cums[-1]//2)
d = 1
while time() - st_time < 1.9:
    if abs(d) >= N: break
    if 2 <= c <= N-2:
        b1 = bisect(cums[:c], cums[c]//2)
        b2 = bisect(cums[c:], (cums[c]+cums[-1])//2) + c
        pq_cands = []
        for i in (b1,b1-1):
            if not 0 < i < c: continue
            p = cums[i]
            q = cums[c] - cums[i]
            pq_cands.append((p,q))
        rs_cands = []
        for i in (b2,b2-1):
            if not c < i < N: continue
            r = cums[i] - cums[c]
            s = cums[-1] - cums[i]
            rs_cands.append((r,s))
        for p,q in pq_cands:
            for r,s in rs_cands:
                mn = min(p,q,r,s)
                mx = max(p,q,r,s)
                ans = min(ans, mx-mn)
    c += d
    d = (abs(d)+1) * [1,-1][d%2]
print(ans)

# Optimized solution
from bisect import bisect
from time import time

st_time = time()
N = int(eval(input()))
src = list(map(int,input().split()))

cums = [0]
for a in src:
    cums.append(cums[-1] + a)

ans = cums[-1] * 999
c = bisect(cums, cums[-1]//2)
d = 1
while time() - st_time < 1.85:
    if abs(d) >= N: break
    if 2 <= c <= N-2:
        b1 = bisect(cums[:c], cums[c]//2)
        b2 = bisect(cums[c:], (cums[c]+cums[-1])//2) + c
        pq_cands = []
        for i in (b1,b1-1):
            if not 0 < i < c: continue
            p = cums[i]
            q = cums[c] - cums[i]
            pq_cands.append((p,q))
        rs_cands = []
        for i in (b2,b2-1):
            if not c < i < N: continue
            r = cums[i] - cums[c]
            s = cums[-1] - cums[i]
            rs_cands.append((r,s))
        for p,q in pq_cands:
            for r,s in rs_cands:
                mn = min(p,q,r,s)
                mx = max(p,q,r,s)
                ans = min(ans, mx-mn)
    c += d
    d = (abs(d)+1) * [1,-1][d%2]
print(ans)
# Original solution
from bisect import bisect
from time import time

st_time = time()
N = int(eval(input()))
src = list(map(int,input().split()))

cums = [0]
for a in src:
    cums.append(cums[-1] + a)

ans = cums[-1] * 999
c = bisect(cums, cums[-1]//2)
d = 1
while time() - st_time < 1.85:
    if abs(d) >= N: break
    if 2 <= c <= N-2:
        b1 = bisect(cums[:c], cums[c]//2)
        b2 = bisect(cums[c:], (cums[c]+cums[-1])//2) + c
        pq_cands = []
        for i in (b1,b1-1):
            if not 0 < i < c: continue
            p = cums[i]
            q = cums[c] - cums[i]
            pq_cands.append((p,q))
        rs_cands = []
        for i in (b2,b2-1):
            if not c < i < N: continue
            r = cums[i] - cums[c]
            s = cums[-1] - cums[i]
            rs_cands.append((r,s))
        for p,q in pq_cands:
            for r,s in rs_cands:
                mn = min(p,q,r,s)
                mx = max(p,q,r,s)
                ans = min(ans, mx-mn)
    c += d
    d = (abs(d)+1) * [1,-1][d%2]
print(ans)

# Optimized solution
N = int(eval(input()))
src = list(map(int,input().split()))

cums = [0]
for a in src:
    cums.append(cums[-1] + a)

ps = [None] * (N+1)
qs = [None] * (N+1)
rs = [None] * (N+1)
ss = [None] * (N+1)

l = 1
for r in range(2,N-1):
    p1 = cums[l]
    q1 = cums[r] - cums[l]
    d1 = abs(p1-q1)
    while l < r:
        p2 = cums[l+1]
        q2 = cums[r] - cums[l+1]
        d2 = abs(p2-q2)
        if d2 > d1: break
        l += 1
        p1,q1,d1 = p2,q2,d2
    ps[r] = p1
    qs[r] = q1

r = N-1
for l in range(N-2,1,-1):
    r1 = cums[r] - cums[l]
    s1 = cums[N] - cums[r]
    d1 = abs(r1-s1)
    while l < r:
        r2 = cums[r-1] - cums[l]
        s2 = cums[N] - cums[r-1]
        d2 = abs(r2-s2)
        if d2 > d1: break
        r -= 1
        r1,s1,d1 = r2,s2,d2
    rs[l] = r1
    ss[l] = s1

ans = cums[-1] * 999
for p,q,r,s in zip(ps,qs,rs,ss):
    if p is None: continue
    tmp = max(p,q,r,s) - min(p,q,r,s)
    ans = min(ans, tmp)
print(ans)
# Original solution
# arc 100 D

def ruiseki(n,array):
    res = [0]*(n+1)
    for i in range(n):
        res[i+1] = res[i] + array[i]
    return res

n = int(eval(input()))
A = list(map(int,input().split()))
sa = ruiseki(n,A)

from bisect import bisect_right

ans = 10**15
for i in range(1,n-2):
    left = sa[i+1]
    right = sa[-1] - sa[i+1]

    lsplit = min(bisect_right(sa[:i+1],left//2) - 2,i-1)
    rsplit = min(bisect_right(sa[i+1:],right//2 + left) + i-1,n-2)
    lleft = [(sa[lsplit + 1],sa[i+1] - sa[lsplit + 1]),(sa[lsplit + 2],sa[i+1] - sa[lsplit + 2])]
    rright = [(sa[rsplit+1] - sa[i+1],sa[-1] - sa[rsplit+1]),(sa[rsplit+2] - sa[i+1],sa[-1] - sa[rsplit+2])]
    for s,t in [(0,0),(0,1),(1,0),(1,1)]:
        l = lleft[s]
        r = rright[t]
        m = min(l+r)
        M = max(l+r)
        ans = min(ans,M-m)

print(ans)




# Optimized solution
# arc 100 D

def ruiseki(n,array):
    res = [0]*(n)
    for i in range(n):
        res[i] = res[i-1] + array[i]
    return res

n = int(eval(input()))
A = list(map(int,input().split()))
sa = ruiseki(n,A)

ans = 10**15
left_c = 0
right_c = 2
for center in range(1,n-1):
    while left_c+1 < center:
        if abs(sa[center]-2*sa[left_c]) > abs(sa[center]-2*sa[left_c+1]):
            left_c+=1
        else:
            break

    right_c = max(right_c, center+1)
    while right_c+1 < n-1:
        if abs(sa[-1] + sa[center] - 2*sa[right_c]) > abs(sa[-1] + sa[center] - 2*sa[right_c+1]):
            right_c+=1
        else:
            break
    
    x = max(sa[left_c], sa[center]-sa[left_c], sa[right_c]-sa[center], sa[-1]-sa[right_c])
    y = min(sa[left_c], sa[center]-sa[left_c], sa[right_c]-sa[center], sa[-1]-sa[right_c])
    ans = min(ans, x-y)

print(ans)
# Original solution
import random
from itertools import accumulate
N = int(eval(input()))
As = [int(i) for i in input().split(' ')]

#N = pow(10, 4)
#As = [random.randint(0,1000) for r in range(N)] 
As = list(accumulate(As))


def bsearch(L, l, r, ls, rs):
    if r - l == 0:
        l_temp = L[l-1] if l!=0 else 0
        lsum = l_temp - ls
        return [max(lsum, rs - L[l]), min(lsum, rs - L[l]) + L[l] - l_temp]
    n = int((r-l)/2)
    lsum = L[l+n] - ls
    rsum = rs - L[l+n]
    if lsum > rsum:
        return bsearch(L, l, l+n, ls, rs)
    else:
        return bsearch(L, l+n+1, r, ls, rs)

for i in range(N-3):
    cs = bsearch(As, 0, i+1, 0, As[i+1]) + bsearch(As ,i+2, N-1, As[i+1], As[-1])
    if i == 0:
        result = max(cs) - min(cs)
    else:
        if max(cs) - min(cs) < result:
            result = max(cs) - min(cs)
print(result)


# Optimized solution

def bsearch(mn, mx, func):
    #func(i)=False を満たす最大のi
    idx = (mx + mn)//2
    while mx-mn>1:
        if func(idx):
            idx, mx = (idx + mn)//2, idx
            continue
        idx, mn = (idx + mx)//2, idx
    return idx
N, = list(map(int, input().split()))
A = list(map(int, input().split()))
X = [0]*(N+2)
for i in range(N):
    X[i+1] = X[i] + A[i]

R = 10**18
for i in range(2, N-1):
    # X0, X1, X2 .. Xi
    # Xj Xj+1 (1<= j <= i-1)
    idx1 = bsearch(1, i-1, lambda j:X[i]//2 < X[j])
    idx2 = bsearch(i+1, N, lambda j:(X[N]-X[i])//2 < (X[j]-X[i]))
#    print(X[idx1], X[i]-X[idx1], X[idx2]-X[i], X[N]-X[idx2])
    if idx1+1 <= i-1:
        if abs(2*X[idx1]-X[i]) > abs(2*X[idx1+1]-X[i]):
            idx1+=1
#    print(1,i,idx2)
    if idx2+1 <= N-1:
        if abs(2*X[idx2]-X[i]-X[N]) > abs(2*X[idx2+1]-X[i]-X[N]):
            idx2+=1
    mx = max([X[idx1], X[i]-X[idx1], X[idx2]-X[i], X[N]-X[idx2]])
    mn = min([X[idx1], X[i]-X[idx1], X[idx2]-X[i], X[N]-X[idx2]])
#    print(X[i]-X[idx1], X[idx1], X[idx2]-X[i])
#    print(idx1, i, idx2)
#    print(mx-mn)
    R = min(R, mx-mn)
print(R)
# Original solution
def inpl(): return [int(i) for i in input().split()]
N, K = inpl()
H = {0: 0}
for i in range(N):
    a, b= inpl()
    for ia, ib in list(H.copy().items()):
        if ia | a <= K or (ia == 0 and a <= K):
            H[ia|a] = max(H.get(ia|a, 0), ib+b)
print((max(H.values())))

# Optimized solution
import sys
N, K = list(map(int, input().split()))
candi = []
p = 0
for i in range(30, -1, -1):
    if 2**i & K == 0:
        p += 2**i
        continue
    else:
        candi.append(p + 2**i)
candi.append(p)
AB = [tuple(map(int, input().split())) for _ in range(N)]
ans = 0

for c in candi:
    cans = 0
    for a, b in AB:
        if a & c:
            continue
        cans += b
    ans = max(ans, cans)
print(ans)
# Original solution
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n,k=list(map(int,input().split()))
    k+=1
    AB=[tuple(map(int,input().split())) for _ in range(n)]

    dp=[0]*35 # dp[d]: less than が確定するのが d 桁目のときの最大値
    # dp[d] の状態のものが a を買えるのは、d 以上の桁については ad<=kd かつ、
    # d 桁目については ad<kd となるもののみ
    for a,b in AB:
        ndp=dp[:]
        for d in range(34,-1,-1):
            kd=(k>>d)&1
            ad=(a>>d)&1
            if(ad>kd): break
            if(ad<kd):
                ndp[d]=dp[d]+b
        dp=ndp

    print((max(dp)))
resolve()

# Optimized solution
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n,k=list(map(int,input().split()))
    k+=1
    AB=[tuple(map(int,input().split())) for _ in range(n)]

    dp=[0]*35 # dp[d]: less than が確定するのが d 桁目のときの最大値
    # dp[d] の状態のものが a を買えるのは、d 以上の桁については ad<=kd かつ、
    # d 桁目については ad<kd となるもののみ
    for a,b in AB:
        for d in range(34,-1,-1):
            kd=(k>>d)&1
            ad=(a>>d)&1
            if(ad>kd): break
            if(ad<kd):
                dp[d]=dp[d]+b

    print((max(dp)))
resolve()
# Original solution
N,K,*X = list(map(int, open(0).read().split()))
S = bin(K)[2:]
L = len(S)
one = set()
ls = [(a,b) for a,b in zip(*[iter(X)]*2) if a<=K]
kls = [K]
ans = 0
for i in range(L):
  if S[i]=='1':
    m = int(S[:i]+'0'+'1'*(L-i-1),2)
    kls.append(m)
for k in kls:
  m = 0
  for a,b in ls:
    if k==k|a:
      m += b
  ans = max(ans,m)
print(ans)

# Optimized solution
N,K,*X = list(map(int,open(0).read().split()))
S = bin(K)[2:]
L = len(S)
ls = [(a,b) for a,b in zip(*[iter(X)]*2)]
s = '0'
ans = 0
for i,c in enumerate(S):
  if c=='0':
    s += c
    continue
  j = L-i
  m = (1<<(j-1))-1
  n = int(s,2)<<j
  x = m|n
  v = 0
  for a,b in ls:
    if x==x|a:
      v += b
  ans = max(v,ans)
  s += c
v = 0
for a,b in ls:
  if K==K|a:
    v += b
ans = max(ans,v)
print(ans)
# Original solution
import sys

input=sys.stdin.readline

N,K=list(map(int,input().split()))
integer=[]
for i in range(N):
    a,b=list(map(int,input().split()))
    integer.append([a,b])

bit=[]
for i in range(0,30):
    if K>>i &1==1:
        bit.append(i)

ans=0
for j in range(N):
    test=0
    for j in range(N):
        a,b=integer[j]
        if a|K==K:
            test+=b
    ans=max(ans,test)

for i in bit:
    check=(K>>i)-1
    test=0
    for j in range(N):
        a,b=integer[j]
        if a>>i|check==check:
            test+=b
    ans=max(ans,test)

print(ans)

# Optimized solution
import sys

input=sys.stdin.readline

N,K=list(map(int,input().split()))
integer=[]
for i in range(N):
    a,b=list(map(int,input().split()))
    integer.append([a,b])

bit=[]
for i in range(0,30):
    if K>>i &1==1:
        bit.append(i)

ans=0
for j in range(N):
    a,b=integer[j]
    if a|K==K:
        ans+=b

for i in bit:
    check=(K>>i)-1
    test=0
    for j in range(N):
        a,b=integer[j]
        if a>>i|check==check:
            test+=b
    ans=max(ans,test)

print(ans)
# Original solution
n,k = list(map(int, input().split()))

ans = 0
abl = []
for _ in range(n):
    a,b = list(map(int, input().split()))
    abl.append((a,b))

max_bit = 0
for i in range(31):
    if k < pow(2,i):
        max_bit = i
        break

k_bits = [-1]*(max_bit)
for i in range(max_bit-1,-1,-1):
    if k&(1<<i) > 0:
        k_bits[i] = 1
    else:
        k_bits[i] = 0

# print(k_bits)

k_bits = k_bits[::-1]
bits_cands = []
bits_cands.append(k_bits[::-1])
for i in range(max_bit):
    curr_row = []
    for j in range(0,i):
        curr_row.append(k_bits[j])
    if k_bits[i] == 1:
        curr_row.append(0)
        curr_row = curr_row + [1]*(max_bit-1-i)
    else:
        for j in range(i,max_bit):
            curr_row.append(k_bits[j])
    bits_cands.append(curr_row[::-1])

# print(bits_cands)
ans = 0
for bits in bits_cands:
    val = 0
    for a,b in abl:
        if a > k: continue
        for i in range(max_bit):
            if a&(1<<i) > 0 and bits[i] == 0:
                break
        else:
            val += b
    ans = max(ans,val)
print(ans)

# bits_cands = []
# print(max_bit)
# curr = [1]
# for i in range(max_bit-1,-1,-1):
#     if k%(1<<i) == 0:
#         curr.append(0)
#         row = curr + []

# Optimized solution
n,k = list(map(int, input().split()))
ans = 0
abl = [ tuple(map(int, input().split())) for _ in range(n) ]

kset = set()
kset.add(k)
for i in range(30):
    if k&(1<<i) == 0: continue
    v = 0
    for j in range(i):
        v += pow(2,j)
    for j in range(i+1,30):
        if k&(1<<j) > 0:
            v += pow(2,j)
    kset.add(v)

kl = list(kset)
ans = 0
for ki in kl:
    v = 0
    for a,b in abl:
        if a > k: continue
        for i in range(30):
            if a&(1<<i) > 0 and ki&(1<<i) == 0:
                break
        else:
            v += b
    ans = max(ans, v)

print(ans)
# Original solution
from collections import defaultdict
N, K = list(map(int, input().split()))
AB = [[int(i) for i in input().split()] for _ in range(N)]

visited = defaultdict(lambda : False)
L = set()
def dfs(x) :
    if visited[x] :
        return 
        
    visited[x] = True
    for i in range(30) :
        if x & (1 << i) == 0 :
            continue
            
        y = x ^ (1 << i)
        if y <= K :
            L.add(y)
        else :
            dfs(y)


dfs((1 << (len(bin(K)) - 2)) - 1)
L.add(K)

ret = 0
for mask in L :
    s = 0
    for A, B in AB :
        if A & ~mask == 0 :
            s += B
    ret = max(ret, s)
    
print(ret)

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

ret = 0
for A, B in AB :
    if A & ~K == 0 :
        ret += B
        
for i in range(30) :
    if K & (1 << i) == 0 :
        continue
        
    k = K
    k ^= 1 << i
    k |= (1 << i) - 1
    s = 0
    for A, B in AB :
        if A & ~k == 0 :
            s += B
            
    ret = max(ret, s)
    
print(ret)
# Original solution
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
import copy

N, K = list(map(int, sys.stdin.readline().rstrip().split()))
data = []
for _ in range(N):
    A, B = list(map(int, sys.stdin.readline().rstrip().split()))
    # print(A)
    # print(bin(A)[2:])
    data.append([list(reversed(bin(A)[2:])), B])

ans = 0


def lower(x, y):
    if(len(x) > len(y)):
        return False
    for xx, yy in zip(x, y):
        if (xx == "1" and yy == "0"):
            return False
    else:
        return True


Nstr = list(reversed(bin(K)[2:]))
subNstrs = [Nstr]
for i in range(len(Nstr)):
    if Nstr[i] == "0":
        continue
    subNstr = copy.deepcopy(Nstr)
    subNstr[i] = "0"
    for j in range(i):
        subNstr[j] = "1"
    subNstrs.append(subNstr)


for subNstr in subNstrs:
    subans = 0
    for A, B in data:
        # print(A, subNstr, lower(A, subNstr))
        if(lower(A, subNstr)):
            subans += B
    # print(subNstr, subans)
    ans = max(subans, ans)

print(ans)

exit(0)


# Optimized solution
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
import copy

N, K = list(map(int, sys.stdin.readline().rstrip().split()))
data = []
for _ in range(N):
    A, B = list(map(int, sys.stdin.readline().rstrip().split()))
    data.append([A, B])

ans = 0


def lower(x, y):
    if(x > y):
        return False
    # print(bin(x), bin(y), bin(x|y), bin((x | y) ^ y))
    if((x | y) ^ y) == 0:
        return True
    else:
        return False


Nstr = list(reversed(bin(K)[2:]))
subNs = [K]
for i in range(len(Nstr)):
    if Nstr[i] == "0":
        continue
    subNstr = copy.deepcopy(Nstr)
    subNstr[i] = "0"
    for j in range(i):
        subNstr[j] = "1"

    realstr = ""
    for s in reversed(subNstr):
        realstr += s
    subNs.append(int(realstr, 2))


for subNstr in subNs:
    subans = 0
    for A, B in data:
        # print(A, subNstr, lower(A, subNstr))
        if(lower(A, subNstr)):
            subans += B
    # print(subNstr, subans)
    ans = max(subans, ans)

print(ans)

exit(0)
# Original solution
def issubset(a,b):
    #a<<bならtrue
    x=bin(a)[2:]
    y=bin(b)[2:]
    if a>b:
        return False
    for i in range(1,len(x)+1):
        if (x[-i],y[-i])==("1","0"):
            return False
    return True
N,K=list(map(int,input().split()))
A=[0 for i in range(N)]
B=[0 for i in range(N)]
for i in range(N):
    A[i],B[i]=list(map(int,input().split()))
Kl=[K]
Ks=bin(K)[2:]
for i,c in enumerate(Ks):
    if c=="1":
        Kl.append(int(Ks[:i]+"0"+"1"*(len(Ks)-i-1),2))
ans=0
for k in Kl:
    tmp=0
    for i in range(N):
        if issubset(A[i],k):
            tmp+=B[i]
    ans=max(tmp,ans)
print(ans)


# Optimized solution
def issubset(x,y):
    #a<<bならtrue
    if len(x)>len(y):
        return False
    for i in range(1,len(x)+1):
        if (x[-i],y[-i])==("1","0"):
            return False
    return True
N,K=list(map(int,input().split()))
A=[0 for i in range(N)]
B=[0 for i in range(N)]
for i in range(N):
    A[i],B[i]=list(map(int,input().split()))
    A[i]=bin(A[i])[2:]
Ks=bin(K)[2:]
Kl=[Ks]
for i,c in enumerate(Ks):
    if c=="1":
        Kl.append(Ks[:i]+"0"+"1"*(len(Ks)-i-1))
ans=0
for k in Kl:
    tmp=0
    for i in range(N):
        if issubset(A[i],k):
            tmp+=B[i]
    ans=max(tmp,ans)
print(ans)
# Original solution
import bisect
import copy
import heapq
import math
import sys
from collections import *
from itertools import accumulate, combinations, permutations, product
from math import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

n,k=list(map(int,input().split()))
ab=[list(map(int,input().split())) for i in range(n)]

ans=0
ab.sort()
m=bin(k)
# print(m)
klst=[m]
for i in range(2,len(m)):
    if m[i]=="1":
        klst.append(m[:i]+"0"+"1"*(len(m)-i-1))

# print(klst)
for i in klst:
    tmp=0
    for j in range(n):
        a,b=ab[j]
        m=bin(a)
        if int(i,0)>=a:
            for l in range(min(len(m),len(i))):
                if i[-1-l]=="0" and m[-1-l]=="1":
                    break
            else:
                tmp+=b
    ans=max(ans,tmp)
print(ans)

# Optimized solution
import bisect
import copy
import heapq
import math
import sys
from collections import *
from itertools import accumulate, combinations, permutations, product
from math import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

n,k=list(map(int,input().split()))
ab=[list(map(int,input().split())) for i in range(n)]

ans=0
ab.sort()
m=bin(k)
# print(m)
klst=[m]
for i in range(2,len(m)):
    if m[i]=="1":
        klst.append(m[:i]+"0"+"1"*(len(m)-i-1))

# print(klst)
for i in klst:
    tmp=0
    for j in range(n):
        a,b=ab[j]
        if a|int(i,0)==int(i,0):
            tmp+=b
    ans=max(ans,tmp)
print(ans)
# Original solution
n,k=list(map(int,input().split()))
ab=[list(map(int,input(). split())) for _ in range(n)]
bk=list(bin(k)[2:])
if '0' not in bk:
  t=k
  ans=0
  for a,b in ab:
    if a<=t:ans+=b
  print(ans)
else:
  ks=[k]
  for i in range(len(bk)):
    if bk[i]=='1':
      if i==0:
        ks.append(int('1'*(len(bk)-1),2))
      else:
        ks.append(int(''.join(bk[:i])+'0'+'1'*(len(bk)-i-1),2))
  ans=[0]*len(ks)
  for a,b in ab:
    for i in range(len(ks)):
      if a&ks[i]==a:ans[i]+=b
  print((max(ans)))

# Optimized solution
def main(n,k,ab):
  ks=[k]
  m=len(bin(k))-2
  # kはm桁
  kbin=bin(k)[2:]
  now=0
  for i in range(m):
    if kbin[i]=='1' and i<m-1:
      ks.append(now+pow(2,m-i-1)-1)
    now+=int(kbin[i])*pow(2,m-i-1)
  ans=0
  for x in ks:
    tmp=0
    for a,b in ab:
      if a|x==x:tmp+=b
    ans=max(ans,tmp)
  print(ans)

n,k=list(map(int,input().split()))
ab=[list(map(int,input().split())) for _ in range(n)]
main(n,k,ab)