Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
class Pqueue:
    heap = []
    tail = -1

    def insert(self, k):
        self.heap.append(k)
        self.tail += 1
        self.bubble(self.tail)

    def bubble(self, i):
        if not i: return
        heap = self.heap
        pi = (i - 1) // 2
        if heap[i] > heap[pi]:
            heap[i], heap[pi] = heap[pi], heap[i]
            self.bubble(pi)

    def extract(self):
        heap = self.heap
        self.tail -= 1
        if self.tail < 0:
            return heap.pop()
        else:
            ret = heap[0]
            heap[0] = heap.pop()
            self.anchor(0)
            return ret

    def anchor(self, i):
        li, ri = i * 2 + 1, i * 2 + 2
        if li <= self.tail:
            heap = self.heap
            largest = li
            if ri <= self.tail and heap[ri] > heap[li]:
                largest = ri
            if heap[largest] > heap[i]:
                heap[i], heap[largest] = heap[largest], heap[i]
                self.anchor(largest)


pq = Pqueue()

while True:
    l = input()
    o = l[:2]
    if o == 'in':
        pq.insert(int(l.split()[1]))
    elif o == 'ex':
        print(pq.extract())
    else:
        break

Fixed submission:
from heapq import heappush, heappop

heap = []

while True:
    l = input()
    if l.startswith('i'):
        heappush(heap, -int(l.split()[1]))
    elif l[1] == 'x':
        print(-heappop(heap))
    else:
        break

Buggy python submission (verdict: Time Limit Exceeded):
heap = []
tail = -1


def insert(k):
    global heap, tail
    heap.append(k)
    tail += 1
    bubble(tail)


def bubble(i):
    global heap
    if not i: return
    pi = (i - 1) // 2
    if heap[i] > heap[pi]:
        heap[i], heap[pi] = heap[pi], heap[i]
        bubble(pi)


def extract():
    global heap, tail
    tail -= 1
    if tail < 0:
        return heap.pop()
    else:
        ret = heap[0]
        heap[0] = heap.pop()
        anchor(0)
        return ret


def anchor(i):
    global heap, tail
    li, ri = i * 2 + 1, i * 2 + 2
    if li <= tail:
        largest = li
        if ri <= tail and heap[ri] > heap[li]:
            largest = ri
        if heap[largest] > heap[i]:
            heap[i], heap[largest] = heap[largest], heap[i]
            anchor(largest)


while True:
    l = input()
    if l.startswith('i'):
        insert(int(l.split()[1]))
    elif l[1] == 'x':
        print(extract())
    else:
        break

Fixed submission:
from heapq import heappush, heappop

heap = []

while True:
    l = input()
    if l.startswith('i'):
        heappush(heap, -int(l.split()[1]))
    elif l[1] == 'x':
        print(-heappop(heap))
    else:
        break
Buggy python submission (verdict: Runtime Error):
def cross(a, b):
    return a.real * b.imag - a.imag * b.real


def cross_point(c, d):
    l = d - c
    v1 = cross(lv, l)
    v2 = cross(lv, lt - c)
    return c + v2 / v1 * l


n = int(input())
points = [complex(*map(int, input().split())) for _ in range(n)]
point0 = points.pop(0)
points.append(point0)

q = int(input())
while q:
    x1, y1, x2, y2 = map(int, input().split())
    ls, lt = (x1 + 1j * y1, x2 + 1j * y2)
    lv = lt - ls

    area = 0
    prev = point0
    prev_flag = cross(lv, prev - ls) >= 0
    cp1, cp2 = None, None
    for p in points:
        curr_flag = cross(lv, p - ls) >= 0
        if prev_flag and curr_flag:
            area += cross(prev, p)
        elif prev_flag != curr_flag:
            cp = cross_point(prev, p)
            if prev_flag:
                area += cross(prev, cp)
                cp1 = cp
            else:
                area += cross(cp, p)
                cp2 = cp
        prev, prev_flag = p, curr_flag
    area += cross(cp1, cp2)
    print(area / 2)
    q -= 1

Fixed submission:
def cross(a, b):
    return a.real * b.imag - a.imag * b.real


def cross_point(c, d):
    l = d - c
    v1 = cross(lv, l)
    v2 = cross(lv, lt - c)
    return c + v2 / v1 * l


n = int(input())
points = [complex(*map(int, input().split())) for _ in range(n)]
point0 = points.pop(0)
points.append(point0)

q = int(input())
while q:
    x1, y1, x2, y2 = map(int, input().split())
    ls, lt = (x1 + 1j * y1, x2 + 1j * y2)
    lv = lt - ls

    area = 0
    prev = point0
    prev_flag = cross(lv, prev - ls) >= 0
    cp1, cp2 = None, None
    for p in points:
        curr_flag = cross(lv, p - ls) >= 0
        if prev_flag and curr_flag:
            area += cross(prev, p)
        elif prev_flag != curr_flag:
            cp = cross_point(prev, p)
            if prev_flag:
                area += cross(prev, cp)
                cp1 = cp
            else:
                area += cross(cp, p)
                cp2 = cp
        prev, prev_flag = p, curr_flag
    if cp1 is not None and cp2 is not None:
        area += cross(cp1, cp2)
    print(area / 2)
    q -= 1
Buggy python submission (verdict: Runtime Error):
n = int(input())
array = [int(input()) for _ in range(n)]
dp = [0] * (max(array) + 1)

for a in array:
    dp[a] = max(dp[a], max(dp[:a]) + 1)

print(max(dp))

Fixed submission:
from bisect import bisect_left

n = int(input())
dp = [int(1e9) + 1] * n
eol = 0

for _ in range(n):
    a = int(input())
    if dp[eol] < a:
        eol += 1
        dp[eol] = a
    else:
        dp[bisect_left(dp, a, hi=eol)] = a

print(eol + 1)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
array = [int(input()) for _ in range(n)]
dp = [0] * n

for i, ai in enumerate(array):
    dp[i] = max((dp[j] for j, aj in enumerate(array[:i]) if aj < ai), default=0) + 1

print(max(dp))

Fixed submission:
from bisect import bisect_left

n = int(input())
dp = [int(1e9) + 1] * n
eol = 0

for _ in range(n):
    a = int(input())
    if dp[eol] < a:
        eol += 1
        dp[eol] = a
    else:
        dp[bisect_left(dp, a, hi=eol)] = a

print(eol + 1)
Buggy python submission (verdict: Runtime Error):
n, knapsack = map(int, input().split())
dp = [0] * (knapsack + 1)
for i in range(n):
    value, weight = map(int, input().split())
    for j in range(knapsack - weight, 0, -1):
        if not dp[j]:
            continue
        new_value = dp[j] + value
        if dp[j + weight] < new_value:
            dp[j + weight] = new_value
    if dp[weight] < value:
        dp[weight] = value
print(max(dp))

Fixed submission:
from math import isfinite

n, knapsack = map(int, input().split())
dpn = n * 100  # MAX VALUE
dp = [float('inf')] * (dpn + 1)
dp[0] = 0
for i in range(n):
    value, weight = map(int, input().split())
    for j in range(dpn - value, -1, -1):
        if not isfinite(dp[j]):
            continue
        new_weight = dp[j] + weight
        if new_weight <= knapsack and dp[j + value] > new_weight:
            dp[j + value] = new_weight

print(next(filter(lambda i: isfinite(dp[i]), range(dpn, -1, -1))))
Buggy python submission (verdict: Runtime Error):
from collections import defaultdict

v, e = map(int, input().split())

links = [defaultdict(int) for _ in range(v)]
bests = [None] * (1 << v)

for _ in range(e):
    s, t, d = map(int, input().split())
    links[s][t] = max(links[s][t], d)

bests[0] = {0: 0}

for visited, best in enumerate(bests):
    if best is None:
        continue
    for last, cost in best.items():
        for t, d in links[last].items():
            new_visited = visited | (1 << t)
            if visited == new_visited:
                continue
            new_best = bests[new_visited]
            if new_best is None:
                bests[new_visited] = defaultdict(lambda: 15001, [(t, cost + d)])
            else:
                new_best[t] = min(new_best[t], cost + d)

print(bests[-1][0])

Fixed submission:
def warshall_floyd(n, dists):
    prev = [t.copy() for t in dists]

    for k in range(n):
        current = [[0] * n for _ in range(n)]
        prev_k = prev[k]
        for i in range(n):
            prev_i, current_i = prev[i], current[i]
            prev_i_k = prev_i[k]
            for j in range(n):
                current_i[j] = min(prev_i[j], prev_i_k + prev_k[j])
        prev = current

    return prev


def solve(n, links, total_d, odd_vertices):
    if not odd_vertices:
        return total_d

    d_table = warshall_floyd(n, links)
    d_table = [[d for oj, d in enumerate(d_table[oi]) if oj in odd_vertices] for ni, oi in enumerate(odd_vertices)]
    ndt = len(d_table)
    bit_dict = {1 << i: i for i in range(ndt)}

    def minimum_pair(remains):
        if not remains:
            return 0
        b = remains & -remains
        remains ^= b
        i = bit_dict[b]
        return min(minimum_pair(remains ^ (1 << j)) + d_table[i][j] for j in range(ndt) if remains & (1 << j))

    return total_d + minimum_pair((1 << ndt) - 1)


v, e = map(int, input().split())

dists = [[float('inf')] * v for _ in range(v)]
for i in range(v):
    dists[i][i] = 0
odd_vertices = [0] * v
total_d = 0

for _ in range(e):
    s, t, d = map(int, input().split())
    dists[s][t] = min(dists[s][t], d)
    dists[t][s] = min(dists[t][s], d)
    odd_vertices[s] ^= 1
    odd_vertices[t] ^= 1
    total_d += d

print(solve(v, dists, total_d, [i for i, v in enumerate(odd_vertices) if v]))
Buggy python submission (verdict: Runtime Error):
def calc(p1, p2, v):
    dists_v = dists[v]
    if v == n - 1:
        return dists_v[p1] + dists_v[p2]
    if visited[p1][p2]:
        return dp[p1][p2]
    dp[p1][p2] = min(dists_v[p1] + calc(p2, v, v + 1), dists_v[p2] + calc(p1, v, v + 1))
    visited[p1][p2] = True
    return dp[p1][p2]


n = int(input())
points = [complex(*map(int, input().split())) for _ in range(n)]
dists = [[abs(p1 - p2) for p2 in points] for p1 in points]
visited = [[False] * n for _ in range(n)]
dp = [[0] * n for _ in range(n)]

print(calc(0, 0, 0))

Fixed submission:
import sys
sys.setrecursionlimit(1000000)

def calc(p1, p2, v):
    dists_v = dists[v]
    if v == n - 1:
        return dists_v[p1] + dists_v[p2]
    if visited[p1][p2]:
        return dp[p1][p2]
    dp[p1][p2] = min(dists_v[p1] + calc(p2, v, v + 1), dists_v[p2] + calc(p1, v, v + 1))
    visited[p1][p2] = True
    return dp[p1][p2]


n = int(input())
points = [complex(*map(int, input().split())) for _ in range(n)]
dists = [[abs(p1 - p2) for p2 in points] for p1 in points]
visited = [[False] * n for _ in range(n)]
dp = [[0] * n for _ in range(n)]

print(calc(0, 0, 0))

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(1000)

def calc(p1, p2, v):
    dists_v = dists[v]
    if v == n - 1:
        return dists_v[p1] + dists_v[p2]
    if visited[p1][p2]:
        return dp[p1][p2]
    dp[p1][p2] = min(dists_v[p1] + calc(p2, v, v + 1), dists_v[p2] + calc(p1, v, v + 1))
    visited[p1][p2] = True
    return dp[p1][p2]


n = int(input())
points = [complex(*map(int, input().split())) for _ in range(n)]
dists = [[abs(p1 - p2) for p2 in points] for p1 in points]
visited = [[False] * n for _ in range(n)]
dp = [[0] * n for _ in range(n)]

print(calc(0, 0, 0))

Fixed submission:
import sys
sys.setrecursionlimit(1000000)

def calc(p1, p2, v):
    dists_v = dists[v]
    if v == n - 1:
        return dists_v[p1] + dists_v[p2]
    if visited[p1][p2]:
        return dp[p1][p2]
    dp[p1][p2] = min(dists_v[p1] + calc(p2, v, v + 1), dists_v[p2] + calc(p1, v, v + 1))
    visited[p1][p2] = True
    return dp[p1][p2]


n = int(input())
points = [complex(*map(int, input().split())) for _ in range(n)]
dists = [[abs(p1 - p2) for p2 in points] for p1 in points]
visited = [[False] * n for _ in range(n)]
dp = [[0] * n for _ in range(n)]

print(calc(0, 0, 0))
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
hs = list(map(int, input().split()))
maxh = max(hs)
dp = [-1] * (maxh + 1)
ans = 0

for i, h in enumerate(hs):
    for j in range(h + 1):
        if dp[j] == -1:
            dp[j] = i
    for j in range(h + 1, maxh + 1):
        if dp[j] != -1:
            ans = max(ans, j * (i - dp[j]))
            dp[j] = -1
ans = max(ans, max((n - i) * j for j, i in enumerate(dp) if i != -1))

print(ans)

Fixed submission:
n = int(input())
hs = list(map(int, input().split()))
stack = []
ans = 0

for i, h in enumerate(hs):
    j = -1
    while stack and stack[-1][1] > h:
        j, h2 = stack.pop()
        ans = max(ans, (i - j) * h2)
    if not stack or stack[-1][1] < h:
        stack.append((i if j == -1 else j, h))
ans = max(ans, max((n - j) * h2 for j, h2 in stack))

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split())

set_list = [{i} for i in range(n)]
set_dict = list(range(n))

while q:
    op, x, y = map(int, input().split())
    if op:
        print(1 if set_dict[x] == set_dict[y] else 0)
    else:
        idx_x, idx_y = set_dict[x], set_dict[y]
        set_y = set_list[idx_y]

        for i in set_y:
            set_dict[i] = idx_x
        set_list[idx_x] |= set_y

    q -= 1

Fixed submission:
def find(x):
    global set_list
    if set_list[x] == x:
        return x
    result = set_list[x] = find(set_list[x])
    return result


n, q = map(int, input().split())
set_list = list(range(n))

while q:
    op, x, y = map(int, input().split())
    if op:
        print(1 if find(x) == find(y) else 0)
    else:
        set_list[find(y)] = find(x)
    q -= 1

Buggy python submission (verdict: Runtime Error):
def find(x):
    global set_list
    return x if set_list[x] == x else find(set_list[x])


n, q = map(int, input().split())
set_list = list(range(n))

while q:
    op, x, y = map(int, input().split())
    if op:
        print(1 if find(x) == find(y) else 0)
    else:
        if x > y:
            x, y = y, x
        set_list[find(y)] = x

    q -= 1

Fixed submission:
def find(x):
    global set_list
    if set_list[x] == x:
        return x
    result = set_list[x] = find(set_list[x])
    return result


n, q = map(int, input().split())
set_list = list(range(n))

while q:
    op, x, y = map(int, input().split())
    if op:
        print(1 if find(x) == find(y) else 0)
    else:
        set_list[find(y)] = find(x)
    q -= 1

Buggy python submission (verdict: Runtime Error):
def find(x):
    global set_list
    return x if set_list[x] == x else find(set_list[x])


n, q = map(int, input().split())
set_list = list(range(n))

while q:
    op, x, y = map(int, input().split())
    if op:
        print(1 if find(x) == find(y) else 0)
    else:
        if x > y:
            x, y = y, x
        set_list[find(y)] = find(x)

    q -= 1

Fixed submission:
def find(x):
    global set_list
    if set_list[x] == x:
        return x
    result = set_list[x] = find(set_list[x])
    return result


n, q = map(int, input().split())
set_list = list(range(n))

while q:
    op, x, y = map(int, input().split())
    if op:
        print(1 if find(x) == find(y) else 0)
    else:
        set_list[find(y)] = find(x)
    q -= 1
Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split())

l = [2 ** 31 - 1] * n

while q:
    op, s, t = map(int, input().split())
    if op:
        print(min(l[s:t + 1]))
    else:
        l[s] = t
    q -= 1

Fixed submission:
class SegTreeMin:
    SENTINEL = (1 << 31) - 1
    output = []

    def __init__(self, n):
        n2 = 1
        while n2 < n:
            n2 <<= 1
        self.n2 = n2
        self.tree = [self.SENTINEL] * (n2 << 1)

    def update(self, i, x):
        i += self.n2
        self.tree[i] = x
        while i > 1:
            self.tree[i >> 1] = x = min(x, self.tree[i ^ 1])
            i >>= 1

    def get_min(self, s, t):
        self.output.append(self._get_min(s, t + 1, 1, 0, self.n2))

    def _get_min(self, a, b, k, l, r):
        if r <= a or b <= l:
            return self.SENTINEL
        if a <= l and r <= b:
            return self.tree[k]
        m = (l + r) // 2
        k <<= 1
        return min(
            self._get_min(a, b, k, l, m),
            self._get_min(a, b, k + 1, m, r))

    def print(self):
        print('\n'.join(map(str, self.output)))


n, q = map(int, input().split())
st = SegTreeMin(n)
fs = [st.update, st.get_min]
for c, x, y in (map(int, input().split()) for _ in range(q)):
    fs[c](x, y)
st.print()
Buggy python submission (verdict: Runtime Error):
from math import sqrt, ceil

n, q = map(int, input().split())
unit = ceil(sqrt(n))

l = [(0, [0] * unit) for _ in range(unit)]

while q:
    op, s, t = map(int, input().split())
    sd, sm = s // unit, s % unit
    if op:
        td, tm = t // unit, t % unit
        if sd == td:
            print(sum(l[sd][1][sm:tm + 1]))
        else:
            unit_sum = sum((tup[0] for tup in l[sd + 1:td]))
            s_sum = sum(l[sd][1][sm:])
            t_sum = sum(l[td][1][:tm + 1])
            print(unit_sum + s_sum + t_sum)
    else:
        tup = l[sd]
        tup[1][sm] += t
        l[sd] = (tup[0] + t, tup[1])
    q -= 1

Fixed submission:
from math import sqrt, ceil

n, q = map(int, input().split())
unit = ceil(sqrt(n))

l = [(0, [0] * unit) for _ in range(unit)]

while q:
    op, s, t = map(int, input().split())
    s -= 1
    sd, sm = s // unit, s % unit
    if op:
        t -= 1
        td, tm = t // unit, t % unit
        if sd == td:
            print(sum(l[sd][1][sm:tm + 1]))
        else:
            unit_sum = sum((tup[0] for tup in l[sd + 1:td]))
            s_sum = sum(l[sd][1][sm:])
            t_sum = sum(l[td][1][:tm + 1])
            print(unit_sum + s_sum + t_sum)
    else:
        tup = l[sd]
        tup[1][sm] += t
        l[sd] = (tup[0] + t, tup[1])
    q -= 1
Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split())
a = [2 ** 31 - 1] * n
for _ in range(q):
    query = input().split()
    if query[0] == '0':
        s, t, x = map(int, query[1:])
        a[s:t + 1] = [x] * (t - s + 1)
    else:
        print(a[int(query[1])])

Fixed submission:
from math import log2, ceil


class SegmentTree:
    def __init__(self, n):
        tn = 2 ** ceil(log2(n))
        self.a = [2 ** 31 - 1] * (tn * 2)

    def find(self, c, l, r, i):
        if self.a[c] != -1:
            return self.a[c]
        mid = (l + r) // 2
        if i <= mid:
            return self.find(c * 2, l, mid, i)
        else:
            return self.find(c * 2 + 1, mid + 1, r, i)

    def update(self, c, l, r, s, t, x):
        if l == s and r == t:
            self.a[c] = x
            return
        cv = self.a[c]
        if cv != -1:
            self.a[c * 2] = self.a[c * 2 + 1] = cv
            self.a[c] = -1
        mid = (l + r) // 2
        if t <= mid:
            self.update(c * 2, l, mid, s, t, x)
        elif s > mid:
            self.update(c * 2 + 1, mid + 1, r, s, t, x)
        else:
            self.update(c * 2, l, mid, s, mid, x)
            self.update(c * 2 + 1, mid + 1, r, mid + 1, t, x)


n, q = map(int, input().split())
st = SegmentTree(n)
for _ in range(q):
    query = input().split()
    if query[0] == '0':
        s, t, x = map(int, query[1:])
        st.update(1, 0, n - 1, s, t, x)
    else:
        print(st.find(1, 0, n - 1, int(query[1])))
Buggy python submission (verdict: Time Limit Exceeded):
from math import log2, ceil


class SegmentTree:
    def __init__(self, n):
        tn = 2 ** ceil(log2(n))
        self.a = [0] * (tn * 2)
        self.tn = tn

    def get_sum(self, s, t):
        return self.__get_sum(1, 1, self.tn, s, t)

    def __get_sum(self, c, l, r, s, t):
        if l == r:
            return self.a[c]
        mid = (l + r) // 2
        if t <= mid:
            return self.a[c] * (t - s + 1) + self.__get_sum(c * 2, l, mid, s, t)
        elif s > mid:
            return self.a[c] * (t - s + 1) + self.__get_sum(c * 2 + 1, mid + 1, r, s, t)
        else:
            return self.a[c] * (t - s + 1) + \
                   self.__get_sum(c * 2, l, mid, s, mid) + \
                   self.__get_sum(c * 2 + 1, mid + 1, r, mid + 1, t)

    def add(self, s, t, x):
        self.__add(1, 1, self.tn, s, t, x)

    def __add(self, c, l, r, s, t, x):

        if l == s and r == t:
            self.a[c] += x
            return

        mid = (l + r) // 2
        if t <= mid:
            self.__add(c * 2, l, mid, s, t, x)
        elif s > mid:
            self.__add(c * 2 + 1, mid + 1, r, s, t, x)
        else:
            self.__add(c * 2, l, mid, s, mid, x)
            self.__add(c * 2 + 1, mid + 1, r, mid + 1, t, x)


n, q = map(int, input().split())
st = SegmentTree(n)
buf = []
for _ in range(q):
    query = input().split()
    if query[0] == '0':
        st.add(*map(int, query[1:]))
    else:
        buf.append(st.get_sum(*map(int, query[1:])))
print('\n'.join(map(str, buf)))

Fixed submission:
class BinaryIndexedTree:
    def __init__(self, n):
        self.a = [0] * (n + 1)
        self.n = n

    def add(self, i, x):
        while i <= self.n:
            self.a[i] += x
            i += i & -i

    def sum(self, i):
        ret = 0
        while i > 0:
            ret += self.a[i]
            i -= i & -i
        return ret


class Solve:
    def __init__(self, n):
        self.n = n
        self.p = BinaryIndexedTree(n + 1)
        self.q = BinaryIndexedTree(n + 1)

    def add(self, s, t, x):
        t += 1
        self.p.add(s, -x * s)
        self.p.add(t, x * t)
        self.q.add(s, x)
        self.q.add(t, -x)

    def get_sum(self, s, t):
        t += 1
        return self.p.sum(t) + self.q.sum(t) * t - \
               self.p.sum(s) - self.q.sum(s) * s

    def dump(self):
        print(*(self.get_sum(i, i) for i in range(self.n + 1)))


n, q = map(int, input().split())
st = Solve(n)
buf = []
for _ in range(q):
    query = input().split()
    if query[0] == '0':
        st.add(*map(int, query[1:]))
    else:
        buf.append(st.get_sum(*map(int, query[1:])))
print('\n'.join(map(str, buf)))
Buggy python submission (verdict: Runtime Error):
from itertools import accumulate

n = int(input())
a = [[0] * 10 for _ in range(10)]
for x1, y1, x2, y2 in (map(int, input().split()) for _ in range(n)):
    a[x1][y1] += 1
    a[x1][y2] -= 1
    a[x2][y1] -= 1
    a[x2][y2] += 1
print(max(map(max, map(accumulate, zip(*map(accumulate, a))))))

Fixed submission:
from itertools import accumulate

n = int(input())
a = [[0] * 1001 for _ in range(1001)]
for x1, y1, x2, y2 in (map(int, input().split()) for _ in range(n)):
    a[x1][y1] += 1
    a[x1][y2] -= 1
    a[x2][y1] -= 1
    a[x2][y2] += 1
print(max(map(max, map(accumulate, zip(*map(accumulate, a))))))
Buggy python submission (verdict: Runtime Error):
import heapq

nv, ne = map(int, input().split())

edges = [set() for _ in range(nv)]
while ne:
    s, t, w = map(int, input().split())
    edges[s].add((w, t))
    edges[t].add((w, s))
    ne -= 1

cost = 0
queue = list(edges[0])
heapq.heapify(queue)
visited = [False] * nv
visited[0] = True

while True:
    edge_cost, t = heapq.heappop(queue)
    if visited[t]:
        continue
    visited[t] = True
    cost += edge_cost
    if all(visited):
        break
    for e in edges[t]:
        if not visited[e[1]]:
            heapq.heappush(queue, e)

print(cost)

Fixed submission:
import heapq

nv, ne = map(int, input().split())

edges = [set() for _ in range(nv)]
while ne:
    s, t, w = map(int, input().split())
    edges[s].add((w, t))
    edges[t].add((w, s))
    ne -= 1

cost = 0
queue = list(edges[0])
heapq.heapify(queue)
visited = [False] * nv
visited[0] = True

while queue:
    edge_cost, t = heapq.heappop(queue)
    if visited[t]:
        continue
    visited[t] = True
    cost += edge_cost
    if all(visited):
        break
    for e in edges[t]:
        if not visited[e[1]]:
            heapq.heappush(queue, e)

print(cost)