Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
lenS = len(S)
ans = 0
flag = True

while flag is True:
    i = S.find('ABC')
    if i == -1:
      falg = False
      break
      
    num_l_A = 0
    if i>=1:
      for ia in range(i-1,-1,-1):
        if S[ia] == 'A':
          num_l_A += 1
        else:break
    num_r_BC = 0
    if i+3 < lenS:
      for ibc in range(i+3,lenS):
        if S[ibc:ibc+1] == 'BC': num_r_BC += 1
        else:break
    ans += 1 + num_l_A + num_r_BC
    S = S[:i-num_l_A] + 'BC'*(1+num_r_BC) + 'A'*(1+num_l_A) + S[i+num_r_BC*2+3:]
print(ans)


Fixed submission:
def sol(S):
  N = len(S)
  nums = [0 for _ in range(N+1)]
  i = N-1
  while i >= 0:
    if S[i-1:i+1] == 'BC':
      nums[i-1] = nums[i+1] + 1
      nums[i] = nums[i+1] + 1
      i -= 2
    else:
      nums[i] = nums[i+1]
      i -= 1
  ans = 0
  for i, s in enumerate(S):
    if s == 'A':
      ans += nums[i]
  return ans

S = input()
ans = 0
N = len(S)
si, i = 0, 0
while i < N:
  if S[i] == 'A':
    i += 1
  elif S[i:i+2] == 'BC':
    i += 2
  else:
    ans += sol(S[si:i+1])
    si = i + 1
    i += 1
ans += sol(S[si:])
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
ids = [0 for _ in range(n)]
for i in range(m):
  l, r = map(int, input().split())
  for j in range(l-1, r):
    ids[j] += 1

ans = 0
for tmp_id in ids:
  if tmp_id == m:
    ans += 1
print(ans)  

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

l_tmp = 0
r_tmp = n+1
for i in range(m):
  l, r = map(int, input().split())
  l_tmp = l if l > l_tmp else l_tmp
  r_tmp = r if r < r_tmp else r_tmp

if r_tmp-l_tmp >= 0:
  print(r_tmp-l_tmp+1)
else:
  print(0)
Buggy python submission (verdict: Time Limit Exceeded):
R,G,B,N = map(int,input().split())
ans = 0
for ri in range(N//R+1):
  if ri*R > N: continue
  for gi in range(N//G+1):
    if gi*G > N: continue
    for bi in range(N//B+1):
      if bi*B > N: continue
      if ri*R+gi*G+bi*B==N:ans += 1
print(ans)

Fixed submission:
R,G,B,N = map(int,input().split())
ans = 0
for ri in range(N//R+1):
  for gi in range(N//G+1):
    if (N-ri*R-gi*G)>=0 and (N-ri*R-gi*G)%B==0:ans += 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
R,G,B,N = map(int,input().split())
ans = 0
for ri in range(N//R+1):
  for gi in range(N//G+1):
    for bi in range(N//B+1):
      if ri*R+gi*G+bi*B==N:ans += 1
print(ans)

Fixed submission:
R,G,B,N = map(int,input().split())
ans = 0
for ri in range(N//R+1):
  for gi in range(N//G+1):
    if (N-ri*R-gi*G)>=0 and (N-ri*R-gi*G)%B==0:ans += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd
from functools import reduce

N = int(input())
A = list(map(int,input().split()))
ans = -float('inf')
for i in range(N):
  tmpA = A[i]
  A[i] = A[i+1] if i != N-1 else A[i-1]
  ans = max(ans, reduce(gcd,A))
  A[i] = tmpA
print(ans)

Fixed submission:
from fractions import gcd
from functools import reduce

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

gcds = [-1] * N
gcds_rev = [-1] * N
for i in range(0,N):
  if i == 0: gcds[i] = A[0]
  else: gcds[i] = gcd(gcds[i-1], A[i])

  rev_i = N-1-i
  if rev_i == N-1: gcds_rev[rev_i] = A[-1]
  else: gcds_rev[rev_i] = gcd(gcds_rev[rev_i+1], A[rev_i])

ans = -float('inf')
for i in range(N):
  if i == 0:val = gcds_rev[1]
  elif i == N-1:val = gcds[-2]
  else: val = gcd(gcds[i-1], gcds_rev[i+1])
  ans = max(ans, val)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)

def dp(i,is_reverse):
  if i == 0:
    if is_reverse == 0: return 0
    else: return -float('inf')
  else:
    if is_reverse == 0: return max(dp(i-1,0)+A[i-1],dp(i-1,1)-A[i-1])
    else: return max(dp(i-1,0)-A[i-1],dp(i-1,1)+A[i-1])

N = int(input())
A = list(map(int,input().split()))
print(dp(N,0))

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
neg_num = 0
min_abs_a = float('inf')
sum_abs_A = 0
include_zero = False
for a in A:
  sum_abs_A += abs(a)
  min_abs_a = min(abs(a),min_abs_a)
  if a == 0: include_zero = True
  if a < 0 : neg_num += 1
print(sum_abs_A if neg_num%2==0 or include_zero is True else sum_abs_A - 2*min_abs_a)
Buggy python submission (verdict: Runtime Error):
N = int(input())
ans = []
for i in range(1,N+1):
  for j in range(i+1,N+1):
    ans.append(sorted([i,j]))

if N%2==1:
  for i in range(1, N//2+1):
    del ans[ans.index(sorted([i, N-i]))]
else:
  for i in range(1, N//2+1):
    del ans[ans.index(sorted([i, N-i]))]

print(len(ans))
[print(*a) for a in ans]

Fixed submission:
N = int(input())
ans = []
for i in range(1,N+1):
  for j in range(i+1,N+1):
    ans.append(sorted([i,j]))

if N%2==1:
  for i in range(1, N//2+1):
    del ans[ans.index(sorted([i, N-i]))]
else:
  for i in range(1, N//2+1):
    del ans[ans.index(sorted([i, N-i+1]))]

print(len(ans))
[print(*a) for a in ans]
Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations
import bisect
import copy

def move(positions, tmp_X, N, tmp_ans):
  min_dif = float('inf')
  min_idx = None
  min_x = None
  for x in tmp_X:
    idx = bisect.bisect_left(positions,x)
    if idx == N:
      tmp_dif = abs(x-positions[idx-1])
      tmp_idx = idx -1
    elif idx == 0:
      tmp_dif = abs(x-positions[idx])
      tmp_idx = idx
    else:
      dif1, dif2 = abs(x-positions[idx-1]), abs(x-positions[idx])
      if dif1 >= dif2:
        tmp_dif = dif2
        tmp_idx = idx
      else:
        tmp_dif = dif1
        tmp_idx = idx - 1

    if min_dif > tmp_dif:
      min_dif = tmp_dif
      min_idx = tmp_idx
      min_x = x
  
  del positions[min_idx]
  idx = bisect.bisect_left(positions, min_x)
  positions.insert(idx, min_x)

  tmp_X.remove(min_x)
  return tmp_ans + min_dif

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

if N >= M:
  print(0)
  exit()

ans = float('inf')
for cmb in combinations(X,N):
  tmp_ans = 0
  tmp_X = copy.deepcopy(X)
  positions = sorted(list(cmb))
  for position in positions: tmp_X.remove(position)
  
  while len(tmp_X) > 0:
    tmp_ans = move(positions, tmp_X, N, tmp_ans)
  ans = min(tmp_ans, ans)
print(ans)

Fixed submission:
N,M = map(int,input().split())
if M == 1:
  print(0)
  exit()

X = sorted(list(map(int,input().split())))
difX = [0] + [X[i+1]-X[i] for i in range(M-1)]
difX.sort(reverse=True)
print(sum(difX)-sum(difX[:N-1]))

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations
import bisect
import copy

def move(positions, tmp_X, N, tmp_ans):
  min_dif = float('inf')
  min_idx = None
  min_x = None
  for x in tmp_X:
    idx = bisect.bisect_left(positions,x)
    if idx == N:
      tmp_dif = abs(x-positions[idx-1])
      tmp_idx = idx -1
    elif idx == 0:
      tmp_dif = abs(x-positions[idx])
      tmp_idx = idx
    else:
      dif1, dif2 = abs(x-positions[idx-1]), abs(x-positions[idx])
      if dif1 >= dif2:
        tmp_dif = dif2
        tmp_idx = idx
      else:
        tmp_dif = dif1
        tmp_idx = idx - 1

    if min_dif > tmp_dif:
      min_dif = tmp_dif
      min_idx = tmp_idx
      min_x = x
  
  positions[min_idx] = min_x
  positions.sort()
  tmp_X.remove(min_x)
  return tmp_ans + min_dif

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

if N >= M:
  print(0)
  exit()

ans = float('inf')
for cmb in combinations(X,N):
  tmp_ans = 0
  tmp_X = copy.deepcopy(X)
  positions = sorted(list(cmb))
  for position in positions: tmp_X.remove(position)
  
  while len(tmp_X) > 0:
    tmp_ans = move(positions, tmp_X, N, tmp_ans)
  ans = min(tmp_ans, ans)
print(ans)

Fixed submission:
N,M = map(int,input().split())
if M == 1:
  print(0)
  exit()

X = sorted(list(map(int,input().split())))
difX = [0] + [X[i+1]-X[i] for i in range(M-1)]
difX.sort(reverse=True)
print(sum(difX)-sum(difX[:N-1]))
Buggy python submission (verdict: Time Limit Exceeded):
import heapq

N = int(input())
A, B = [], []
for i in range(N):
  a,b = map(int,input().split())
  heapq.heappush(A,[-a,-b,i])
  heapq.heappush(B,[-b,-a,i])

flags = [1 for _ in range(N)]
Asum, Bsum = 0, 0
ans = 0
turn = 1
while sum(flags)>0:
  if turn == 1:
    while len(A)>0:
      neg_a, _, ii = heapq.heappop(A)
      if flags[ii] == 1:
        flags[ii] = 0
        break
    Asum += -neg_a
  else:
    while len(B)>0:
      neg_b, _, ii = heapq.heappop(B)
      if flags[ii] == 1:
        flags[ii] = 0
        break
    Bsum += -neg_b
  turn *= -1
print(Asum-Bsum)

Fixed submission:
N = int(input())
AB = []
ans = 0
for _ in range(N):
  a,b = map(int,input().split())
  AB.append(a+b)
  ans -= b
AB.sort(reverse=True)

print(ans+sum(AB[::2]))

Buggy python submission (verdict: Runtime Error):
import heapq

N = int(input())
A, B = [], []
for i in range(N):
  a,b = map(int,input().split())
  heapq.heappush(A,[-a,i])
  heapq.heappush(B,[-b,i])

flags = [1 for _ in range(N)]
Asum, Bsum = 0, 0
ans = 0
for i in range(N):
  if i%2==0:
    flag = 0
    while flag == 0:
      neg_a, ii = heapq.heappop(A)
      flag = flags[ii]
      flags[ii] = 0
    Asum += -neg_a
  else:
    neg_b, ii = heapq.heappop(B)
    flag = 0
    while flag == 0:
      neg_b, ii = heapq.heappop(B)
      flag = flags[ii]
      flags[ii] = 0
    Bsum += -neg_b  
print(Asum-Bsum)

Fixed submission:
N = int(input())
AB = []
ans = 0
for _ in range(N):
  a,b = map(int,input().split())
  AB.append(a+b)
  ans -= b
AB.sort(reverse=True)

print(ans+sum(AB[::2]))

Buggy python submission (verdict: Runtime Error):
import heapq

N = int(input())
A, B = [], []
for i in range(N):
  a,b = map(int,input().split())
  heapq.heappush(A,[-a,i])
  heapq.heappush(B,[-b,i])

flags = [1 for _ in range(N)]
Asum, Bsum = 0, 0
ans = 0
for i in range(N):
  if i%2==0:
    flag = 0
    while flag == 0:
      neg_a, i = heapq.heappop(A)
      flag = flags[i]
      flags[i] = 0
    Asum += -neg_a
  else:
    neg_b, i = heapq.heappop(B)
    flag = 0
    while flag == 0:
      neg_b, i = heapq.heappop(B)
      flag = flags[i]
      flags[i] = 0
    Bsum += -neg_b  
print(Asum-Bsum)

Fixed submission:
N = int(input())
AB = []
ans = 0
for _ in range(N):
  a,b = map(int,input().split())
  AB.append(a+b)
  ans -= b
AB.sort(reverse=True)

print(ans+sum(AB[::2]))
Buggy python submission (verdict: Time Limit Exceeded):
N,W = map(int,input().split())
WV = [[0,0]] + [list(map(int,input().split())) for _ in range(N)]
vrng = (10**3)*N+1
dp = [[float('inf') for _ in range(vrng)] for _ in range(N+1)]
dp[0][0] = 0
for i in range(1, N+1):
  tmpw, tmpv = WV[i]
  for j in range(vrng):
    if j-tmpv>=0:
      dp[i][j] = min(dp[i-1][j],dp[i-1][j-tmpv]+tmpw)
    else:
      dp[i][j] = dp[i-1][j]

ans = -float('inf')
for row in dp:
  for j,w in enumerate(row):
    if w<=W and ans<j:
      ans = j
print(ans)

Fixed submission:
N,W = map(int,input().split())
WV = [[0,0]] + [list(map(int,input().split())) for _ in range(N)]

sumV = sum([v for _,v in WV])
dp = [[float('inf') for _ in range(sumV+1)] for _ in range(N+1)]
dp[0][0] = 0
for i in range(1, N+1):
  tmpw, tmpv = WV[i]
  for j in range(sumV+1):
    if j-tmpv>=0:
      dp[i][j] = min(dp[i-1][j],dp[i-1][j-tmpv]+tmpw)
    else:
      dp[i][j] = dp[i-1][j]

ans = -float('inf')
for row in dp:
  for j,w in enumerate(row):
    if w<=W and ans<j:
      ans = j
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
level0 = 'BPPPB'
N, X = map(int, input().split())

dp = [[0 for _ in range(X)] for _ in range(N)]
layers_num = [0 for _ in range(N)]

layers_num[0] = len(level0)

for x in range(X):
    if x < len(level0):
        pb = level0[x]
        if x == 0:
            if pb == 'P':
                dp[0][x] = 1
        else:
            if pb == 'P':
                dp[0][x] = dp[0][x-1] + 1
            else:
                dp[0][x] = dp[0][x-1]
    else:
        dp[0][x] = dp[0][x - 1]

for n in range(1, N):
    layers_num[n] = 2 * layers_num[n-1] + 3

    for x in range(X):
        if x == 0:
            dp[n][x] = 0
        elif 0 < x and x <= layers_num[n - 1]:
            dp[n][x] = dp[n - 1][x - 1]
        elif x == layers_num[n-1] + 1:
            dp[n][x] = dp[n - 1][x - 1] + 1
        else:
            dp[n][x] = dp[n - 1][x - 1] + 1 + dp[n - 1][x - layers_num[n-1] - 2]

print(dp[-1][-1])


Fixed submission:
def f(n, x):
    if n == 0:
        return min(x, 3)
    elif x == 0:
        return 0
    elif 0 < x and x <= layers_num[n - 1]:
        return f(n - 1, x - 1)
    elif x == 1 + layers_num[n - 1]:
        return p[n - 1] + 1
    elif 1 + layers_num[n - 1] < x <= 1 + 2 * layers_num[n - 1]:
        return p[n - 1] + 1 + f(n - 1, x - 2 - layers_num[n - 1])
    else:
        return 2 * p[n - 1] + 1


level0 = 'BPPPB'
N, X = map(int, input().split())

layers_num = []
p = []
for n in range(N):
    if n == 0:
        layers_num.append(len(level0))
        p.append(level0.count('P'))
    else:
        layers_num.append(layers_num[n - 1] * 2 + 3)
        p.append(2 * p[n - 1] + 1)

print(f(N - 1, X - 1))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

def burger(n):
  if n == 0:
    return '1'
  else:
    return '0' + burger(n-1) + '1' + burger(n-1) + '0'

N,X = map(int,input().split())
print(Counter(burger(N)[:X])['1'])


Fixed submission:
def f(n, x):
    if n == 0:
        return min(x, 3)
    elif x == 0:
        return 0
    elif 0 < x and x <= layers_num[n - 1]:
        return f(n - 1, x - 1)
    elif x == 1 + layers_num[n - 1]:
        return p[n - 1] + 1
    elif 1 + layers_num[n - 1] < x <= 1 + 2 * layers_num[n - 1]:
        return p[n - 1] + 1 + f(n - 1, x - 2 - layers_num[n - 1])
    else:
        return 2 * p[n - 1] + 1


level0 = 'BPPPB'
N, X = map(int, input().split())

layers_num = []
p = []
for n in range(N):
    if n == 0:
        layers_num.append(len(level0))
        p.append(level0.count('P'))
    else:
        layers_num.append(layers_num[n - 1] * 2 + 3)
        p.append(2 * p[n - 1] + 1)

print(f(N - 1, X - 1))
Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
from functools import reduce
from operator import mul
from math import factorial

def make_divisors(n):
    divs = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divs.append(i)
            if i != n // i:
                divs.append(n//i)
    # divisors.sort()
    return divs

def prime_factorize(n):
  factors = {}
  for i in range(2,n+1):
    while n%i ==0:
      n /= i
      if i not in factors:
        factors[i] = 1
      else:
        factors[i] += 1
  return factors

N = int(input())
ans = 0
for div in make_divisors(factorial(N)):
  res = prime_factorize(div)
  if len(res) > 0:
    div_num = reduce(mul, [num+1 for num in res.values()])
    if div_num == 75: ans += 1
print(ans)

Fixed submission:
from collections import defaultdict
from collections import Counter


def prime_factorization(divs, n):
    while n % 2 == 0:
        divs[2] += 1
        n //= 2

    i = 3
    while i <= int(n ** .5) + 1:
        while n % i == 0:
            divs[i] += 1
            n //= i
        i += 2
    if n != 1:
        divs[n] += 1

    return divs


def num(m):
    return sum([value if key >= m else 0 for key, value in cnt_divs.items()])


N = int(input())
divs = defaultdict(int)
for n in range(2, N + 1):
    divs = prime_factorization(divs, n)

cnt_divs = Counter(divs.values())

print(num(74) + \
      num(24) * (num(2) - 1) + \
      num(14) * (num(4) - 1) + \
      num(4) * (num(4) - 1) * (num(2) - 2) // 2)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
from functools import reduce
from operator import mul
from math import factorial

def make_divisors(n):
    divs = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divs.append(i)
            if i != n // i:
                divs.append(n//i)
    # divisors.sort()
    return divs

def prime_factorize(n):
  divs = []
  for i in range(2,n+1):
    while n%i ==0:
      n /= i
      divs.append(i)
  return divs

N = int(input())
ans = 0
for div in make_divisors(factorial(N)):
  res = prime_factorize(div)
  if len(res) > 0:
    div_num = reduce(mul, [num+1 for num in Counter(res).values()])
    if div_num == 75: ans += 1
print(ans)

Fixed submission:
from collections import defaultdict
from collections import Counter


def prime_factorization(divs, n):
    while n % 2 == 0:
        divs[2] += 1
        n //= 2

    i = 3
    while i <= int(n ** .5) + 1:
        while n % i == 0:
            divs[i] += 1
            n //= i
        i += 2
    if n != 1:
        divs[n] += 1

    return divs


def num(m):
    return sum([value if key >= m else 0 for key, value in cnt_divs.items()])


N = int(input())
divs = defaultdict(int)
for n in range(2, N + 1):
    divs = prime_factorization(divs, n)

cnt_divs = Counter(divs.values())

print(num(74) + \
      num(24) * (num(2) - 1) + \
      num(14) * (num(4) - 1) + \
      num(4) * (num(4) - 1) * (num(2) - 2) // 2)
Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import collections

n, m = map(int, input().split())
py_dict = collections.defaultdict(list)
cityi_by_year = []
for i in range(m):
  p, y = map(int, input().split())
  py_dict[p].insert(bisect.bisect_left(py_dict[p],y),y)
  cityi_by_year.append([p,y])
  
for p, y in cityi_by_year:
  idx = bisect.bisect_left(py_dict[p],y)
  print('0'*(6-len(str(p))) + str(p) + '0'*(6-len(str(idx+1))) + str(idx+1))


Fixed submission:
import collections
import bisect
n,m=map(int,input().split())
p=[[int(j) for j in input().split()] for i in range(m)]
a=collections.defaultdict(list)
for x,y in sorted(p):
    a[x]+=[y]
for x,y in p:
    z=bisect.bisect(a[x],y)
    print("%06d%06d"%(x,z))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect

n, m = map(int, input().split())
py_dict = {}
cityi_by_year = []
for i in range(m):
  p, y = map(int, input().split())
  if p not in py_dict:py_dict[p] = [y]
  else:py_dict[p].insert(bisect.bisect_left(py_dict[p],y),y)
  cityi_by_year.append([p,y])
  
for p, y in cityi_by_year:
  idx = py_dict[p].index(y)
  print('0'*(6-len(str(p))) + str(p) + '0'*(6-len(str(idx+1))) + str(idx+1))


Fixed submission:
import collections
import bisect
n,m=map(int,input().split())
p=[[int(j) for j in input().split()] for i in range(m)]
a=collections.defaultdict(list)
for x,y in sorted(p):
    a[x]+=[y]
for x,y in p:
    z=bisect.bisect(a[x],y)
    print("%06d%06d"%(x,z))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect

n, m = map(int, input().split())
py_dict = {}
cityi_by_year = []
for i in range(m):
  p, y = map(int, input().split())
  if p not in py_dict:py_dict[p] = [y]
  else:py_dict[p].insert(bisect.bisect_left(py_dict[p],y),y)
  cityi_by_year.append([p,y])
  
for p, y in cityi_by_year:
  idx = bisect.bisect_left(py_dict[p],y)
  print('0'*(6-len(str(p))) + str(p) + '0'*(6-len(str(idx+1))) + str(idx+1))


Fixed submission:
import collections
import bisect
n,m=map(int,input().split())
p=[[int(j) for j in input().split()] for i in range(m)]
a=collections.defaultdict(list)
for x,y in sorted(p):
    a[x]+=[y]
for x,y in p:
    z=bisect.bisect(a[x],y)
    print("%06d%06d"%(x,z))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
py_dict = {}
cityi_by_year = []
for i in range(m):
  p, y = map(int, input().split())
  if p not in py_dict:py_dict[p] = [y]
  else:py_dict[p].append(y)
  cityi_by_year.append(y)

ans = [-1 for _ in range(m)]
for p in sorted(py_dict.keys()):
  p_str = '0'*(6-len(str(p))) + str(p)
  cnt = 0
  for y in sorted(py_dict[p]):
    cnt += 1
    ans[cityi_by_year.index(y)] = p_str + '0'*(6-len(str(cnt))) + str(cnt)
[print(id) for id in ans]

Fixed submission:
import collections
import bisect
n,m=map(int,input().split())
p=[[int(j) for j in input().split()] for i in range(m)]
a=collections.defaultdict(list)
for x,y in sorted(p):
    a[x]+=[y]
for x,y in p:
    z=bisect.bisect(a[x],y)
    print("%06d%06d"%(x,z))
Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd
from functools import reduce
from itertools import combinations

N,M = map(int,input().split())
alist = []
for cmb in combinations([i for i in range(1,M)],N-1):
  cmb = [0] + list(cmb) + [M]
  tmp_a = sorted([cmb[i]-cmb[i-1] for i in range(1,N+1)])
  if tmp_a not in alist: alist.append(tmp_a)

ans = -float('inf')
for a in alist:
  ans = max(ans, reduce(gcd,a))
print(ans)

Fixed submission:
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    return divisors

ans = -float('inf')
N,M = map(int,input().split())
for div in make_divisors(M):
  if M//div >= N: ans = max(ans, div)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations_with_replacement

N,K = map(int,input().split())
ans = 0
for cmb in combinations_with_replacement([i for i in range(1,N+1)],3):
  if all([True if a%K ==0 else False for a in [cmb[0]+cmb[1], cmb[1]+cmb[2],cmb[2]+cmb[0]]]):
    set_num = len(set(cmb))
    if set_num == 1: ans += 1
    elif set_num == 2: ans += 3
    elif set_num == 3: ans += 6
  #print(ans, cmb)
print(ans)

Fixed submission:
from itertools import combinations_with_replacement

def calc_cases(num):
  cases = num
  if num == 2: cases += num*(num-1)*3
  elif num >= 3: cases += num*(num-1)*3 + num*(num-1)*(num-2)
  return int(cases)

N,K = map(int,input().split())
num, num2 = 0, 0
if K%2 ==1:
  for i in range(1,N+1):
    if i%K == 0: num +=1
else:
  for i in range(1,N+1):
    if i%K == 0:
      num +=1
    elif i%K == K//2:
      num2 += 1

print(calc_cases(num)+calc_cases(num2))
#print(num, calc_cases(num), num2, calc_cases(num2))
Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations

N,K=map(int,input().split())
X = list(map(int, input().split()))

print(min([min(abs(cmb[0]) + abs(cmb[-1]-cmb[0]), abs(cmb[-1]) + abs(cmb[0]-cmb[-1])) for cmb in combinations(X,K)]))

Fixed submission:
N,K=map(int,input().split())
X = list(map(int, input().split()))

print(min([min(abs(X[i]) + abs(X[i+K-1]-X[i]), abs(X[i+K-1]) + abs(X[i]-X[i+K-1])) for i in range(N-K+1)]))

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations

N,K=map(int,input().split())
X = list(map(int, input().split()))

ans = float('inf')
for cmb in combinations(X,K):
  tmp_ans = min(abs(cmb[0]) + abs(cmb[-1]-cmb[0]), abs(cmb[-1]) + abs(cmb[0]-cmb[-1]))
  if ans > tmp_ans: ans = tmp_ans
print(ans)


Fixed submission:
N,K=map(int,input().split())
X = list(map(int, input().split()))

print(min([min(abs(X[i]) + abs(X[i+K-1]-X[i]), abs(X[i+K-1]) + abs(X[i]-X[i+K-1])) for i in range(N-K+1)]))

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import permutations

N,K=map(int,input().split())
X = list(map(int, input().split()))

ans = float('inf')
for permutation in permutations(X,K):
  tmp_ans = 0
  prv_pos = 0
  for pos in permutation:
    tmp_ans += abs(pos-prv_pos)
    prv_pos = pos
  if ans > tmp_ans:ans = tmp_ans
print(ans)

Fixed submission:
N,K=map(int,input().split())
X = list(map(int, input().split()))

print(min([min(abs(X[i]) + abs(X[i+K-1]-X[i]), abs(X[i+K-1]) + abs(X[i]-X[i+K-1])) for i in range(N-K+1)]))