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

Fixed submission:
import sys


sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    S = input()
    N = len(S)
    dp = [[0] * 13 for _ in range(N)]

    if S[-1] == "?":
        for i in range(10):
            dp[0][i] = 1
    else:
        dp[0][int(S[-1])] = 1

    tenfactor = 1
    for i in range(1, N):
        tenfactor *= 10
        tenfactor %= 13

        if S[N - i - 1] != "?":
            for k in range(13):
                j = int(S[N - i - 1])
                idx = (k + j * tenfactor) % 13
                dp[i][idx] += dp[i - 1][k]
                dp[i][idx] %= MOD
        else:
            for j in range(10):
                for k in range(13):
                    idx = (k + j * tenfactor) % 13
                    dp[i][idx] += dp[i - 1][k]
                    dp[i][idx] %= MOD

    print(dp[-1][5])


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys


sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    S = input()
    N = len(S)
    dp = [[0] * 13 for _ in range(N)]

    if S[0] == "?":
        for i in range(10):
            dp[0][i] = 1
    else:
        dp[0][int(S[-1])] = 1

    for i in range(1, N):
        if S[N - i - 1] != "?":
            for k in range(13):
                j = int(S[N - i - 1])
                idx = (k + j * (10 ** i)) % 13
                dp[i][idx] += dp[i - 1][k]
                dp[i][idx] %= MOD
        else:
            for j in range(10):
                for k in range(13):
                    idx = (k + j * (10 ** i)) % 13
                    dp[i][idx] += dp[i - 1][k]
                    dp[i][idx] %= MOD

    print(dp[-1][5])


if __name__ == "__main__":
    main()
Problem p02972 (python)

Fixed submission:
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


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

    return divisors


def main():
    N = int(input())
    A = list(map(int, input().split()))
    S = [0] * (N)
    B = []
    for i in range(N - 1, -1, -1):
        S[i] %= 2
        if A[i] != S[i]:
            S[i] += 1
            B.append(i + 1)
            ds = make_divisors(i + 1)
            for d in ds:
                S[d - 1] += 1
        else:
            continue

    print(len(B))
    print(*B, sep=" ")


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


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

    return divisors


def main():
    N = int(input())
    A = list(map(int, input().split()))
    S = [0] * (N)
    B = []
    for i in range(N - 1, -1, -1):
        S[i] %= 2
        if A[i] != S[i]:
            S[i] += 1
            B.append(i + 1)
            ds = make_divisors(i + 1)
            for d in ds:
                S[d] += 1
        else:
            continue

    print(len(B))
    print(*B, sep=" ")


if __name__ == "__main__":
    main()
Problem p02997 (python)

Fixed submission:
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N, K = map(int, input().split())
    if (N - 1) * (N - 2) // 2 < K:
        print(-1)
        return
    else:
        edges = set()
        for i in range(2, N + 1):
            edges.add((1, i))

        cnt = (N - 1) * (N - 2) // 2
        for i in range(1, N):
            for j in range(i, N + 1):
                if i == j:
                    continue

                if cnt == K:
                    print(len(edges))
                    for a, b in edges:
                        print(a, b)
                    return

                if (i, j) not in edges:
                    cnt -= 1
                    edges.add((i, j))

        print(len(edges))
        for a, b in edges:
            print(a, b)
        return


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N, K = map(int, input().split())
    if N < K:
        print(-1)
    elif K == N:
        edges = [(N, 1)]
        for i in range(1, N):
            edges.append((i, i + 1))
    elif K == N - 1:
        edges = [(N, 1)]
        for i in range(1, N):
            edges.append((i, i + 1))
        edges.append((2, N))
    else:
        cnt = 0
        edges = []
        for i in range(1, N + 1):
            for j in range(i + 1, N + 1):
                if i == 1 and cnt < K:
                    cnt += 1
                    continue
                else:
                    edges.append((i, j))

    print(len(edges))
    for a, b in edges:
        print(a, b)


if __name__ == "__main__":
    main()
Problem p03006 (python)

Fixed submission:
def main():
    N = int(input())
    if N == 1:
        print(1)
        return
    xy = []
    for _ in range(N):
        x, y = map(int, input().split())
        xy.append((x, y))

    d = {}
    for i in range(N):
        tmp = set()
        for j in range(N):
            if i == j:
                continue
            dx = xy[i][0] - xy[j][0]
            dy = xy[i][1] - xy[j][1]

            if (dx, dy) in tmp:
                continue
            if (dx, dy) not in d:
                d[(dx, dy)] = 1
            else:
                d[(dx, dy)] += 1
            tmp.add((dx, dy))

    d = sorted(d.items(), key=lambda x: x[1], reverse=True)
    cnt = d[0][1]
    print(N - cnt)

main()

Buggy submission (verdict: Runtime Error):
import sys
from collections import defaultdict

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    X = []
    Y = []
    for _ in range(N):
        x, y = map(int, input().split())
        X.append(x)
        Y.append(y)

    cnt = defaultdict(int)
    for i in range(N):
        for j in range(N):
            if i == j:
                continue

            dx = X[j] - X[i]
            dy = Y[j] - Y[i]
            cnt[(dx, dy)] += 1

    cnt = sorted(cnt.items(), key=lambda x: x[1], reverse=True)
    ans = N - cnt[0][1]
    print(ans)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
N = int(input())
xy = []
for _ in range(N):
    x, y = map(int, input().split())
    xy.append((x, y))

d = {}
for i in range(N):
    tmp = set()
    for j in range(N):
        if i == j:
            continue
        dx = xy[i][0] - xy[j][0]
        dy = xy[i][1] - xy[j][1]

        if (dx, dy) in tmp:
            continue
        if (dx, dy) not in d:
            d[(dx, dy)] = 1
        else:
            d[(dx, dy)] += 1
        tmp.add((dx, dy))

d = sorted(d.items(), key=lambda x: x[1], reverse=True)
cnt = d[0][1]
print(N - cnt)
Problem p03007 (python)

Fixed submission:
import sys
import copy
from collections import deque

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort()

    if A[0] >= 0:
        ans = sum(A[1:]) - A[0]
        plus = copy.deepcopy(A[1:])
        minus = [A[0]]
    elif A[-1] <= 0:
        ans = -sum(A[:-1]) + A[-1]
        plus = [A[-1]]
        minus = copy.deepcopy(A[:-1])
    else:
        plus = []
        minus = []
        ans = 0
        for a in A:
            if a >= 0:
                ans += a
                plus.append(a)
            else:
                ans -= a
                minus.append(a)

    plus = deque(plus)
    minus = deque(minus)
    print(ans)

    # minusをまず一つにする
    while len(minus) > 1:
        p = plus.pop()
        m = minus.pop()
        print(p, m)
        plus.append(p - m)

    # minusが1つになったあとの処理
    p = plus.pop()
    m = minus.pop()
    if len(plus) == 0:
        print(p, m)
        return

    print(m, p)
    cur = m - p
    while len(plus) > 1:
        p = plus.pop()
        print(cur, p)
        cur -= p
    if len(plus) != 0:
        p = plus.pop()
        print(p, cur)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
import copy
from collections import deque

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort()

    if A[0] >= 0:
        ans = sum(A[1:]) - A[0]
        plus = copy.deepcopy(A[1:])
        minus = [A[0]]
    elif A[-1] <= 0:
        ans = -sum(A[:-1]) + A[-1]
        plus = [A[-1]]
        minus = copy.deepcopy(A[:-1])
    else:
        plus = []
        minus = []
        ans = 0
        for a in A:
            if a >= 0:
                ans += a
                plus.append(a)
            else:
                ans -= a
                minus.append(a)

    plus = deque(plus)
    minus = deque(minus)
    print(ans)

    # minusをまず一つにする
    while len(minus) > 1:
        p = plus.pop()
        m = minus.pop()
        print(p, m)
        plus.append(p - m)

    # minusが1つになったあとの処理
    p = plus.pop()
    m = minus.pop()
    print(m, p)
    cur = m - p
    while len(plus) > 1:
        p = plus.pop()
        print(cur, p)
        cur -= p
    p = plus.pop()
    print(p, cur)


if __name__ == "__main__":
    main()
Problem p03032 (python)

Fixed submission:
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))

    ans = 0
    for n_pick in range(min(N, K) + 1):
        for left in range(n_pick + 1):
            right = n_pick - left
            tmp = []
            for i in range(left):
                tmp.append(V[i])
            for i in range(right):
                tmp.append(V[N - i - 1])
            tmp.sort()
            s = sum(tmp)
            n_return = min(K - n_pick, len(tmp))
            for i in range(n_return):
                if tmp[i] < 0:
                    s -= tmp[i]
                else:
                    break
            if s > ans:
                ans = s

    print(ans)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))

    ans = 0
    for a in range(min(N, K) + 1):
        for b in range(K - a):
            if a + b > N:
                continue
            tmp = []
            for i in range(a):
                tmp.append(V[i])
            for i in range(b):
                tmp.append(V[N - i - 1])
            rest = K - a - b
            tmp.sort()
            s = sum(tmp)
            for r in range(rest):
                if tmp[r] < 0:
                    s -= r
                else:
                    break
            if s > ans:
                ans = s

    print(ans)


if __name__ == "__main__":
    main()
Problem p03061 (python)

Fixed submission:
import sys
from fractions import gcd


sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    A = list(map(int, input().split()))
    # i番目から左の全ての要素のGCD
    gcd_from_left = [None] * N
    gcd_from_left[0] = A[0]
    # i番目から右の全ての要素のGCD
    gcd_from_right = [None] * N
    gcd_from_right[-1] = A[-1]

    for i in range(1, N):
        gcd_from_left[i] = gcd(A[i], gcd_from_left[i - 1])

    for i in range(N - 2, -1, -1):
        gcd_from_right[i] = gcd(A[i], gcd_from_right[i + 1])

    gcd_from_right.sort()

    # 要素iを取り除いたときの最大公約数
    ans = max(gcd_from_left[-2], gcd_from_right[1])
    for i in range(1, N - 1):
        tmp = gcd(gcd_from_right[i + 1], gcd_from_left[i - 1])
        if tmp > ans:
            ans = tmp

    print(ans)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
import math

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    A = list(map(int, input().split()))
    # i番目から左の全ての要素のGCD
    gcd_from_left = [None] * N
    gcd_from_left[0] = A[0]
    # i番目から右の全ての要素のGCD
    gcd_from_right = [None] * N
    gcd_from_right[-1] = A[-1]

    for i in range(1, N):
        gcd_from_left[i] = math.gcd(A[i], gcd_from_left[i - 1])

    for i in range(N - 2, -1, -1):
        gcd_from_right[i] = math.gcd(A[i], gcd_from_right[i + 1])

    gcd_from_right.sort()

    # 要素iを取り除いたときの最大公約数
    ans = max(gcd_from_left[-2], gcd_from_right[1])
    for i in range(1, N - 1):
        tmp = math.gcd(gcd_from_right[i + 1], gcd_from_left[i - 1])
        if tmp > ans:
            ans = tmp

    print(ans)


if __name__ == "__main__":
    main()
Problem p03069 (python)

Fixed submission:
N = int(input())
S = input()

white = [0] * (N + 1)
for i in range(N):
    if S[i] == ".":
        white[i + 1] = white[i] + 1
    else:
        white[i + 1] = white[i]

ans = N
for i in range(N + 1):
    tmp = (i - white[i]) + (white[-1] - white[i])
    if tmp < ans:
        ans = tmp

print(ans)

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

for i in range(N - 1, -1, -1):
    if S[i] == ".":
        right_white = i
        break

print(min(S.count("."), S[:right_white].count("#")))
Problem p03096 (python)

Fixed submission:
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    C = [int(input()) for _ in range(N)] + [-1]
    C = [C[i] for i in range(N) if C[i] != C[i + 1]]

    N = len(C)
    # dp[i] := i個までの色の塗り方の総和
    dp = [0] * N
    dp[0] = 1
    used = [-1] * (max(C) + 1)
    used[C[0]] = 0

    for i in range(1, N):
        if used[C[i]] == -1:
            dp[i] = dp[i - 1]
        else:
            dp[i] = dp[i - 1] + dp[used[C[i]]]

        dp[i] %= MOD
        used[C[i]] = i

    print(dp[-1])


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np
from collections import defaultdict

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    C = [int(input()) for _ in range(N)] + [None]
    C = [C[i] for i in range(N) if C[i] != C[i + 1]]

    colors = defaultdict(list)
    for i, c in enumerate(C):
        colors[c].append(i)

    N = len(C)
    # dp[i] := i個までの色の塗り方の総和
    dp = [0] * (N + 1)
    dp[0] = 1
    for i in range(N):
        # i個目を塗らない
        dp[i + 1] += dp[i]
        dp[i + 1] %= MOD
        for j in colors[C[i]]:
            if j <= i:
                continue

            # ぬる
            dp[j + 1] += dp[i]
            dp[j + 1] %= MOD

    print(dp[-1])


if __name__ == "__main__":
    main()
Problem p03150 (python)

Fixed submission:
S = input()

target = "keyence"
N = len(target)

for i in range(8):
    s = S[:i] + S[len(S) - N + i :]
    if s == target:
        print("YES")
        break
else:
    print("NO")

Buggy submission (verdict: Runtime Error):
S = input()

target = "keyence"
now = 0
start = None
end = None
for i in range(len(S)):
    if target[now] == S[i]:
        now += 1
        if start is not None and end is None:
            end = i
    elif start is not None:
        continue
    else:
        start = i

if start is None:
    pred = S
elif end is None:
    pred = S[:start]
else:
    pred = S[:start] + S[end:]

if pred == target:
    print("YES")
else:
    print("NO")
Problem p03328 (python)

Fixed submission:
H = [1]
for i in range(2, 1000):
    H.append(H[-1] + i)

a, b = map(int, input().split())

for i in range(998):
    if H[i] - a == H[i + 1] - b:
        print(H[i] - a)
        break

Buggy submission (verdict: Runtime Error):
def func(x):
    if x == 1:
        return 1
    else:
        return x + func(x - 1)


a, b = map(int, input().split())

for i in range(1, 999):
    if func(i) - a == func(i + 1) - b:
        print(func(i) - a)
        break
Problem p03380 (python)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
a.sort()
M = max(a)
m = M / 2

diff = 10 ** 9
r = None
for i in range(n):
    if a[i] == M:
        continue
    else:
        if abs(m - a[i]) < diff:
            diff = abs(m - a[i])
            r = a[i]

print(M, r)

Buggy submission (verdict: Runtime Error):
import sys
from bisect import bisect_left

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort()
    MAX = max(A)
    A = A[:-1]
    target = MAX // 2
    idx = bisect_left(A, target)

    if idx == 0:
        print(MAX, A[0])
    else:
        if abs(target - A[idx]) < abs(target - A[idx - 1]):
            print(MAX, A[idx])
        else:
            print(MAX, A[idx - 1])


if __name__ == "__main__":
    main()
Problem p03393 (python)

Fixed submission:
def main():
    S = input()
    S_set = set(list(S))
    C_set = set([chr(ord("a") + i) for i in range(26)])

    if len(S_set) == len(C_set):
        S = list(S)
        p = None
        for i in range(len(S) - 2, -1, -1):
            if S[i] < S[i + 1]:
                p = i
                break
        else:
            print(-1)
            return

        ans = S[:p].copy()
        rest = C_set - set(S[: p + 1])
        rest = sorted(rest)
        for r in rest:
            if S[p] < r:
                ans.append(r)
                break
        print(*ans, sep="")
    else:
        diff = C_set - S_set
        diff = sorted(diff)
        print(S + diff[0])
        return


main()

Buggy submission (verdict: Runtime Error):
S = input()
S_set = set(list(S))
C_set = set([chr(ord("a") + i) for i in range(26)])

if len(S_set) == len(C_set):
    S = list(S)
    p = None
    for i in range(len(S) - 2, -1, -1):
        if S[i] < S[i + 1]:
            p = i
            break
    else:
        print(-1)
    ans = S[:p].copy()
    rest = C_set - set(S[: p + 1])
    rest = sorted(rest)
    for r in rest:
        if S[p] < r:
            ans.append(r)
            break
    print(*ans, sep="")
else:
    diff = C_set - S_set
    diff = sorted(diff)
    print(S + diff[0])

Buggy submission (verdict: Runtime Error):
S = input()
S_set = set(list(S))
C_set = set([chr(ord("a") + i) for i in range(26)])

if len(S_set) == len(C_set):
    S = list(S)
    p = None
    for i in range(len(S) - 2, -1, -1):
        if S[i] < S[i + 1]:
            p = i
            break

    if p == 0:
        print(-1)
    else:
        ans = S[:p].copy()
        rest = C_set - set(S[: p + 1])
        rest = sorted(rest)
        ans.append(rest[0])
        print(*ans, sep="")
else:
    diff = C_set - S_set
    diff = sorted(diff)
    print(S + diff[0])
Problem p03504 (python)

Fixed submission:
from collections import defaultdict
import numpy as np
import sys


sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N, C = map(int, input().split())
    MAX = 10 ** 5 + 2
    vals = [0] * MAX
    D = defaultdict(list)
    for _ in range(N):
        s, t, c = map(int, input().split())
        D[c].append((s, t))

    for k, v in D.items():
        prev = -1
        v.sort(key=lambda x: x[0])
        for s, t in v:
            if prev == s:
                vals[prev + 1] += 1
                vals[t + 1] -= 1
            else:
                vals[s] += 1
                vals[t + 1] -= 1
            prev = t

    cumsum = np.cumsum(vals)
    ans = max(cumsum)
    print(ans)


if __name__ == "__main__":
    main()

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


sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N, C = map(int, input().split())
    MAX = 10 ** 5 + 1
    vals = [0] * MAX
    for _ in range(N):
        s, t, c = map(int, input().split())
        vals[s] += 1
        vals[t + 1] -= 1
    cumsum = np.cumsum(vals)
    ans = max(cumsum)
    print(ans)


if __name__ == "__main__":
    main()
Problem p03583 (python)

Fixed submission:
def solve():
    N = int(input())
    M = 3501

    for h in range(1, M):
        for n in range(h, M):
            numerator = N * h * n
            denominator = 4 * n * h - N * h - N * n
            if numerator <= 0 or denominator <= 0:
                continue
            if numerator % denominator == 0:
                return h, n, numerator // denominator


h, n, w = solve()
print(h, n, w)

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def solve(N):
    N = int(input())
    M = 3501

    for h in range(1, M):
        for n in range(h, M):
            denominator = 4 * n * h - N * h - N * n
            if denominator <= 0:
                continue
            numerator = N * h * n

            if numerator % denominator == 0:
                return h, n, numerator // denominator


def main():
    N = int(input())
    h, n, w = solve(N)
    print(h, n, w)


if __name__ == "__main__":
    main()
Problem p03627 (python)

Fixed submission:
from collections import Counter

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

c = Counter(A)
hen = [key for key, val in c.items() if val > 1]

l = max(hen) if hen else 0
if l != 0:
    if c[l] > 3:
        s = l
    else:
        hen.remove(l)
        s = max(hen) if hen else 0
else:
    s = 0

print(l * s)

Buggy submission (verdict: Runtime Error):
from collections import Counter

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

c = Counter(A)
hen = [(key, val) for key, val in c.items() if val > 1]

l = max(hen) if hen else 0
if l != 0:
    if c[l] > 3:
        s = l
    else:
        hen.remove(l)
        s = max(hen) if hen else 0
else:
    s = 0

print(l * s)
Problem p03944 (python)

Fixed submission:
W, H, N = map(int, input().split())
min_H = 0
min_W = 0
max_H = H
max_W = W

for _ in range(N):
    x, y, a = map(int, input().split())

    if a == 1:
        min_W = max(x, min_W)
    elif a == 2:
        max_W = min(x, max_W)
    elif a == 3:
        min_H = max(y, min_H)
    else:
        max_H = min(y, max_H)

if min_W > max_W or min_H > max_H:
    print(0)
else:
    print((max_H - min_H) * (max_W - min_W))

Buggy submission (verdict: Runtime Error):
W, H, N = map(int, input().split())
min_H = 0
min_W = 0
max_H = H
max_W = W

for _ in range(N):
    x, y, a = map(int, input().split())

    if a == 1:
        min_W = x
    elif a == 2:
        max_W = x
    elif a == 3:
        min_H = y
    else:
        max_H = y

print(max(((max_H - min_H) * (max_W - min_W)), 0)