ManavSinghal157/NoFunEval huggingface dataset:
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 756 ms):
import numpy as np

n, k = list(map(int, input().split()))
z = np.zeros(10**5 + 1)
for i in range(n):
  a, b = list(map(int, input().split()))
  z[a] += b

a = 0
for i in range(1, 10**5 + 1):
  if k <= z[i]:
    a = i
    break
  k -= z[i]

print((int(a)))


Optimized program (CPU time: 620 ms):
n, k = list(map(int, input().split()))
d = {}
for i in range(n):
  a, b = list(map(int, input().split()))
  d[a] = d[a] + b if a in d else b
 
a = 0
for i in range(1, 10**5+1):
  if i in d and k <= d[i]:
    a = i
    break
  k -= d[i] if i in d else 0
 
print(a)
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 557 ms):
import numpy as np
N = int(eval(input()))
a = list(map(int,input().split()))
ans = 0
a = np.array(a)
while a.size > 0:
    a = a[a%2 == 0]
    ans += len(a)
    a = a//2
print(ans)

Optimized program (CPU time: 99 ms):
n = int(eval(input()))
a = list(map(int,input().split()))
ans = 0
for i in range(n):
    while a[i]%2 == 0:
        a[i] //=2
        ans+=1
print(ans)
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 126 ms):
x = int(eval(input()))
if x == 0:
  print((1))
else:
  print((0))

Optimized program (CPU time: 24 ms):
x = int(eval(input()))
print((x^1))
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 295 ms):
import numpy as np
n = int((input()))
s = list((input()))
j = []
now = 0
unclosen = 0
opened = 0
for i in s:
    if i == ")":
        now -= 1
        if opened:
            opened -= 1
        else:
            unclosen += 1
    else:
        now += 1
        opened += 1
j = np.array(j)
print(("(" * unclosen + "".join(s) + ")" * opened))

Optimized program (CPU time: 17 ms):
n = int((input()))
s = list((input()))
now = 0
unclosen = 0
opened = 0
for i in s:
    if i == ")":
        now -= 1
        if opened:
            opened -= 1
        else:
            unclosen += 1
    else:
        now += 1
        opened += 1
print(("(" * unclosen + "".join(s) + ")" * opened))
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 150 ms):
import numpy as np
N, D = list(map(int, input().split()))
print((int(np.ceil(N/(2*D+1)))))

Optimized program (CPU time: 21 ms):
N, D = list(map(int, input().split()))
import math
print((math.ceil(N/(2*D+1))))
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 352 ms):
N, M =list(map(int, input().split()))
L = [0 for i in range(N+1)]
R = [0 for i in range(N+1)]
for i in range(M):
    l,r = list(map(int, input().split()))
    L[l] += 1
    R[r] += 1
cnt = 0
tmp = 0
for i in range(N+1):
    tmp += L[i]
    if tmp == M:
        cnt += 1
    tmp -= R[i]
print(cnt)


Optimized program (CPU time: 284 ms):
n,m = list(map(int, input().split()))
ll = 1
rr = n
for i in range(m):
    l,r = list(map(int,input().split()))
    if l > ll:
        ll = l
    if r < rr:
        rr = r
print((max(rr - ll + 1, 0)))
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 276 ms):
# インポート
import numpy as np
# A,B,Cの値の獲得
num = list(map(int, input().split()))
# numリストの最大値を10倍
num[num.index(max(num))] = max(num) * 10
# npで要素の和を計算し、出力
print((np.sum(num)))

Optimized program (CPU time: 17 ms):
# A,B,Cの値の獲得
num = list(map(int, input().split()))
# numリストの最大値を10倍
num[num.index(max(num))] *= 10
# npで要素の和を計算し、出力
print((sum(num)))
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 541 ms):
n,t=list(map(int,input().split()))
ab=[list(map(int,input().split()))for _ in range(n)]
ab.sort()
dp=[(6007)*[0]for _ in range(n+1)]
dp[0][0]=0
ans=0
for i in range(n):
  for j in range(6007):
    dp[i+1][j]=max(dp[i+1][j],dp[i][j])
    if j<t:dp[i+1][j+ab[i][0]]=max(dp[i][j]+ab[i][1],dp[i][j+ab[i][0]])
    ans=max(ans,dp[i+1][j])
print(ans)


Optimized program (CPU time: 362 ms):
n,t=list(map(int,input().split()))
dp=[[0]*(t+3001)for _ in range(n+1)]
ans=0
ab=[list(map(int,input().split()))for _ in range(n)]
ab.sort()
for i in range(1,n+1):
    a,b=ab[i-1]
    for j in range(t):
        dp[i][j]=max(dp[i-1][j],dp[i][j])
        dp[i][j+a]=dp[i-1][j]+b
        ans=max(dp[i][j],dp[i][j+a],ans)
print(ans)
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 968 ms):
import numpy as np
NM = list(map(int,input().split()))
N = NM[0]
M = NM[1]
A = list(map(int,input().split()))
listBC =[]
while True:
    try:
        listBC.append(list(map(int,input().split())))
    except:
        break;
BC = np.array(listBC)
BCnum = np.argsort(BC[:,1])[::-1]
BC = BC[BCnum,:]

A.sort()
j=0
for i in range(len(A)):
    times = BC[j,0]
    if BC[j,0]==0:
        j+=1
        if j == BC.shape[0]:
            break
        times = BC[j,0]
    if A[i]<BC[j,1]:
        A[i]=BC[j,1]
        BC[j,0] += -1
output = sum(A)
print(output)

Optimized program (CPU time: 337 ms):
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))

from collections import Counter
D = dict(Counter(A))

for i in range(M):
    B,C = list(map(int,input().split()))
    D[C]=D.get(C,0)+B

K = sorted(list(D.keys()),reverse=True)

out = 0
cnt = 0
now = 0
while cnt<=N-1:
    if D[K[now]]>0:
        out += K[now]
        D[K[now]] += -1
        cnt+=1
    else:
        now+=1
print(out)
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 294 ms):
N = int(eval(input()))
X = [[] for i in range(N)]
for i in range(N-1):
    x, y = list(map(int, input().split()))
    X[x-1].append(y-1)
    X[y-1].append(x-1)

P = [-1] * N
DE = [0] * N
Q = [0]
while Q:
    i = Q.pop()
    for a in X[i][::-1]:
        if a != P[i]:
            P[a] = i
            DE[a] = DE[i] + 1
            X[a].remove(i)
            Q.append(a)

def lp(u, v):
    t = 0
    while u != v:
        if DE[u] > DE[v]:
            t += 1 << u-1
            u = P[u]
        elif DE[u] < DE[v]:
            t += 1 << v-1
            v = P[v]
        else:
            t += 1 << u-1
            t += 1 << v-1
            u = P[u]
            v = P[v]
    
    return t

Y = []
M = int(eval(input()))
for _ in range(M):
    a, b = list(map(int, input().split()))
    a, b = a-1, b-1
    Y.append(lp(a, b))

D = {1<<i: i for i in range(50)}
Z = [0] * (1<<M)
ans = 0
CC = [0] * N
BC = [0] * (1<<17)
for m in range(1, 1<<17):
    a = m & (-m)
    BC[m] = BC[m^a] + 1
for m in range(1<<M):
    a = m & (-m)
    if a == m:
        if a == 0:
            Z[m] = 0
        else:
            Z[m] = Y[D[a]]
    else:
        Z[m] = Z[m^a] | Y[D[a]]
    
    aa = Z[m]
    bc = BC[aa % (1<<17)]
    aa >>= 17
    bc += BC[aa % (1<<17)]
    aa >>= 17
    bc += BC[aa]
    CC[N - 1 - bc] += (1 if BC[m%1024] + BC[m>>10] & 1 == 0 else -1)

print((sum([2 ** i * CC[i] for i in range(N)])))

Optimized program (CPU time: 228 ms):
N = int(eval(input()))
X = [[] for i in range(N)]
for i in range(N-1):
    x, y = list(map(int, input().split()))
    X[x-1].append(y-1)
    X[y-1].append(x-1)

P = [-1] * N
DE = [0] * N
Q = [0]
while Q:
    i = Q.pop()
    for a in X[i][::-1]:
        if a != P[i]:
            P[a] = i
            DE[a] = DE[i] + 1
            X[a].remove(i)
            Q.append(a)

def lp(u, v):
    t = 0
    while u != v:
        if DE[u] > DE[v]:
            t += 1 << u-1
            u = P[u]
        elif DE[u] < DE[v]:
            t += 1 << v-1
            v = P[v]
        else:
            t += 1 << u-1
            t += 1 << v-1
            u = P[u]
            v = P[v]
    
    return t

Y = []
M = int(eval(input()))
for _ in range(M):
    a, b = list(map(int, input().split()))
    a, b = a-1, b-1
    Y.append(lp(a, b))

def popcnt(n):
    c = (n & 0x5555555555555555) + ((n>>1) & 0x5555555555555555)
    c = (c & 0x3333333333333333) + ((c>>2) & 0x3333333333333333)
    c = (c & 0x0f0f0f0f0f0f0f0f) + ((c>>4) & 0x0f0f0f0f0f0f0f0f)
    c = (c & 0x00ff00ff00ff00ff) + ((c>>8) & 0x00ff00ff00ff00ff)
    c = (c & 0x0000ffff0000ffff) + ((c>>16) & 0x0000ffff0000ffff)
    c = (c & 0x00000000ffffffff) + ((c>>32) & 0x00000000ffffffff)
    return c

D = {1<<i: i for i in range(50)}
Z = [0] * (1<<M)
ans = 0
CC = [0] * N
for m in range(1<<M):
    a = m & (-m)
    if a == m:
        if a == 0:
            Z[m] = 0
        else:
            Z[m] = Y[D[a]]
    else:
        Z[m] = Z[m^a] | Y[D[a]]
    
    CC[N - 1 - popcnt(Z[m])] += (1 if popcnt(m) & 1 == 0 else -1)

print((sum([2 ** i * CC[i] for i in range(N)])))
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 840 ms):
from decimal import *
getcontext().prec = 14

N, D = list(map(int, input().split()))


def distance(x1, x2, y1, y2):
    dx = x2-x1
    dy = y2-y1
    return (dx*dx + dy*dy).sqrt()


X = [0] * N
Y = [0] * N
ans = 0
for i in range(N):
    X[i], Y[i] = list(map(Decimal, input().split()))
    if (distance(0, X[i], 0, Y[i]) <= D):
        ans += 1

print(ans)


Optimized program (CPU time: 755 ms):
from decimal import *
getcontext().prec = 14  # あんまり大きいと計算遅いかも

N, D = list(map(int, input().split()))

def distance(x1, x2, y1, y2):
    dx = x2-x1
    dy = y2-y1
    return (dx*dx + dy*dy).sqrt()

ans = 0
for i in range(N):
    X, Y = list(map(Decimal, input().split()))
    if (distance(0, X, 0, Y) <= D):
        ans += 1

print(ans)
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 40 ms):
ia = [int(i) for i in input().split(" ")]
W=ia[0]
H=ia[1]
x=ia[2]
y=ia[3]
r=ia[4]
print(("Yes" if 0<=x-r and x+r<=W and 0<=y-r and y+r<=H else "No"))

Optimized program (CPU time: 30 ms):
# encoding:utf-8

input = list(map(int, input().split()))
W, H, x, y, r = input

if x - r < 0 or x + r > W:
	print("No")
elif y - r < 0 or y + r > H:
	print("No")
else:
	print("Yes")
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 254 ms):
import numpy as np

w = (input())
anal = np.array([])

for i in range(ord('a'),ord('z')+1):
    anal = np.append(anal,w.count(chr(i)))
if np.all(anal % 2 == 0):
    #ans = np.sum(anal)
    print("Yes")
else:
    print("No")

Optimized program (CPU time: 17 ms):
w = str((input()))
count = int()

for i in range(len(w)):
    if w.count(w[i]) % 2 == 0:
        count += 1

if count == len(w):
    print('Yes')
else:
    print('No')
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 116 ms):
import numpy as np
N, K = list(map(int, input().split()))
 
A = np.zeros(N, dtype=int)
 
for _ in range(K):
    eval(input())
    for i in input().split():
        A[int(i) - 1] += 1
 
ans = 0
for i in A:
    if i == 0:
        ans += 1
print(ans)
 

Optimized program (CPU time: 60 ms):
N, K = list(map(int, input().split()))

A = [0] * N

for _ in range(K):
    eval(input())
    for i in map(int, input().split()):
        A[i - 1] += 1

print((A.count(0)))
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 918 ms):
import sys
sys.setrecursionlimit(100000)
N, M = list(map(int, input().split()))
edges = [list(map(int, input().split())) for _ in range(M)]

graph = [[] for _ in range(N)]
for x, y in edges:
    graph[x - 1].append(y - 1)
    graph[y - 1].append(x - 1)

def dfs(v, c):
    # c: color = 1 or -1
    node[v] = c
    for i in graph[v]:
        if node[i] == c:
            return False
        
        if node[i] == 0 and not dfs(i, -c):
            return False
        
    return True

node = [0] * N
if dfs(0, 1):
    x = sum(v + 1 for v in node) // 2
    print((x * (N - x) - M))
else:
    print((N * (N - 1) // 2 - M))


Optimized program (CPU time: 684 ms):
from collections import deque
N, M = list(map(int, input().split()))
edges = [list(map(int, input().split())) for _ in range(M)]

graph = [[] for _ in range(N)]
for x, y in edges:
    graph[x - 1].append(y - 1)
    graph[y - 1].append(x - 1)

def dfs(s, c):
    q = deque()
    q.append(s)
    visited[s] = True
    color[s] = c
    
    while len(q) > 0:
        v = q.pop()
        for i in graph[v]:
            if visited[i] and color[i] == color[v]:
                return False
            
            if not visited[i]:
                visited[i] = True
                color[i] = -color[v]
                q.append(i)
                
    return True

visited = [False] * N
color = [0] * N

if dfs(0, 1):
    x = sum(v + 1 for v in color) // 2
    print((x * (N - x) - M))
else:
    print((N * (N - 1) // 2 - M))
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 295 ms):
import numpy as np
(n, m) = list(map(int, input().split()))
a = np.array(list(map(int, input().split()))).astype("int64") 
a = np.cumsum(a)
a %= m
count = np.sum(a == 0)
a.sort()
tc = 1
for i in range(1, n):
    if a[i - 1] == a[i]:
        tc += 1
    else:
        count += tc * (tc - 1) // 2
        tc = 1
else:
    count += tc * (tc - 1) // 2
print(count)

Optimized program (CPU time: 119 ms):
(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
unique_a = {a[0] % m: 1}
for i in range(1, n):
    a[i] += a[i - 1]
    a[i] %= m
    if a[i] in unique_a:
        unique_a[a[i]] += 1
    else:
        unique_a[a[i]] = 1
count = 0
if 0 in unique_a:
    count += unique_a[0]
for k in unique_a:
    count += unique_a[k] * (unique_a[k] - 1) // 2
print(count)
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 799 ms):
from functools import reduce
import fractions
import numpy as np

N, M = list(map(int, input().split()))
a = np.array(list(map(int, input().split())))

a = a // 2

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)

l = lcm(*a)
exist = not 0 in ((l//a)%2)

if exist:
    print(((M // l + 1 ) // 2))
else:
    print((0))

Optimized program (CPU time: 244 ms):
import sys, fractions, functools
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(eval(input()))
def F(): return float(eval(input()))
def S(): return eval(input())
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return functools.reduce(lcm_base, numbers, 1)

def resolve():
    N, M = LI()
    a = LI()

    a_half = [i//2 for i in a]
    a_half_lcm = lcm_list(a_half)
    has_scm = not 0 in [a_half_lcm//i%2 for i in a_half]

    if has_scm:
        print(((M-a_half_lcm)//(2*a_half_lcm)+1))
    else:
        print((0))

if __name__ == '__main__':
    resolve()
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 569 ms):
import numpy as np
N, K= list(map(int, input().split()))
A=[int(i) for i in input().split()]
F=[int(i) for i in input().split()]

A=np.array(sorted(A, reverse=True))
F=np.array(sorted(F))

lower_bound=0
upper_bound=np.max(A*F)
K_tmp=0

while upper_bound>=lower_bound:
    mid=(lower_bound+upper_bound)//2
    K_tmp=np.sum(np.maximum(A-mid//F, 0))
    if K_tmp<=K:
        upper_bound=mid-1
    else:
        lower_bound=mid+1

print(lower_bound)

Optimized program (CPU time: 344 ms):
n,k=list(map(int,input().split()))
*a,= list(map(int,input().split()))
*f,= list(map(int,input().split()))
a=sorted(a)
f=sorted(f, reverse=True)

def is_ok(arg):
    cnt=0
    for i in range(n):
        cnt+=max(a[i]-arg//f[i], 0)
    return cnt<=k

def meguru_bisect(ng, ok):
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if is_ok(mid):
            ok = mid
        else:
            ng = mid
    return ok

print((meguru_bisect(-1, 10**12)))
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 1362 ms):
import numpy as np
S = list((input()))
S_len = len(S)
num_lst = np.zeros(S_len + 1)
for i in range(S_len):
    if S[i] == "<":
        num_lst[i + 1] = max(num_lst[i] + 1, num_lst[i + 1])


for i in range(S_len - 1, -1 , -1):
    if S[i] == ">":
        num_lst[i] = max(num_lst[i], num_lst[i + 1] + 1)
print((int(np.sum(num_lst))))

Optimized program (CPU time: 407 ms):
S = list((input()))
numList = [0] * (len(S) + 1)
for i in range(len(S)):
    if S[i] == '<':
        numList[i + 1] = numList[i] + 1

for i in range(len(S) - 1 , -1 , -1):
    if S[i] == '>':
        numList[i] = max(numList[i + 1] + 1 ,numList[i])
print((sum(numList)))
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 1018 ms):
import time
t=time.time()
A,B,C=list(map(int,input().split()))
cnt=0
while(A%2==B%2==C%2==0):
    a,b,c=A,B,C
    A=b//2+c//2;B=a//2+c//2;C=a//2+b//2
    cnt+=1
    if(time.time()-t>=1):
        print((-1))
        exit()
print(cnt)

Optimized program (CPU time: 18 ms):
A,B,C=list(map(int,input().split()))
cnt=0
if(A==B==C):
    if A%2!=0:
        print((0))
    else:
        print((-1))
    exit()
while(A%2==B%2==C%2==0):
    a,b,c=A,B,C
    A=b//2+c//2;B=a//2+c//2;C=a//2+b//2
    cnt+=1
print(cnt)
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 170 ms):
import numpy as np

def ABC133C(l, r):
    Min = np.inf
    for i in range(l, r):
        for j in range(i+1, r+1):
            a = (i * j) % 2019
            if a < Min:
                Min = a
                if a == 0: # ここに注目
                    print(Min)
                    return
    print(Min)

l, r = list(map(int, input().split()))
ABC133C(l, r)


Optimized program (CPU time: 37 ms):
def ABC133C(l, r):
    Min = 1e10
    for i in range(l, r):
        for j in range(i+1, r+1):
            a = (i * j) % 2019
            if a < Min:
                Min = a
                if a == 0: # ここに注目
                    print(Min)
                    return
    print(Min)

l, r = list(map(int, input().split()))
ABC133C(l, r)
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 153 ms):
import numpy as np

temp=list(map(int, input().split()))

N=temp[0]
X=temp[1]

L=list(map(int, input().split()))

ind=1
while np.sum(L[:ind])<=X and ind<=N:
  ind+=1
  
print(ind)

Optimized program (CPU time: 18 ms):
N, X=list(map(int, input().split()))
L=list(map(int, input().split()))
L=[0]+L
 
l=int(0)
counter=int(0)
while l+L[counter]<=X and counter<=N:
  l+=L[counter]
  counter+=int(1)
  if counter==N+1:
    break
  
print(counter)
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 150 ms):
import copy
import numpy as np

S = (input())

N = int(S)

S = (input())

P = list(map(int, S.split()))

check = []

count = 0

for i in range(0, N-2, 1):

    check = P[i:i+3]
    check = np.array(check)
    check = check.argsort()
    if check[1] == 1:
        count = count + 1

print(count)


Optimized program (CPU time: 18 ms):
n = int((input()))
P = list(map(int, input().split()))
count = 0
for i in range(n-2):

    P_temp = [P[i], P[i+1], P[i+2]]
    P_temp.sort()
    if P_temp[1] == P[i+1]:
        count = count + 1

print(count)
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 207 ms):
a, b, c, x, y = list(map(int, input().split()))

ans = float('inf')

for i in range(2 * 10**5 + 1):
    ans = min(ans, a * max(x - i, 0) + b * max(y - i, 0) + 2 * c * i)

print(ans)

Optimized program (CPU time: 20 ms):
A,B,C,X,Y = list(map(int,input().split()))
min_xy = min(X,Y)
max_xy = max(X,Y)
ans1 = 2 * C * min_xy + A * (X-min_xy) + B * (Y-min_xy) # 無駄にならない範囲でABピザを買い、残りを買う
ans2 = A*X + B*Y # ABピザを買わない
ans3 = 2 * C * max_xy # ABピザだけを買う
print((min(ans1,ans2,ans3)))
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 381 ms):
import numpy as np
from copy import deepcopy

N = int(eval(input()))
A = []
A.append(list(map(int, input().split())))
a = deepcopy(A[0])
a.sort()
A.append(a)
A_t = np.array(A).T.tolist()
A_t.sort()

out = []
for i in range(N):
    out.append(str(A_t[i][1]))
print((" ".join(out)))


Optimized program (CPU time: 79 ms):
N = int(eval(input()))
A = list(map(int, input().split()))

rev = [""] * N
for i in range(N):
    rev[A[i]-1] = str(i+1)
print((" ".join(rev)))
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 149 ms):
import numpy as np
N,M=list(map(int, input().split()))
human=[]
for i in range(N):
  a,b=list(map(int, input().split()))
  human.append((a,b))
C=[]
for i in range(M):
  c,d=list(map(int, input().split()))
  C.append((c,d))
  
for h in human:
  a,b=h
  now=0
  mini=np.inf
  for i,ch in enumerate(C):
    c,d=ch
    L=abs(a-c)+abs(b-d)
    if mini>L:
      mini=L
      now=i+1
  print(now)

Optimized program (CPU time: 19 ms):
N,M=list(map(int, input().split()))
S=[]
for i in range(N):
    a,b=list(map(int, input().split()))
    S.append((a,b))

C=[]
for i in range(M):
    c,d=list(map(int, input().split()))
    C.append((c,d,i+1))
#print(C)
for s in S:
    a,b=s
    now=0
    dis=10**9

    for t in C:
        c,d,n=t
        D=abs(c-a)+abs(d-b)
        
        if D<dis:
            dis=D
            now=n
    print(now)
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 341 ms):
from scipy.spatial import distance


def solve(string):
    n, d, *x = list(map(int, string.split()))
    x = [tuple(c) for c in zip(*[iter(x)] * d)]
    dist = distance.cdist(x, x)
    return str(((dist == dist.astype("int32")).sum() - n) // 2)


if __name__ == '__main__':
    n, m = list(map(int, input().split()))
    print((solve('{} {}\n'.format(n, m)+'\n'.join([(input()) for _ in range(n)]))))


Optimized program (CPU time: 18 ms):
import math
N,D = list(map(int,input().split()))
X = [list(map(float, input().split())) for i in range(N)]
counter = 0
for i, x0 in enumerate(X): 
    for x1 in X[i+1:]:
        if math.sqrt(sum([(a-b)**2 for a, b in zip(x0,x1)])).is_integer():
            counter+=1
print(counter)
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 790 ms):
import numpy as np
import heapq

N = int(eval(input()))
A =  np.sort([int(x) for x in input().split(" ")])[::-1]

q = [(-A[1], (0, 1)), (-A[1], (0, 1))]
confort = A[0]
heapq.heapify(q)

i = 2
while N > i:
    m = heapq.heappop(q)
    # print(f"{m[1][0]}と{m[1][1]}の間に割り込む. 気持ち良さ {-m[0]}")
    confort -= m[0]
    heapq.heappush(q, (-A[i], (i, m[1][0])))
    heapq.heappush(q, (-A[i], (i, m[1][1])))
    i += 1
print(confort)


Optimized program (CPU time: 143 ms):
N = int(eval(input()))
A = sorted([int(x) for x in input().split(" ")])[::-1]

N -= 2
S = A[0]

i = 1
while 1:
    if N == 0:
        break 
    if N == 1:
        S += A[i]
        break
    S += 2 * A[i]
    N -= 2
    i += 1
print(S)
Task: rewrite the given python program to improve its speed of execution.

Original program (CPU time: 1475 ms):
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = np.array(lr())
A = A[A%2==0].tolist()
bool = True
for a in A:
    if a%3 != 0 and a%5 != 0:
        bool = False

print(('APPROVED' if bool else 'DENIED'))


Optimized program (CPU time: 17 ms):
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = lr()
bl = all(x % 3 == 0 or x % 5 == 0 for x in A if x % 2 == 0)
print(('APPROVED' if bl else 'DENIED'))