Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p02608 (python)

Fixed submission:
import math
N = int(input())
ans_list = [0 for i in range(N)]
index = int(math.sqrt(N))

for x in range(1,index):
  for y in range(1,index):
    for z in range(1,index):
      n = x**2 + y**2 + z**2 + x*y + y*z + z*x 
      if (n <= N):
        ans_list[n-1] += 1
for i in range(N):
  print(ans_list[i])

Buggy submission (verdict: Runtime Error):
N = int(input())
X = int(input(),2)

def pcrem(n,cnt):
  if (n==0):
    print(cnt)
    return 0
  pc = bin(n).count("1")
  pcrem(n%pc,cnt+1)

for i in range(N):
  f = X^(1<<N-i-1)
  pcrem(f,0)
Problem p02660 (python)

Fixed submission:
import collections
import bisect
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

N = int(input())
c = collections.Counter(prime_factorize(N))
sums = []
s=1
ans = 0
for v in c.values():
  count = 0
  for i in range(1,v+1):
    if count+i > v:
      break
    else:
      count += i
      ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
import collections
import bisect
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

N = int(input())
c = collections.Counter(prime_factorize(N))
sums = []
s=1
ans = 0
if c.values():
  while s <= max(c.values()):
    sums.append(s)
    s += s+1
  for v in c.values():
    idx = bisect.bisect_left(sums,v)
    
    if idx <= len(c.values()):
      if sums[idx] == v:
        ans += idx+1
      else:
        ans += idx
    else:
      ans += max(sums)
print(ans)
Problem p02981 (python)

Fixed submission:
N, A, B = map(int, input().split())
print(min([N*A,B]))

Buggy submission (verdict: Runtime Error):
N, A, B = map(int, innput().split())
print(min([N*A,B]))
Problem p03001 (python)

Fixed submission:
W, H, x, y = map(float, input().split())
if x == W/2 and y == H/2:
  chohuku = 1
else:
  chohuku = 0
print(str((H*W)/2) + " " + str(chohuku))

Buggy submission (verdict: Runtime Error):
W, H, x, y = map(float, input().split())
if x == W/2 & y == H/2:
  chohuku = 1
else:
  chohuku = 0
print(str((H*W)/2) + " " + str(chohuku))
Problem p03076 (python)

Fixed submission:
A = int(input())
B = int(input())
C = int(input())
D = int(input())
E = int(input())
 
inter_A = (A//10)*10 if A%10 == 0 else (A//10)*10 + 10
inter_B = (B//10)*10 if B%10 == 0 else (B//10)*10 + 10
inter_C = (C//10)*10 if C%10 == 0 else (C//10)*10 + 10
inter_D = (D//10)*10 if D%10 == 0 else (D//10)*10 + 10
inter_E = (E//10)*10 if E%10 == 0 else (E//10)*10 + 10
 
first_A = A + inter_B + inter_C + inter_D + inter_E
first_B = inter_A + B + inter_C + inter_D + inter_E
first_C = inter_A + inter_B + C + inter_D + inter_E
first_D = inter_A + inter_B + inter_C + D + inter_E
first_E = inter_A + inter_B + inter_C + inter_D + E
print(min([first_A, first_B, first_C, first_D, first_E]))

Buggy submission (verdict: Runtime Error):
A = int(input())
B = int(input())
C = int(input())
D = int(input())
E = int(input())

inter_A = (A//10)*10 + 10
inter_B = (B//10)*10 + 10
inter_C = (C//10)*10 + 10
inter_D = (D//10)*10 + 10
inter_E = (E//10)*10 + 10

first_A = A + inter_B + inter_C + inter_D + inter_E
first_B = inter_A + B + inter_C + inter_D + inter_E
first_C = inter_A + inter_B + C + inter_D + inter_E
first_D = inter_A + inter_B + inter_C + D + inter_E
first_E = inter_A + inter_B + inter_C + inter_D + E
print(min[first_A, first_B, first_C, first_D, first_E])
Problem p03087 (python)

Fixed submission:
import bisect

N, Q = map(int, input().split())
S = input()
question = []
for i in range(Q):
  question.append(list(map(int, input().split())))

# Sの中で、ACという部分文字列が含まれるときの（Aの）index
index = []
for i in range(N-1):
  if S[i]+S[i+1] == "AC":
    index.append(i)

# i番目の文字以降に存在するACという部分文字列の数
sum_index = [0]*N
sum_ac = 0
for i in range(N-1):
  if S[N-(i+2)]+S[N-(i+1)] == "AC":
    sum_ac += 1
  sum_index[N-(i+2)] = sum_ac

for i, q in enumerate(question):
  ans = sum_index[q[0]-1] - sum_index[q[1]-1]
  print(ans)

Buggy submission (verdict: Runtime Error):
S = input()
question = []
for i in range(Q):
  question.append(list(map(int, input().split())))

# Sの中で、ACという部分文字列が含まれるときの（Aの）index
index = []
for i in range(N-1):
  if S[i]+S[i+1] == "AC":
    index.append(i)

for i, q in enumerate(question):
  ans = 0
  for j in range(len(index)):
    if index[j] >= q[0]-1 and index[j]+1 <= q[1]-1:
      ans +=1
  print(ans)
Problem p03145 (python)

Fixed submission:
A, B, C = map(int, input().split())
print((A*B)//2)

Buggy submission (verdict: Runtime Error):
A, B, C = map(int, input().split())
return (A*B)//2
Problem p03160 (python)

Fixed submission:
N = int(input())
h = list(map(int,input().split()))
dp = [-1]*N

def rec_dp(i,dp):
    if i==0:
        return 0
    elif i==1:
        return abs(h[1]-h[0])
    else:
        onestepmin = dp[i-1] + abs(h[i-1] - h[i])
        twostepmin = dp[i-2] + abs(h[i-2] - h[i])
        return min([onestepmin,twostepmin])

for i in range(N):
    dp[i] = rec_dp(i,dp)

print(dp[N-1])

Buggy submission (verdict: Runtime Error):
N = int(input())
h = list(map(int,input().split()))
dp = [-1]*N

for i in range(N):
    dp[i] = rec_dp(i,dp)

def rec_dp(i,dp):
    if i==0:
        return 0
    elif i==1:
        return abs(h[1]-h[0])
    else:
        onestepmin = dp[i-1] + abs(h[i-1] - h[i])
        twostepmin = dp[i-2] + abs(h[i-2] - h[i])
        return min(onestepmin,twostepmin)


print(dp[N-1])
Problem p03219 (python)

Fixed submission:
X, Y = map(int,input().split())
print(X+Y//2)

Buggy submission (verdict: Runtime Error):
X, Y = int(input().split())
print(X+Y//2)
Problem p03238 (python)

Fixed submission:
N = int(input())


if N == 1:
    print('Hello World')
elif N == 2:
    A = int(input())
    B = int(input())
    print(A+B)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = int(input())
B = int(input())

if N == 1:
    print('Hello World')
elif N == 2:
    print(A+B)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = int(input())
B = int(input())

if N == 1:
    print('Hello World')
if N == 2:
    print(A+B)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = int(input())
B = int(input())

if N == 1:
    print('Hello World')
if N == 2:
    print(A+B)
Problem p03351 (python)

Fixed submission:
a,b,c,d = map(int,input().split())
if abs(b-a) <= d and abs(c-b) <= d:
	print('Yes')
elif abs(c-a)<= d:
	print('Yes')
else:
	print('No')

Buggy submission (verdict: Runtime Error):
a,b,c,d = map(int,input().split())
if (abs(b-a) <= d and abs(c-b) <= d) 
	print('Yes')
else if (abs(c-a)<= d)
	print('Yes')
else
	print('No')
Problem p03361 (python)

Fixed submission:
H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]
a = [[-1]*W for _ in range(H)]
for i in range(H):
  for j in range(W):
    if s[i][j] == ".":
      a[i][j] = 1
    dh = [-1,0,1,0]
    dw = [0,-1,0,1]
    for k in range(4):
      ii = i + dh[k]
      jj = j + dw[k]
      if 0 <= ii < H and 0 <= jj < W:
        if s[i][j] == "#" and s[ii][jj] == "#":
          a[i][j] = 1
          a[ii][jj] = 1
flag = True
for i in range(H):
  for j in range(W):
    if a[i][j] != 1:
      flag = False
      break
if flag:
  print("Yes")
else:
  print("No")

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]
a = [[-1]*W for _ in range(H)]
for i in range(H):
  for j in range(W):
    if s[i][j] == ".":
      a[i][j] = 1
    dh = [-1,0,1,0]
    dw = [0,-1,0,1]
    for k in range(4):
      ii = i + dh[k]
      jj = j + dw[k]
      if 0 <= ii < W and 0 <= jj < H:
        if s[i][j] == "#" and s[ii][jj] == "#":
          a[i][j] = 1
          a[ii][jj] = 1
flag = True
for i in range(H):
  for j in range(W):
    if a[i][j] != 1:
      flag = False
      break
if flag:
  print("Yes")
else:
  print("No")
Problem p03371 (python)

Fixed submission:
a,b,c,x,y = map(int,input().split())
if (a+b <= 2*c) :
  print(a*x+b*y)
if (a+b > 2*c) :
  if (x == y) :
    print(2*c*x)
  if (x > y) :
    print(min([2*c*y + a*(x-y),2*c*x]))
  if (x < y) :
    print(min([2*c*x + b*(y-x),2*c*y]))

Buggy submission (verdict: Runtime Error):
a,b,c,x,y = map(int,input().split())
if (a+b <= 2*c) :
  print(a*x+b*y)
if (a+b > 2*c) :
  if (x == y) :
    print(2*c*(x+y))
  if (x > y) :
    print(min(2*c*y + a*(x-y),2*c*(x+y))
  if (x < y) :
    print(min(2*c*x + b*(y-x),2*c*(x+y))
Problem p03393 (python)

Fixed submission:
# 写経#2284578
S = input()
alp = "abcdefghijklmnopqrstuvwxyz"
def h(string, least = 'Z'):
  for c in alp:
    if c not in string:
      if c <= least:
        continue
      return string + c
  if string == "":
    return "-1"
  return h(string[:-1], string[-1])
print(h(S))

Buggy submission (verdict: Runtime Error):
S = list(input())
ic = ""
for i in range(97, 123):
  if (chr(i) not in S):
    ic = chr(i)
    break
if ic != "":
  print("".join(S) + ic)
else:
  h = S[0]
  for i in range(1,len(S)):
    if h < S[i]:
      h = S[i]
    else:
      ii = i
      break
  print(ii)
Problem p03523 (python)

Fixed submission:
S = list(input())
for i in range(len(S)):
  if S[0] == "K":
    S.insert(0,"A")
for i in range(len(S)):
  if S[i] == "H" and S[i+1] != "A":
    S.insert(i+1,"A")
for i in range(len(S)):
  if S[i] == "B" and S[i+1] != "A":
    S.insert(i+1,"A")
for i in range(len(S)):
  if S[-1] == "R":
    S.append("A")

if "".join(S) == "AKIHABARA":
  print("YES")
else:
  print("NO")

Buggy submission (verdict: Runtime Error):
S = list(input())
for i in range(len(S)):
  if S[0] == "K":
    S.insert(0,"A")
for i in range(len(S)):
  if S[i] == "H" and S[i+1] != "A":
    S.insert(i+1,"A")
for i in range(len(S)):
  if S[i] == "B" and S[i+1] != "A":
    S.insert(i+1,"A")
for i in range(len(S)):
  if S[-1] == "R":
if "".join(S) == "AKIHABARA":
  print("YES")
else:
  print("NO")
Problem p02909 (python)

Fixed submission:
import sys

S = sys.stdin.readline().rstrip()

WS = ["Sunny", "Cloudy", "Rainy"]

i = WS.index(S)
i += 1
if i == 3:
    i = 0

print(WS[i])

Buggy submission (verdict: Runtime Error):
import sys

S = sys.stdin.readline().rstrip()

WS = ["Sunny", "Cloudy", "Rainy"]

i = WS.index(S)
i += 1
if i == 2:
    i = 0

print(WS[i])
Problem p02760 (python)

Fixed submission:
import math
A = list(map(int,"\n".join([input() for i in range(3)]).strip().split()))
N = int(input())
B = [int(input()) for i in range(N)]
size = 3
punched = set()

corrects = []
# 縦の揃い方
corrects += [{i + size*j for i in range(size)} for j in range(size)]
# 横の揃い方
corrects += [{j + size*i for i in range(size)} for j in range(size)]
# 斜めの揃い方
corrects += [{i + size*i for i in range(size)}]
corrects += [{(size - 1 - i) + size*i for i in range(size)}]

def punch(b, A, punched):
    for i in range(len(A)):
        if b == A[i]: punched.add(i)
          
for b in B:
    punch(b, A, punched)
    
for correct in corrects:
    if correct.issubset(punched):
        print("Yes")
        break
else:
  	print("No")

Buggy submission (verdict: Runtime Error):
import math

A = [int(a) for _ in range(3) for a in input().split()]
punched = [False] * 9
num_to_i = {a:i for i, a in enumerate(A)}
N = int(input())
B = [int(input()) for _ in range(N)]

for b in B:
  if b in num_to_i:
    punched[num_to_i[b]] = True

# 縦
for i in range(3):
  if punched[i] and punched[i+3] and punched[i+6]:
    print("Yes")
    quit()

# 横
for i in range(3):
  if punched[i*3] and punched[i*3+1] and punched[i*3+3]:
    print("Yes")
    quit()

# 斜め
if (punched[0] and punched[4] and punched[8]) or (punched[2] and punched[4] and punched[6]):
  print("Yes")
  quit()
  
print("No")
Problem p03200 (python)

Fixed submission:
S = input().strip()

w_total = 0
num_op = 0
for i,c in enumerate(S):
    if c == "W":
        w_total += 1
        num_op += i + 1 - w_total
        
print(num_op)

Buggy submission (verdict: Runtime Error):
#S = input().strip()
Sb = [c == "B" for c in S]

def reverse(Sb,i):
    _list = Sb.copy()
    _list[i] = not _list[i]
    return _list

def operate_onestep(Sb):
    _list = Sb.copy()
    cnt = 0
    flg = True
    while flg:
        flg = False
        for i in range(len(_list)-1):
            if _list[i] and not _list[i+1]:
                cnt += 1
                _list = reverse(_list,i)
                _list = reverse(_list,i+1)
                flg = True
    return cnt

print(operate_onestep(Sb))
Problem p03206 (python)

Fixed submission:
D = int(input())
assert 22 <= D <= 25
print(" ".join(["Christmas"] + ["Eve"] * (25 - D)))

Buggy submission (verdict: Runtime Error):
N, X = map(int, input().split())

def get_lburger(l):
    lburger = [1]
    for i in range(l):
        lburger = [0] + lburger + [1] + lburger + [0]
    return lburger

if __name__ == "__main__":
    print(sum(get_lburger(N)[:X]))
Problem p03210 (python)

Fixed submission:
print("YES" if int(input()) in [3,5,7] else "NO")

Buggy submission (verdict: Runtime Error):
S = input().strip()
print(min(int([S[i:i+3]) for i in range(0,len(S)-2,3)))
Problem p03211 (python)

Fixed submission:
S = input().strip()
print(min(abs(753 - int(S[i:i+3])) for i in range(0,len(S)-2)))

Buggy submission (verdict: Runtime Error):
S = input().strip()
print(min(int([S[i:i+3]) for i in range(0,len(S)-2,3)))
Problem p03352 (python)

Fixed submission:
def function(X):
    B = list(range(31, 0, -1))
    res = 0
    for b in B:
        p = 2
        cp = p
        while True:
            tmp = b ** p
            if tmp > X:
                break
            else:
                res = max(res,tmp)
                if b == 1: break
                cp = p
                p += 1
    return res

X = int(input())
print(function(X))

Buggy submission (verdict: Runtime Error):
def function(X):
  	B = list(range(31, 0, -1))
    res = 0
    for b in B:
        p = 2
        cp = p
        while True:
            tmp = b ** p
            if tmp > X:
                break
            else:
                res = max(res,tmp)
                if b == 1: break
                cp = p
                p += 1
    return res

X = int(input())
print(function(X))

Buggy submission (verdict: Runtime Error):
X = int(input())

p = 2
res = 0
for b in B:
    p = 2
    cp = p
    while True:
        tmp = b ** p
        if tmp > X:
            break
        else:
            res = max(res,tmp)
            if b == 1: break
            cp = p
            p += 1
Problem p02744 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)
from functools import lru_cache

n = int(input())
ans = []

if n == 1:
    print('a')
    exit()

@lru_cache(None)
def rec(before, depth, MAX):
    for i in range(MAX-ord('a') +2):
        tmp = before + chr(ord('a') + i)
        if len(tmp) >= n:
            ans.append(tmp)
        else:
            MAX = max(MAX, ord(tmp[-1]) )
            rec(tmp, depth+1, MAX)


rec('a',1, ord('a'))

ans.sort()

print(*ans, sep ='\n')

Buggy submission (verdict: Runtime Error):
mport sys
sys.setrecursionlimit(10 ** 7)
from functools import lru_cache

n = int(input())
ans = []

if n == 1:
    print('a')
    exit()

@lru_cache(None)
def rec(before, depth, MAX):
    for i in range(MAX-ord('a') +2):
        tmp = before + chr(ord('a') + i)
        if len(tmp) >= n:
            ans.append(tmp)
        else:
            MAX = max(MAX, ord(tmp[-1]) )
            rec(tmp, depth+1, MAX)


rec('a',1, ord('a'))

ans.sort()

print(*ans, sep ='\n')
Problem p02761 (python)

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

c = [0]* m
s = [0]* m
for i in range(m):
    s[i], c[i] = map(int, input().split())

flg = True

if n == 1:
    i = 0
    i_str = str(i)
    for j in range(m):
        if i_str[s[j]-1] != str(c[j]):
            flg = False
            continue
    if flg==True:
        print(i)
        exit()
    flg = True

for i in range(10**(n-1),10**n):
    i_str = str(i)
    for j in range(m):
        if i_str[s[j]-1] != str(c[j]):
            flg = False
            continue
    if flg==True:
        print(i)
        exit()
    flg = True

print(-1)

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())

c = [0]* m
s = [0]* m
for i in range(m):
    s[i], c[i] = map(int, input().split())

flg = True

if n == 1:
    i_str = str(0)
    for j in range(m):
        if i_str[s[j]-1] != str(c[j]):
            flg = False
            continue
    if flg==True:
        print(i)
        exit()
    flg = True

for i in range(10**(n-1),10**n):
    i_str = str(i)
    for j in range(m):
        if i_str[s[j]-1] != str(c[j]):
            flg = False
            continue
    if flg==True:
        print(i)
        exit()
    flg = True

print(-1)
Problem p02768 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)
mod = p = 10**9+7
def extgcd(a,b):
    r = [1,0,a]
    w = [0,1,b]
    while w[2]!=1:
        q = r[2]//w[2]
        r2 = w
        w2 = [r[0]-q*w[0],r[1]-q*w[1],r[2]-q*w[2]]
        r = r2
        w = w2
    #[x,y]
    return [w[0],w[1]]

# aの逆元(mod m)を求める。(aとmは互いに素であることが前提)
def mod_inv(a,m):
    x = extgcd(a,m)[0]
    return (m+x%m)%m

n, a, b = map(int, input().split())
nCa= 1
nCb= 1
for i in range(b):
    nCb = (nCb%p * (n-i)%p)%p
    nCb = nCb * mod_inv(i+1, p)
    if i+1 == a:
        nCa = nCb

ans = 2**n - 1- nCa%p - nCb%p
if ans < 0:
    print(0)
else:
    print(int(ans%p))

Buggy submission (verdict: Runtime Error):
def extgcd(a,b):
    r = [1,0,a]
    w = [0,1,b]
    while w[2]!=1:
        q = r[2]//w[2]
        r2 = w
        w2 = [r[0]-q*w[0],r[1]-q*w[1],r[2]-q*w[2]]
        r = r2
        w = w2
    #[x,y]
    return [w[0],w[1]]

# aの逆元(mod m)を求める。(aとmは互いに素であることが前提)
def mod_inv(a,m):
    x = extgcd(a,m)[0]
    return (m+x%m)%m

n, a, b = map(int, input().split())
nCa= 1
nCb= 1
for i in range(b):
    nCb = (nCb%p * (n-i)%p)%p
    nCb = nCb * mod_inv(i+1, p)
    if i+1 == a:
        nCa = nCb

ans = 2**n - 1- nCa%p - nCb%p
if ans < 0:
    print(0)
else:
    print(int(ans%p))
Problem p02775 (python)

Fixed submission:
s = input()
l = len(s)

INF = float('inf')
dp = [[INF]*2 for _ in range(l+1)]
dp[0][0]=0
dp[0][1]=1
for i in range(l):
    num = int(s[i])
    dp0 = dp[i][0]
    dp1 = dp[i][1]
    dp[i+1][0] = min(dp0+num, dp1+min(10-num,num+1))
    dp[i+1][1] = min(dp0+num+1,dp1+10-num-1)

print(dp[-1][0])

Buggy submission (verdict: Runtime Error):
n = int(input())
s = input()
l = len(s)

INF = float('inf')
dp = [[INF]*2 for _ in range(l+1)]
dp[0][0]=0
dp[0][1]=1
for i in range(l):
    num = int(s[i])
    dp0 = dp[i][0]
    dp1 = dp[i][1]
    dp[i+1][0] = min(dp0+num, dp1+min(10-num,num+1))
    dp[i+1][1] = min(dp0+num+1,dp1+10-num-1)

print(dp[-1][0])

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
s = input()
l = len(s)

INF = float('inf')
dp = [[INF]*2 for _ in range(l+1)]
dp[0][0]=0
dp[0][1]=1
for i in range(l):
    num = int(s[i])
    dp0 = dp[i][0]
    dp1 = dp[i][1]
    dp[i+1][0] = min(dp0+num, dp1+min(10-num,num+1))
    dp[i+1][1] = min(dp0+num+1,dp1+10-num-1)

print(dp[-1][0])

Buggy submission (verdict: Runtime Error):
import numpy as np

n = int(input())
l = np.array([int(x) for x in list(str(n))])

ans = 0

num_0to5 = l[l<=5]
num_6to9 = l[l>=6]

ans = np.sum(num_0to5)
ans += np.sum(11-num_6to9)


if l[-1] <=5:
    ans -= len(num_6to9)
else:
    ans -= len(num_6to9)-1

if l[0] >= 6:
    ans += 1


dd = 0
for i in l:
    if i >= 6:
        dd += 1
    else:
        dd = 0

if l[-2] <= 5 and l[-1] >= 6:
    dd -= 1

ans -= dd

print(ans)
Problem p02804 (python)

Fixed submission:
def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7
N = 10**5
g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

n, k = map(int, input().split())
a = list( map(int, input().split()))

ans = 0


a.sort()

for j in range(n):
    i = j +1
    if j <= n-k:
        minX = a[i-1]
        ans -= a[i-1]*cmb(n-i,k-1,mod)%mod
    if k -1 <= i:
        maxX = a[i-1]
        ans += a[i-1]*cmb(i-1,k-1,mod)%mod

print(ans%mod)

Buggy submission (verdict: Runtime Error):
def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7
N = 10**4
g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

n, k = map(int, input().split())
a = list( map(int, input().split()))

ans = 0


a.sort()

print(a)

for j in range(n):
    i = j +1
    if j <= n-k:
        minX = a[i-1]
        ans -= a[i-1]*cmb(n-i,k-1,mod)%mod
    if k -1 <= i:
        maxX = a[i-1]
        ans += a[i-1]*cmb(i-1,k-1,mod)%mod

print(ans%mod)
Problem p02807 (python)

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


def extgcd(a,b):
    r = [1,0,a]
    w = [0,1,b]
    while w[2]!=1:
        q = r[2]//w[2]
        r2 = w
        w2 = [r[0]-q*w[0],r[1]-q*w[1],r[2]-q*w[2]]
        r = r2
        w = w2
    #[x,y]
    return [w[0],w[1]]

def mod_inv(a,mod):
    x = extgcd(a,mod)[0]
    return (mod+x%mod)%mod

ans = 0
MOD = 10**9 + 7

factorial = 1
for i in range(2,n):
    factorial = (factorial * i%MOD) %MOD

p = [0]*(n-1)
p[0] = 1 * factorial%MOD

for i in range(1,n-1):
    p[i] = p[i-1] + factorial*mod_inv(i+1,MOD)
    p[i] %= MOD

for i in range(n-1):
    ans += (x[i+1]-x[i])%MOD*p[i]
    ans %= MOD

print(int(ans))

Buggy submission (verdict: Runtime Error):
n = int(input())
testimony_table = [[] for _ in range(n)]

for i_witness in range(n):
    ai = int(input())
    for _ in range(ai):
        xij, yij= map(int, input().split())
        testimony_table[i_witness].append((xij-1,yij))

ans = 0
for i in range(2**n):
    isConflict = False

    for j_witness in range(n): # それぞれの桁について0か1かを判断したいので
        if (i >> j_witness) & 1 == 0: # j桁目が1かどうかをジャッジ
            continue


        for x_suspect, y_testimony in testimony_table[j_witness]:
            if ((i >> x_suspect) & 1) != y_testimony:
                isConflict = True
                break

        if isConflict:
            break
    if isConflict == False:
        ans = max(ans, bin(i).count("1"))

print(ans)

Buggy submission (verdict: Runtime Error):
import math

n = int(input())
x = list( map(int, input().split()))

ans = 0
MOD = 10**9 + 7

factorial = math.factorial(n-1)

p = [0]*(n-1)
p[0] = 1 * factorial%MOD

for i in range(1,n-1):
    p[i] = p[i-1] + (factorial/(i+1))%MOD
    p[i] %= MOD

for i in range(n-1):
    ans += (x[i+1]-x[i])*p[i]
    ans %= MOD

ans %= MOD

print(int(ans))

Buggy submission (verdict: Runtime Error):
import math

n = int(input())
x = list( map(int, input().split()))

ans = 0
MOD = 10**9 + 7

fact = math.factorial(n-1)

p = [0]*(n-1)
p[0] = 1 * fact%MOD

for i in range(1,n-1):
    p[i] = p[i-1] + (fact/(i+1))%MOD
    p[i] %= MOD

for i in range(n-1):
    ans += (x[i+1]-x[i])*p[i]
    ans %= MOD

ans %= MOD

print(int(ans))

Buggy submission (verdict: Runtime Error):
import math

n = int(input())
x = list( map(int, input().split()))

ans = 0
MOD = 10**9 + 7

p = [0]*(n-1)
p[0] = 1 * math.factorial(n-1)
for i in range(1,n-1):
    p[i] = p[i-1] + math.factorial(n-1)/(i+1)
    p[i] %= MOD

for i in range(n-1):
    ans += (x[i+1]-x[i])*p[i]
    ans %= MOD

ans %= MOD

print(int(ans))

Buggy submission (verdict: Runtime Error):
import math
import numpy as np

n = int(input())
x = list( map(int, input().split()))

x = np.array(x)

dist = x[1:] - x[:-1]

ans = 0
MOD = 10**9 + 7

p = [0]*(n-1)
p[0] = 1 * math.factorial(n-1)
for i in range(1,n-1):
    p[i] = p[i-1] + math.factorial(n-1)/(i+1)
    p[i] %= MOD

for i in range(n-1):
    ans += dist[i]*p[i]
    ans %= MOD

ans %= MOD

print(int(ans))