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

Fixed submission:
a, b = map(int, input().split())

from fractions import gcd
g = gcd(a, b)

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

gg = prime_factorize(g)
print(len(list(set(gg))) + 1)

Buggy submission (verdict: Runtime Error):
a, b = map(int, input().split())

import math
g = math.gcd(a, b)

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

gg = prime_factorize(g)
print(len(list(set(gg))) + 1)
Problem p02972 (python)

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

ans = [0] * n

for i in reversed(range(1, n+1)):
    count = 0
    for j in range(2 * i, n + 1, i):
        count += ans[j-1]

    if count % 2 != a[i-1]:
        ans[i-1] = 1

print(sum(ans))
for i in range(n):
    if ans[i] == 1:
        print(i + 1)

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

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

import numpy as np
baisuu_count = np.zeros(N)
b = []
for i in reversed(range(N)):
    a = A[i]
    if (baisuu_count[i] // 2 == 1 and a == 0) or (baisuu_count[i] // 2 == 0 and a == 1):
        b.append(i + 1)
        divisors = np.array(make_divisors(i + 1))
        baisuu_count[divisors] += 1


if len(b) == 0:
    print(0)
else:
    print(len(b))
    for _b in range(len(b)):
        print(_b + 1)
Problem p02991 (python)

Fixed submission:
n, m = map(int, input().split())
uv = [list(map(int, input().split())) for _ in range(m)]
s, t = map(int, input().split())

import heapq
from collections import defaultdict
g1 = defaultdict(set)
for u, v in uv:
    g1[u-1].add(v-1)

s -= 1
t -= 1

dist = [[float('inf')] * 3 for _ in range(n)]
dist[s][0] = 0

q = [(0, s, 0)] # (dist, pos, kenken)
heapq.heapify(q)

while q:
    du, u, c = heapq.heappop(q)
    if dist[u][c] < du:
        continue
    c2 = (c + 1) % 3
    for v in g1[u]:
        if du + 1 < dist[v][c2]:
            dist[v][c2] = du + 1
            heapq.heappush(q, (dist[v][c2], v, c2))

if dist[t][0] != float('inf'):
    print(dist[t][0] // 3)
else:
    print(-1)

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
uv = [list(map(int, input().split())) for _ in range(m)]
s, t = map(int, input().split())

import heapq
from collections import defaultdict, deque
g1 = defaultdict(set)
for u, v in uv:
    g1[u-1].add(v-1)

s -= 1
t -= 1

dist = [[float('inf')] * 3 for _ in range(n)]
dist[s][0] = 0

q = [(0, s, 0)] # (dist, pos, kenken)
heapq.heapify(q)

while q:
    du, u, c = heapq.heappop(q)
    if dist[u][c] < du:
        continue
    c2 = (c + 1) % 3
    for v in g1[u]:
        if du + 1 < dist[v][c2]:
            dist[v][c2] = du + 1
            heapq.heappush(q, (dist[v][c2], v, c2))

if dist[t][0] != float('inf'):
    print(d)
else:
    print(-1)
Problem p02994 (python)

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

import numpy as np
taste = np.arange(n)
taste = taste + l

idx = np.argmin(np.abs(taste))

s = 0
for i in range(n):
    if i != idx:
        s += taste[i]

print(s)

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

import numpy as np
taste = np.arange(n)
taste = taste + l

idx = np.argmin(np.abs(taste))

s = 0
for i in range(n):
    if i != idx:
        s += taste[i]

print(s)
Problem p03000 (python)

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

coutner = 1
d = 0

for i in range(n):
    d += l[i]

    if d > x:
        break
    else:
        coutner += 1


print(coutner)

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

i = 0
total = 0
while total <= x:
    total += l[i]
    i += 1
print(i)
Problem p03001 (python)

Fixed submission:
w, h, x, y = map(int, input().split())

m = w * h / 2
if x == w / 2 and y == h / 2:
    flag = 1
else:
    flag = 0
print(m, flag)

Buggy submission (verdict: Runtime Error):
w, h, x, y = map(int, input().split())

if x > w / 2:
    x = w - x
if y > h / 2:
    y = h - y


if x / w > y / h:
    a = []
    a.append(w * w * y / x / 2)
    a.append(h * h * x / (h - y) / 2)
    a.append(w * w * y / (w - x) / 2)
    a.append(h * h  * (w - x) / (h - y) / 2)
    a.append(x * h)
    a.append(y * w)
else:
    a = []
    a.append(h * h * x / y / 2)
    a.append(h * (h - y) * x / y / 2)
    a.append(w * w * y / (w - x) / 2)
    a.append(w * w * (h - y) / (w - x) / 2)
    a.append(x * h)
    a.append(y * w)

m = max(a)

counter = 0
for i in range(6):
    if a[i] == m:
        counter += 1
if counter >= 2:
    flag = 1
else:
    flag = 0

print(m, flag)
Problem p03037 (python)

Fixed submission:
n, m = list(map(int, input().split()))
lr = [list(map(int, input().split())) for _ in range(m)]

l_list = [_lr[0] for _lr in lr]
r_list = [_lr[1] for _lr in lr]

a = min(r_list)
b = max(l_list)
print(max(a - b + 1, 0))

Buggy submission (verdict: Runtime Error):
n, m = list(map(int, input().split()))
lr = [list(map(int, input().split())) for _ in range(m)]

import numpy as np
tmp = np.zeros(n)

for i in range(m):
    l = lr[i][0] - 1
    r = lr[i][1] - 1
    tmp[l: r+1] = tmp[l: r+1] + 1

counter = 0
for j in range(n):
    if tmp[j] == m:
        counter += 1

print(counter)
Problem p03047 (python)

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

print(nk[0] - nk[1] + 1)

Buggy submission (verdict: Runtime Error):
n, k = list(map(int, input()))

print(n - k + 1)
Problem p03074 (python)

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

counts = []
tmp = []
for i in range(len(s)):
    if tmp == []:
        tmp = [s[i], 1]
    else:
        if tmp[0] == s[i]:
            tmp[1] += 1
        else:
            counts.append(tmp)
            tmp = [s[i], 1]
if tmp:
    counts.append(tmp)

zero_ints = 0
for char, count in counts:
    if char == '0':
        zero_ints += 1

if counts[0][0] == '0':
    left = 0
else:
    left = 1
right = left - 2

used = 0
l = 0
for char, count in counts:
    if char == '0':
        if used < k:
            used += 1
            right += 2
        else:
            break
    l += count

ans = l
for i in range(zero_ints):
    l = l - counts[left][1]
    if left - 1 >= 0:
        l-= counts[left - 1][1]
    if right + 2 < len(counts):
        l += counts[right + 2][1]
    if right + 3 < len(counts):
        l += counts[right + 3][1]
    ans = max(ans, l)
    left += 2
    right += 2
    # print(left, right, l)
print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
s = input() # str

zero_positions = []
flag = False
zeros = []
for i in range(n):
    if s[i] == '0':
        zeros.append(i)
        flag = True
    if s[i] == '1' and flag:
        flag = False
        zero_positions.append(zeros)
        zeros = []
if zeros != []:
    zero_positions.append(zeros)

def get_one_positions(s):
    one_positions_len = []
    flag = False
    ones_len = 0
    for i in range(n):
        if s[i] == '1':
            ones_len += 1
            flag = True
        if s[i] == '0' and flag:
            flag = False
            one_positions_len.append(ones_len)
            ones_len = 0
    if ones_len != []:
        one_positions_len.append(ones_len)
    return(max(one_positions_len))

one_positions_lens = []
for i in range(len(zero_positions) - k + 1):
    choice = []
    for j in range(k):
        choice.extend(zero_positions[i + j])

    # reverse
    s_new = []
    for l in range(n):
        if l in choice:
            s_new.append('1')
        elif s[l] == '1':
            s_new.append('1')
        elif s[l] == '0':
            s_new.append('0')


    # count
    one_positions_len = get_one_positions(s_new)
    one_positions_lens.append(one_positions_len)

print(max(one_positions_lens))
Problem p03128 (python)

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

x = [-float('inf')] * (n + 1)
x[0] = 0
l = [0] * (n + 1)
_c = [2,5,5,4,5,6,3,7,6]
c = dict()
for i in range(m):
    c[a[i]] = _c[a[i] - 1]

for i in range(1, n + 1):
    x_max = -float('inf')
    l_max = 0
    for digit in c.keys():
        left = i - c[digit]
        if left >= 0:
            tmp_x = digit * (10 ** l[left]) + x[left]
            tmp_l = l[left] + 1
            if tmp_x > x_max:
                x_max = tmp_x
                l_max = tmp_l

    x[i] = x_max
    l[i] = l_max
print(x[-1])

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

x = [-float('inf')] * (n + 1)
x[0] = 0
l = [0] * (n + 1)
_c = [2,5,5,4,5,6,3,7,6]
c = dict()
for i in range(m):
    c[a[i]] = _c[a[i] - 1]
# print(c)

for i in range(1, n + 1):
    tmp_xl = []
    for digit in c.keys():
        left = i - c[digit]
        if left >= 0:
            tmp_xl.append([digit * (10 ** l[left]) + x[left], l[left] + 1])

    if len(tmp_xl):
        tmp_xl = sorted(tmp_xl, key=lambda x:x[0])
        x[i] = tmp_xl[-1][0]
        l[i] = tmp_xl[-1][1]
print(x[-1])

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

x = [-float('inf')] * (n + 1)
x[0] = 0
l = [0] * (n + 1)
_c = [2,5,5,4,5,6,3,7,6]
c = dict()
for i in range(m):
    c[a[i]] = _c[a[i] - 1]
# print(c)

for i in range(1, n + 1):
    tmp_xl = []
    for digit in c.keys():
        left = i - c[digit]
        if left >= 0:
            tmp_xl.append([digit * (10 ** l[left]) + x[left], l[left] + 1])

    if len(tmp_xl):
        tmp_xl = sorted(tmp_xl, key=lambda x:x[0])
        x[i] = tmp_xl[-1][0]
        l[i] = tmp_xl[-1][1]
print(x[-1])
Problem p03262 (python)

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

x.append(a)
x.sort()

diff_all = []
for i in range(len(x) - 1):
    diff_all.append(abs(x[i] - x[i+1]))

import fractions
from functools import reduce

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

print(gcd_list(diff_all))

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

x.append(a)
x.sort()

diff_all = []
for i in range(len(x) - 1):
    diff_all.append(abs(x[i] - x[i+1]))

import fraction
from functools import reduce

def gcd_list(numbers):
    return reduce(fraction.gcd, numbers)

print(gcd_list(diff_all))

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

x.append(X)
x.sort()

diff_all = []
for i in range(len(x) - 1):
    diff_all.append(abs(x[i] - x[i+1]))

import math
from functools import reduce

def gcd_list(numbers):
    return reduce(math.gcd, numbers)

print(gcd_list(diff_all))

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

diff_all = []
x.append(X)
x.sort()

for i in range(len(x) - 1):
    diff_all.append(abs(x[i] - x[i+1]))

import math
from functools import reduce

def gcd_list(numbers):
    return reduce(math.gcd, numbers)
print(gcd_list(diff_all))
Problem p03274 (python)

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

x_neg = []
x_pos = []
for xx in x:
    if xx < 0:
        x_neg.append(-xx)
    else:
        x_pos.append(xx)
x_neg = x_neg[::-1]

ans = float('inf')
if k <= len(x_neg):
    ans = min(ans, x_neg[k-1])
if k <= len(x_pos):
    ans = min(ans, x_pos[k-1])

for i in range(1, k):
    j = k - i
    if i <= len(x_pos) and 1 <= j <= len(x_neg):
        d1 = x_pos[i - 1]
        d2 = x_neg[j - 1]
        d = min(d1, d2) * 2 + max(d1, d2)
        ans = min(ans, d)
print(ans)

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

neg_x = []
for i in range(n):
    if x[i] < 0:
        neg_x.append(x[i])
    if x[i] >= 0:
        break
pos_x = x[len(neg_x):]
neg_x = neg_x[::-1]

if len(neg_x) == 0:
    print(pos_x[k - 1])
elif len(pos_x) == 0:
    print(-1 * neg_x[k - 1])
else:
    total_all = []
    for i in range(max(0, k - len(pos_x)), min(k, len(neg_x))):
        if i == 0:
            total1 = 0
        else:
            total1 = -1 * neg_x[i - 1]
        if k - i == 0:
            total2 = 0
        else:
            total2 = pos_x[k - i - 1]
        total = min(total1 * 2 + total2, total2 * 2 + total1)
        total_all.append(total)
    print(min(total_all))
Problem p03282 (python)

Fixed submission:
s = input()
k = int(input())

if s[0] != '1':
    print(s[0])
else:
    len_1 = 0
    while True:
        if s == '1':
            len_1 = 1
            ans = 1
            break
        if s[0] == '1':
            s = s[1:]
            len_1 += 1
        elif s[0] != '1':
            ans = s[0]
            break
    if k > len_1:
        print(ans)
    else:
        print('1')

Buggy submission (verdict: Runtime Error):
s = input()
k = int(input())

while True:
    if s[0] == '1':
        s = s[1:]
    elif s[0] != '1':
        ans = s[0]
        break
    if s == []:
        ans = 1
        break
print(ans)

Buggy submission (verdict: Runtime Error):
s = input()
k = int(input())

i = 0
while True:
    if s[i] == '1':
        i += 1
    elif s[i] != '1':
        ans = s[i]
        break
print(ans)
Problem p03354 (python)

Fixed submission:
n, m = map(int, input().split())
p = list(map(int, input().split()))
xy = [list(map(int, input().split())) for _ in range(m)]

from collections import defaultdict
g = defaultdict(list)
for x, y in xy:
    x, y = x - 1, y - 1
    g[x].append(y)
    g[y].append(x)

visited = [0] * n
ans = 0

for i in range(n):
    if visited[i] == 0:
        q = [i]
        visited[i] = 1
        s = set([i])
        while q:
            cur = q.pop()
            for target in g[cur]:
                if visited[target] == 0:
                    visited[target] = 1
                    q.append(target)
                    s.add(target)

        ps = set()
        for j in s:
            ps.add(p[j] - 1)

        intersect = s & ps
        ans += len(list(intersect))
print(ans)

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
p = list(map(int, input().split()))
xy = [list(map(int, input().split())) for _ in range(m)]

class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

u = UnionFind(n)
for i in range(m):
    x = xy[i][0]
    y = xy[i][1]
    u.union(x, y)

idxs = []
for i in range(1, n+1):
    idxs.append(u.find(i))

idx_dict = dict()
keys = []
for i in range(n):
    if idxs[i] not in keys:
        idx_dict[idxs[i]] = [i]
        keys.append(idx[i])
    else:
        idx_dict[idxs[i]].append(i)

counter = 0
for k in keys:
    l = idx_dict[k]
    template = []
    for i in range(len(l)):
        template.append(p[l[i]] - 1)

    counter += len(set(l) & set(template))

print(counter)
Problem p03447 (python)

Fixed submission:
x = int(input())
a = int(input())
b = int(input())

print((x - a) % b)

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

counter = 0
for a in range(A + 1):
  for b in range(B + 1):
    for c in range(C + 1):
      if X == a * 500 + b * 100 + c * 50:
        counter += 1
print(counter)
Problem p03469 (python)

Fixed submission:
s = list(input())

s[3] = '8'
print(''.join(s))

Buggy submission (verdict: Runtime Error):
n,y = map(int,input().split())
total = 0
for i in range(n+1):
	for j in range(n+1-i):
		k = n - i - j
		total = i*10000 + j*5000 + k*1000
		if total == y:
			print(i,j,k)
			break
		total = 0
	else:
		continue
	break
else:
	print("-1 -1 -1")
Problem p03472 (python)

Fixed submission:
n, h = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]

a_all = [_ab[0] for _ab in ab]
b_all = [_ab[1] for _ab in ab]

a_max = max(a_all)

b_all_useful = []
for b in b_all:
    if b > a_max:
        b_all_useful.append(b)

b_all_useful.sort()

useful_sum = sum(b_all_useful)
if useful_sum > h:
    counter = 0
    i = 0
    while True:
        counter += b_all_useful[-1 * (i + 1)]
        i += 1
        if counter >= h:
            break
    print(i)

else:
    h_new = h - useful_sum
    import math
    print(math.ceil(h_new / a_max) + len(b_all_useful))

Buggy submission (verdict: Runtime Error):
n, h = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]

a_all = [_ab[0] for _ab in ab]
b_all = [_ab[1] for _ab in ab]

a_max = max(a_all)

b_all_useful = []
for b in b_all:
    if b > a_max:
        b_all_useful.append(b)

b_all_useful.sort()

useful_sum = sum(b_all_useful)
if useful_sum > h:
    counter = 0
    i = 0
    while True:
        counter += b_all_useful[-1 * ii]
        i += 1
        if counter >= h:
            break
    print(i)

else:
    h_new = h - useful_sum
    import math
    print(math.ceil(h_new / a_max) + len(b_all_useful))
Problem p03493 (python)

Fixed submission:
import numpy as np

a = str(input())
s1 = int(a[0])
s2 = int(a[1])
s3 = int(a[2])

print(np.sum(s1 + s2 + s3))

Buggy submission (verdict: Runtime Error):
N = input()
A = input().split()
A = [int(a) for a in A]

counter = 0
while sum([a % 2 for a in A]) == 0:
  A = [a / 2 for a in A]
  counter += 1
print(counter)
Problem p03545 (python)

Fixed submission:
n = input()

for i in range(2 ** 3):
    i2 = bin(i)[2:].zfill(3)
    i3 = n[0]
    total = int(n[0])
    for j in range(3):
        if i2[j] == '1':
            # plus
            total += int(n[j+1])
            i3 += '+'
        else:
            # minus
            total -= int(n[j+1])
            i3 += '-'
        i3 += n[j+1]

    if total == 7:
        break

print(i3 + '=7')

Buggy submission (verdict: Runtime Error):
n = list(input().split())


for i in range(2 ** 3):
    i2 = bin(i)[2:].zfill(3)
    i3 = n[0]
    total = int(n[0])
    for j in range(3):
        if i2[j] == '1':
            # plus
            total += int(n[j+1])
            i3 += '+'
        else:
            # minus
            total -= int(n[j+1])
            i3 += '-'
        i3 += n[j+1]

    if total == 7:
        break

print(i3 + '=7')
Problem p03550 (python)

Fixed submission:
n, z, w = map(int, input().split())
a = list(map(int, input().split()))

score = []

# only X
score.append(abs(a[-1] - w))

if n > 1:
    # X -> Y
    score.append(abs(a[-2] - a[-1]))

print(max(score))

Buggy submission (verdict: Runtime Error):
n, z, w = map(int, input().split())
a = list(map(int, input().split()))

score = []

# only X
score.append(abs(a[-1] - w))

if n > 1:
    # X -> Y
    score.append(abs(a[i] - a[-1]))

print(max(score))
Problem p03599 (python)

Fixed submission:
A, B, C, D, E, F = map(int, input().split())

ans = [0, 0]
max_conc = -1
for a in range(F // 100 + 1):
    left = F - 100 * a * A
    for b in range(left // 100 + 1):
        w = 100 * a * A + 100 * b * B

        s_max = min(F - w, w * E // 100)
        if s_max >= 0:
            for c in range(s_max // C + 1):
                d = (s_max - c * C) // D

                if d >= 0:
                    # suger
                    s = c * C + d * D
                    if s + w > 0:
                        conc = s / (s + w)
                        if conc > max_conc:
                            ans = [s, w]
                            max_conc = conc


print(str(ans[0] + ans[1]) + ' ' + str(ans[0]))

Buggy submission (verdict: Runtime Error):
A, B, C, D, E, F = map(int, input().split())

ans = []
max_conc = 0
for a in range(F // 100 + 1):
    left = F - 100 * a * A
    for b in range(left // 100 + 1):
        w = 100 * a * A + 100 * b * B

        s_max = min(F - w, w * E // 100)
        if s_max >= 0:
            for c in range(s_max // C + 1):
                d = (s_max - c * C) // D

                if d >= 0:
                    # suger
                    s = c * C + d * D
                    if s + w > 0:
                        conc = s / (s + w)
                        if conc > max_conc:
                            ans = [s, w]
                            max_conc = conc


print(str(ans[0] + ans[1]) + ' ' + str(ans[0]))

Buggy submission (verdict: Runtime Error):
A, B, C, D, E, F = map(int, input().split())

ans = []
max_conc = 0
for a in range(F // 100 + 1):
    for b in range(F // 100 + 1):
        w = 100 * a * A + 100 * b * B

        s_max = min(F - w, w * E // 100)
        if s_max >= 0:
            for c in range(s_max // C + 1):
                d_max = (s_max - c * C) // D

                if d_max >= 0:
                    # suger
                    s = c * C + d_max * D
                    if s + w > 0:
                        conc = s / (s + w)
                        if conc > max_conc:
                            ans = [s, w]

print(str(ans[0] + ans[1]) + ' ' + str(ans[0]))
Problem p03608 (python)

Fixed submission:
N, M, R = map(int, input().split())
r = list(map(int, input().split()))
abc = [list(map(int, input().split())) for _ in range(M)]

cost = [[float('inf')] * N for _ in range(N)]
for i in range(M):
    a, b, c = abc[i][0], abc[i][1], abc[i][2]
    cost[a-1][b-1] = c
    cost[b-1][a-1] = c

from scipy.sparse.csgraph import floyd_warshall
dist = floyd_warshall(cost)

mindist = float('inf')
import itertools
for it in itertools.permutations(r):
    it = list(it)
    d = 0
    for i in range(R - 1):
        d += int(dist[it[i]-1][it[i+1]-1])
    # print(it)
    mindist = min(mindist, d)
print(mindist)

Buggy submission (verdict: Runtime Error):
N, M, R = map(int, input().split())
r = list(map(int, input().split()))
abc = [list(map(int, input().split())) for _ in range(M)]

cost = [[float('inf')] * N for _ in range(N)]
for i in range(M):
    a, b, c = abc[i][0], abc[i][1], abc[i][2]
    cost[a-1][b-1] = c
    cost[b-1][a-1] = c

from scipy.sparse.csgraph import floyd_warshall
dist = floyd_warshall(cost)

mindist = float('inf')
import itertools
for it in itertools.permutations(r):
    it = list(it)
    d = 0
    for i in range(R - 1):
        d += int(dist[it[i]-1][it[i+1]-1])
    # print(it)
    mindist = min(mindist, d)
print(mindist)