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

Fixed submission:
X = int(input())

for a in range(-500, 500):
  for b in range(-500, 500):
    Y = a ** 5 - b ** 5
    if Y == X:
      A = a
      B = b
      break
      
print(A, B)

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

c = 0
for a in range(-118, 119):
  for b in range(-118, 119):
    Y = a ** 5 - b ** 5
    if Y == X:
      A = a
      B = b
      c = 1
      break
  if c == 1:
    break
print(A, B)

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

for a in range(-118, 119):
  for b in range(-118, 119):
    Y = a ** 5 - b ** 5
    if Y == X:
      A = a
      B = b
      break
    else:
      continue
      
print(A, B)

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

for a in range(-118, 119):
  for b in range(-118, 119):
    Y = a ** 5 - b ** 5
    if Y == X:
      A = a
      B = b
      break
      
print(A, B)
Problem p02691 (python)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
AM = []
AP = []
AMlist = [0] * N
APlist = [0] * N

for i in range(N):
  plus = i+1 + A[i]
  AP.append(plus)  
  if AP[i] <= N:
    APlist[AP[i]-1] += 1
  minus = i+1 - A[i]
  AM.append(minus)
  if AM[i] > 0 and AM[i] <= N:
    AMlist[AM[i]-1] += 1
    
ans = 0

for j in range(N):
  ans += APlist[j] * AMlist[j]

print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
for i in range(N):
  plus = i+1 + A[i]
  AP.append(plus)
  minus = i+1 - A[i]
  AM.append(minus)
  
print(AP)
print(AM)
ans = 0 

for j in range(N):
  ans += AP.count(j) * AM.count(j)

print(ans)
Problem p03424 (python)

Fixed submission:
N = int(input())
arare = input().split()
y = 0
for i in range(len(arare)):
  if arare[i] == "Y":
    y += 1
    break
if y == 1:
  print("Four")
else:
  print("Three")

Buggy submission (verdict: Runtime Error):
N = int(input())
arare = input().split()
y = 0
for i in arare:
  if arare[i] == "Y":
    y += 1
    break
if y == 1:
  print("Four")
else:
  print("Three")

Buggy submission (verdict: Runtime Error):
N = int(input())
arare = input().split()
y = 0
for i in arare:
  if arere[i] == "Y":
    y += 1
    break
if y == 1:
  print("Four")
else:
  print("Three")
Problem p03471 (python)

Fixed submission:
N, Y = map(int, input().split())
ans = False
for i in range(N+1):
  for j in range(N+1-i):
    k = N-i-j
    if 10000*i + 5000*j + 1000*k == Y:
      ans = True
      print(i, j, k)
      break
  if ans == True:
    break
      
if ans == False:
  print(-1, -1, -1)

Buggy submission (verdict: Runtime Error):
N, Y = map(int, input().split())
ans = False
for i in range(N+1):
  for j in range(N+1):
    k = N-i-j
    if 10000*i + 5000*j + 1000*k == Y and z>=0:
      ans = True
      print(i, j, k)
    break
      
if ans == False:
  print(-1, -1, -1)
Problem p03493 (python)

Fixed submission:
s_str = str(input())
s = [int(i) for i in s_str]
print(sum(s))

Buggy submission (verdict: Runtime Error):
a, b, c = map(int, input().split())
print(a + b + c)
Problem p03378 (python)

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

for i in range(m):
    if P[i]  > x:
        a += 1
    else:
        b += 1
print(min(a, b))

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

for i in range(m):
    if P[i]  > x:
        a += 1
    else:
        b += 1
print(min(a, b))
Problem p03404 (python)

Fixed submission:
white, black = map(int,input().split())
white -= 1
black -= 1

grid = [['#' for i in range(100)] for j in range(100)]
for i in range(1, 99):
    for j in range(50, 99):
        grid[i][j] = "."

a = 3
b = 3
while True:
    if white == 0:
        break
    grid[a][b] = "."
    white -= 1
    a += 2
    if a > 93:
        a = 3
        b += 2
    
 
c = 3
d = 53
while True:
    if black == 0:
        break
    grid[c][d] = "#"
    black -= 1
    c += 2
    if c > 93:
        c = 3
        d += 2
    

print(100, 100)
for i in grid:
    print("".join(i))

Buggy submission (verdict: Runtime Error):
white, black = map(int,input().split())
white -= 1
black -= 1

grid = [['#' for i in range(100)] for j in range(100)]
for i in range(1, 99):
    for j in range(50, 99):
        grid[i][j] = "."

a = 3
b = 3
while True:
    grid[a][b] = "."
    white -= 1
    a += 2
    if a > 93:
        a = 3
        b += 2
    if white == 0:
        break
 
c = 3
d = 53
while True:
    grid[c][d] = "#"
    black -= 1
    c += 2
    if c > 93:
        c = 3
        d += 2
    if black == 0:
        break

print(100, 100)
for i in grid:
    print("".join(i))
Problem p03438 (python)

Fixed submission:
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
p = sum(P)
q = sum(Q)
if P == Q:
    print("Yes")
else:
    m = 0
    l = 0
    for i in range(N):
        if Q[i] - P[i] > 0:
            if (Q[i] - P[i]) % 2 == 1:
                #p += (P[i] - Q[i]) * 2
                m += (Q[i] - P[i] + 1) // 2
                l += 1
            else:
                m += (Q[i] - P[i]) // 2
        elif Q[i] - P[i] < 0:
            #q += (Q[i] - P[i])
            l += (P[i] - Q[i])
    if m >= l:
        print("Yes")
    else:
        print("No")

Buggy submission (verdict: Runtime Error):
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
p = sum(P)
q = sum(Q)
m = 0
l = 0
for i in range(N):
    if P[i] - Q[i] > 0:
        if (P[i] - Q[i]) % 2 == 1:
            #p += (P[i] - Q[i]) * 2
            m += (P[i] - Q[i] + 1) // 2
            l += 1
        else:
            #p += (P[i] - Q[i])
            m += (P[i] - Q[i]) // 2
    elif P[i] - Q[i] < 0:
        #q += (Q[i] - P[i])
        l += (Q[i] - P[i])

a = p + m * 2 + l
b = q + m + l


if a > b:
    print("No")
elif a == b:
    print("Yes")
else:
    if l % m == 0:
        print("Yes")
    else:
        print("No")

Buggy submission (verdict: Runtime Error):
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
p = sum(P)
q = sum(Q)
m = 0
l = 0
for i in range(N):
    if P[i] - Q[i] > 0:
        if (P[i] - Q[i]) % 2 == 1:
            #p += (P[i] - Q[i]) * 2
            m += (P[i] - Q[i] + 1) // 2
            l += 1
        else:
            #p += (P[i] - Q[i])
            m += (P[i] - Q[i]) // 2
    elif P[i] - Q[i] < 0:
        #q += (Q[i] - P[i])
        l += (Q[i] - P[i])

a = p + m * 2
b = q + m

if a > b:
    print("No")
elif a == b:
    print("Yes")
else:
    if l % m == 0:
        print("Yes")
    else:
        print("No")
Problem p03480 (python)

Fixed submission:
import sys

S = input()
center = len(S) // 2

if len(S) % 2 == 1:
    b = S[center]
    for i in range(1, center + 1):
        if S[center - i] != b or S[center + i] != b:
            print(center + i)
            sys.exit()
else:
    b = S[center - 1]
    for i in range(center):
        if S[center - i - 1] != b or S[center + i] != b:
            print(center + i)
            sys.exit()

print(len(S))

Buggy submission (verdict: Runtime Error):
S = input()
N = int(input())
ans = S.count("0")
if ans > N:
    ans = N
print(ans)
Problem p03486 (python)

Fixed submission:
s = input()
t = input()
p = []
q = []
m = len(s)
n = len(t)
if m >= n:
    ans = "No"
else:
    ans = "Yes"
for i in range(m):
    p.append(s[i])
p.sort()
for j in range(n):
    q.append(t[j])
q.sort(reverse=True)
for k in range(min(m,n)):
    if ord(p[k]) < ord(q[k]):
        ans = "Yes"
        break
    elif ord(p[k]) > ord(q[k]):
        ans = "No"
        break
print(ans)

Buggy submission (verdict: Runtime Error):
s = input()
t = input()
p = []
q = []
m = len(s)
n = len(t)
if m >= n:
    ans = "No"
else:
    ans = "Yes"
for i in range(m):
    p.append(s[i])
p.sort()
for j in range(n):
    q.append(t[j])

for k in range(min(m,n)+1):
    if ord(p[k]) < ord(q[k]):
        ans = "Yes"
        break
    elif ord(p[k]) > ord(q[k]):
        ans = "No"
        break
print(ans)
Problem p03543 (python)

Fixed submission:
N = input()
if N[0] == N[1] == N[2] or N[1] == N[2] == N[3]:
  print("Yes")
else:
  print("No")

Buggy submission (verdict: Runtime Error):
    N = input()
    if N[0] == N[1] == N[2] or N[1] == N[2] == N[3]:
        print("Yes")
    else:
        print("No")

Buggy submission (verdict: Runtime Error):
N = input()
if N[0] == N[1] == N[2] or N[1] == N[2] == N[3]:
    print("Yes")
else:
    print("No)
Problem p03547 (python)

Fixed submission:
a,b = map(str,input().split())
A = 1
B = 2
C = 3
D = 4
E = 5
F = 6
     
if a < b:
    print("<")
elif a == b:
    print("=")
else:
    print(">")

Buggy submission (verdict: Runtime Error):
a,b = map(str,input().split())
A = 1
B = 2
C = 3
D = 4
E = 5
F = 6

if a < b:
    print("<")
elif a = b:
    print("=")
else:
    print(">")
Problem p03552 (python)

Fixed submission:
N, Z, W = map(int,input().split())
p = list(map(int,input().split()))
a = abs(p[N-2] - p[N-1])
b = abs(p[N-1] - W)
print(max(a, b))

Buggy submission (verdict: Runtime Error):
    N, Z, W = map(int,input().split())
    p = list(map(int,input().split()))
    a = abs(p[N-2] - p[N-1])
    b = abs(p[N-1] - W)
    print(max(a, b))

Buggy submission (verdict: Runtime Error):
N, Z, W = map(int,input().split())
p = list(map(int,input().split()))
a = abs(p[1] - p[2])
b = abs(p[2] - W)
print(max(a, b))
Problem p03565 (python)

Fixed submission:
S = input()
T = input()
 
for i in range(len(S) - len(T), -1, -1):
    for j in range(len(T)):
        if S[i + j] != '?' and S[i + j] != T[j]:
            break
    else:
        print((S[:i] + T + S[i + len(T):]).replace('?', 'a'))
        break
else:
    print('UNRESTORABLE')

Buggy submission (verdict: Runtime Error):
import sys
s = input()
T=input()
p = 0
for i in range(len(s)-len(T)+1,0,-1):
  for j in range(len(T)):
    if T[j]!=s[i-1+j] and "?"!= s[i-1+j] :
      p = 1
      break
if p == 0:
  s=s[:i-1]+T+s[i+len(T)-1:]
  s = s.replace('?', 'a')
  print(s)
sys.exit()
if p == 1:
  print("UNRESTORABLE")
Problem p03573 (python)

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

if A == B:
 	print(C)
elif B == C:
 	print(A)
else:
    print(B)

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
S = [list(input()) for i in range(h)]
for i in range(h):
    for j in range(w):
        if S[i][j] == '#':
            continue
        cnt = 0
        for dx in [-1, 0, 1]:
            for dy in [-1, 0, 1]:
                if not 0 <= i+dy < h or not 0 <= j+dx < w:
                    continue
                if S[i+dy][j+dx] == '#':
                    cnt += 1
        S[i][j] = str(cnt)
for s in S:
    print("".join(s))

Buggy submission (verdict: Runtime Error):
A,B,C = map(int,input().split())
if A == B:
  print(C)
 elif B == C:
  print(A)
 else:
  print(B)
Problem p03623 (python)

Fixed submission:
x, a, b = map(int,input().split())
p = abs(x-a)
q = abs(x-b)
if p < q:
    print("A")
else:
    print("B")

Buggy submission (verdict: Runtime Error):
x, a, b = map(int(input())
A = abs(x-a)
B = abs(x-b)
if A < B:
    print("A")
else:
    print("B")
Problem p03636 (python)

Fixed submission:
a = input()
b = len(a)
print(a[0] + str(b-2) + a[b-1])

Buggy submission (verdict: Runtime Error):
a = input()
b = len(a)
print(a[0] + str(b) + a[b])
Problem p03729 (python)

Fixed submission:
a, b, c = input().split()
if a[len(a)-1] == b[0] and b[len(b)-1] == c[0]:
    print("YES")
else:
    print("NO")

Buggy submission (verdict: Runtime Error):
a, b, c = input().split()
if a[len(a-1)] == b[0] and b[len(b-1)] == c[0]:
    print("YES")
else:
    print("NO")

Buggy submission (verdict: Runtime Error):
a, b, c = input().split()
if a[len(a)] == b[0] and b[len(b)] == c[0]:
    print("YES")
else:
    print("NO")
Problem p03738 (python)

Fixed submission:
A = int(input())
B = int(input())
if A > B:
    print("GREATER")
elif A == B:
    print("EQUAL")
else:
    print("LESS")

Buggy submission (verdict: Runtime Error):
A = int(input())
B = int(input())
if A > B:
  print("GREATER")
eiif A == B:
  print("EQUAL")
else:
  print("LESS")
Problem p03741 (python)

Fixed submission:
n = int(input())
numbers = list(map(int,input().split()))
p = numbers[0]
a, b = 0, 0
if p > 0:
    q = -1
    b = p + 1
elif p < 0:
    q = 1
    b = -p + 1
else:
    p, q = 1, -1
    a, b = 1, 1
    
for i in range(1, n):
    if p > 0:
        p += numbers[i]
        if p >= 0:
            a += (p + 1)
            p -= (p + 1)
    else:
        p += numbers[i]
        if p <= 0:
            a -= (p - 1)
            p -= (p - 1)

for j in range(1, n):
    if q > 0:
        q += numbers[j]
        if q >= 0:
            b += (q + 1)
            q -= (q + 1)
    else:
        q += numbers[j]
        if q <= 0:
            b -= (q - 1)
            q -= (q - 1)


print(min(a, b))

Buggy submission (verdict: Runtime Error):
n = int(input())
numbers = list(map(int,input().split()))
p = numbers[0]
a, b = 0, 0
if p > 0:
    q = -1
    b = p + 1
elif p < 0:
    q = 1
    b = -p + 1
    
for i in range(1, n):
    if p > 0:
        p += numbers[i]
        if p >= 0:
            a += (p + 1)
            p -= (p + 1)
    else:
        p += numbers[i]
        if p <= 0:
            a -= (p - 1)
            p -= (p - 1)

for j in range(1, n):
    if q > 0:
        q += numbers[j]
        if q >= 0:
            b += (q + 1)
            q -= (q + 1)
    else:
        q += numbers[j]
        if q <= 0:
            b -= (q - 1)
            q -= (q - 1)


print(min(a, b))
Problem p03795 (python)

Fixed submission:
N = int(input())
p = N // 15
print(N*800 - p*200)

Buggy submission (verdict: Runtime Error):
N = int(input())
p = N // 15
print(N*800 - P*200)
Problem p03828 (python)

Fixed submission:
n = int(input())
ans = 1
r = [1] * (n + 1)
s = n


for i in range(1, n + 1):
    p = i
    while p % 2 == 0:
        p = p // 2
        r[1] += 1

    for j in range(3, i + 1, 2):
        while p % j == 0:
            p = p // j
            r[j-1] += 1

for k in range(n+1):
    ans *= r[k]

ans = ans % (10 ** 9 + 7)
print(ans)

Buggy submission (verdict: Runtime Error):
import sympy

Buggy submission (verdict: Runtime Error):
import sympy
N = int(input())
p = 1
for i in range(1, N+1):
    p *= i
ans = sympy.divisor_count(p)
print(ans % (10**9+7))
Problem p02719 (python)

Fixed submission:
import math
n,k = map(int,input().strip().split())
f = 1
x = n
while True:
    if x>k:
        poss = x//k
        x = x-poss*k
    if abs(x-k)<x:
        x = abs(x-k)
        f = 1
    else:
        break
print(x)

Buggy submission (verdict: Runtime Error):
import math
n,k = map(int,input().strip().split())
x = min(math.gcd(n,k),n,k)
f = 1
while f:
    if abs(x-k)<x:
        x = abs(x-k)
        f = 1
    else:
        f = 0
print(x)
Problem p02743 (python)

Fixed submission:
a,b,c = map(int,input().strip().split())
if c-(a+b)<=0:
    print("No")
else:
    l = 4*a*b
    r = (c**2+(a+b)**2-2*c*(a+b))
    if l<r:
        print("Yes")
    else:
        print("No")

Buggy submission (verdict: Runtime Error):
import math
a,b,c = map(int,input().strip().split())
l = math.log(4)+math.log(a)+math.log(b)
r = 2*math.log(c-(a+b))
if l<r:
    print("Yes")
else:
    print("No")
Problem p02576 (python)

Fixed submission:
N, X, T = map(int, input().split())
if N % X == 0:
    ans = N / X * T
else:
    ans = (N // X + 1) * T
print(int(ans))

Buggy submission (verdict: Runtime Error):
    N, X, T = map(int, input().split())
    if N % X == 0:
        ans = N / X * T
    else:
        ans = (N // X + 1) * T
    print(int(ans))
Problem p02683 (python)

Fixed submission:
import itertools as it

N, M, X = map(int, input().split())
book = [[int(_) for _ in input().split()] for i in range(N)]

combs = it.product([0, 1], repeat=N)
prices = []
for comb in combs:
    # print(comb)
    bag = [0] * (M+1)
    for i in range(N):
        if comb[i] == 0:
            continue
        else:
            for j in range(M+1):
                bag[j] += book[i][j]

    if min(bag[1:]) < X:
        continue
    else:
        prices.append(bag[0])

if len(prices) == 0:
    print(-1)
else:
    print(min(prices))

Buggy submission (verdict: Runtime Error):
import itertools as it

N, M, X = map(int, input().split())
book = [[int(_) for _ in input().split()] for i in range(N)]

combs = it.product([0, 1], repeat=N)
prices = []
for comb in combs:
    # print(comb)
    bag = [0] * M+1
    for i in range(N):
        if comb[i] == 0:
            continue
        else:
            for j in range(M+1):
                bag[j] += book[i][j]

    if min(bag[1:]) < X:
        continue
    else:
        prices.append(bag[0])

if len(prices) == 0:
    print(-1)
else:
    print(min(prices))

Buggy submission (verdict: Runtime Error):
import itertools as it
N, M, X = map(int,input().split())
book = [[int(_) for _ in input().split()] for i in range(N)]

combs = it.product([0,1], repeat=N)
prices = []
for comb in combs:
    # print(comb)
    bag = [0]*M
    for i in range(N):
        if comb[i] == 0:
            continue
        else:
            for j in range(M):
                bag[j] += book[i][j]
    
    if min(bag[1:]) < X:
        continue
    else:
        prices.append(bag[0])

if len(prices) == 0:
    print(-1)
else:
    print(min(prices))
Problem p02687 (python)

Fixed submission:
S = str(input())
if S=="ABC":
    print("ARC")
else:
    print("ABC")

Buggy submission (verdict: Runtime Error):
S = str(input())
if S=="ABC":
    print("ARC")
elif
    print("ABC")
Problem p02690 (python)

Fixed submission:
def main():
    X = int(input())
    Y = 2*int(X**(1/5))+1
    for A in range(-Y,Y):
        for B in range(-Y,Y):
            if A**5 - B**5==X:
                return A,B

if __name__ == "__main__":
    A,B=main()
    print("{} {}".format(A,B))

Buggy submission (verdict: Runtime Error):
def main():
    X = int(input())
    Y = int(X**(1/5))+1
    for A in range(-Y,Y):
        for B in range(-Y,Y):
            if A**5 - B**5==X:
                return A,B

if __name__ == "__main__":
    A,B=main()
    print("{} {}".format(A,B))
Problem p02658 (python)

Fixed submission:
import sys
n = int(input())
a = list(map(int,input().split()))
a.sort()
if a[0] == 0:
  print(0)
  sys.exit()
ans = 1
for i in range(n):
  ans *= a[i]
  if ans > 10**18:
    print(-1)
    sys.exit()
print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
a = int(input()).split()
a.sort()
if a[0] == 0:
  print(0)
  return
ans = 1
for i in range(n):
  ans *= a[i]
  if ans > 10**18:
    print(-1)
    return
print(ans)
Problem p02582 (python)

Fixed submission:
s = input()
if s == 'RRR':
    ans = 3
elif s == 'SRR' or s == 'RRS':
    ans = 2
elif s == 'SSS':
    ans = 0
else:
    ans = 1
print(ans)

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
P = list(map(lambda x: int(x) - 1, input().split()))
C = list(map(int,input().split()))

ans = max(C)
for i in range(N):
    s = i
    l = [C[i]]
    while True:
        s = P[s]
        if s == i:
            break
        l.append(C[s])
        

    if sum(l) > 0:
        c = K//len(l)*sum(l)
        n = K%len(l)
        ans = max(ans,c)
        s = i
        for j in range(n):
            c += C[s]
            s = P[s]
            ans = max(ans,c)

    n = min(N*2,K)
    s = i
    c = 0
    for j in range(n):
        c += C[s]
        s = P[s]
        ans = max(ans,c)
            
print(ans)
Problem p02585 (python)

Fixed submission:
N,K = map(int,input().split())
P = list(map(lambda x: int(x) - 1, input().split()))
C = list(map(int,input().split()))

ans = max(C)
for i in range(N):
    s = i
    l = [C[i]]
    while True:
        s = P[s]
        if s == i:
            break
        l.append(C[s])
        

    if sum(l) > 0:
        if K>=len(l):
            c = (K//len(l)-1)*sum(l)
            n = K%len(l)+len(l)
        else:
            c = 0
            n = K
        ans = max(ans,c)
        s = i
        for j in range(n):
            c += C[s]
            s = P[s]
            ans = max(ans,c)

    n = min(N,K)
    s = i
    c = 0
    for j in range(n):
        c += C[s]
        s = P[s]
        ans = max(ans,c)
            
print(ans)

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
P = list(map(lambda x: int(x) - 1, input().split()))
C = list(map(int,input().split()))

ans = max(C)
for i in range(N):
    s = i
    l = [C[i]]
    while:
        s = P[s]
        if s == i:
            break
        l.append(C[s])
        

    if sum(l) > 0:
        c = K//len(l)*sum(l)
        n = K%len(l)
        ans = max(ans,c)
        s = i
        for j in range(n):
            c += C[s]
            s = P[s]
            ans = max(ans,c)

    n = min(N,K)
    s = i
    c = 0
    for j in range(n):
        c += C[s]
        s = P[s]
        ans = max(ans,c)
            
print(ans)

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

ans = max(C)
for i in range(N):
    s = i
    while True:
        s = P[s]-1
        l.append(C[s])
        if s == i:
            break
    if sum(l) > 0:
        c = K//len(l)*sum(l)
        n = K%len(l)
        ans = max(ans,c)
        s = i
        x = 0
        for j in range(n):
            s = P[s]-1
            x += C[s]
            ans = max(ans,c+x)
    else:
        n = min(N,K)
        s = i
        x = 0
        for j in range(n):
            s = P[s]-1
            x += C[s]
            ans = max(ans,x)
            
print(ans)
Problem p02586 (python)

Fixed submission:
def main(R,C,K,V):
    dp = [0]*(4*R*C)
    dp[1+4*0+4*C*0] = V[0][0]
    for k in range(4*R*C):
        n = k%4
        j = k//4%C
        i = k//(4*C)
        if n!=0:
            if i != 0 and n==1:
                dp[k] = max(dp[k-4*C-n:k-4*C-n+4])+V[i][j]
            if j != 0:
                dp[k] = max(dp[k],dp[k-4],dp[k-5]+V[i][j])
        if n==0:
            if i != 0:
                dp[k] = max(dp[k-4*C-n:k-4*C-n+4])

    print(max(dp[-5:-1]))

if __name__ == '__main__':
    R,C,K = map(int,input().split())
    C += 1
    rcv = [list(map(int,input().split())) for _ in range(K)]
    V = [[0]*C for _ in range(R)]
    for i in range(K):
        r,c,v = rcv[i]
        r -= 1
        c -= 1
        V[r][c] = v
    main(R,C,K,V)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**8)
from collections import defaultdict
inf = 10**18
N = int(input())
S = []
C = []
for i in range(N):
    s,c = input().split()
    S.append(s)
    C.append(int(c))
    
d_l = defaultdict(lambda:inf)
def left(s1):
    res = inf
    if s1==s1[::-1]:
        return 0
    if d_l[s1] != inf:
        return d_l[s1]
    for i in range(N):
        s2 = S[i][::-1]
        c = C[i]
        if s1.startswith(s2):
            res = min(res,left(s1[len(s2):])+c)
        if s2.startswith(s1):
            res = min(res,right(s2[len(s1):])+c)
    d_l[s1] = res
    return res

d_r = defaultdict(lambda:inf)
def right(s2):
    res = inf
    if s2==s2[::-1]:
        return 0
    if d_r[s2] != inf:
        return d_r[s2]
    for i in range(N):
        s1 = S[i]
        c = C[i]
        if s1.startswith(s2):
            res = min(res,left(s1[len(s2):])+c)
        if s2.startswith(s1):
            res = min(res,right(s2[len(s1):])+c)
    d_r[s2] = res
    return res

ans = inf
for i in range(N):
    ans = min(ans,left(S[i])+C[i])

if ans == inf:
    ans = -1

print(ans)