Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

n, w = map(int, input().split())
wv = [list(map(int, input().split())) for _ in range(n)]

dp=[[0]*(w+1) for _ in range(n+1)]

for i in range(n):
    for sum_w in range(w+1):
        if sum_w-wv[i][0]>=0:
            dp[i+1][sum_w]=max(dp[i][sum_w], dp[i][sum_w-wv[i][0]]+wv[i][1])
        else:
            dp[i+1][sum_w]=dp[i][sum_w]

print(dp[n][w])


Fixed submission:
import sys
input = sys.stdin.readline

n, w = map(int, input().split())
wv = [list(map(int, input().split())) for _ in range(n)]

dp=[[0]*(w+1) for _ in range(n+1)]

for i in range(n):
    for sum_w in range(w+1):
        if sum_w-wv[i][0]>=0:
            dp[i+1][sum_w]=max(dp[i][sum_w], dp[i][sum_w-wv[i][0]]+wv[i][1])
        else:
            dp[i+1][sum_w]=dp[i][sum_w]

print(dp[n][w])
Buggy python submission (verdict: Runtime Error):
from itertools import product
n=int(input())

ans = 0
for i in range(3, 10):
    for s in product('357', repeat=i):
        if len(set(s)) < 3:
            continue
        753num = int(''.join(s))
        if  753num> n:
            break
        ans += 1
print(ans)


Fixed submission:
from itertools import product
n=int(input())

ans = 0
for i in range(3, 10):
    for s in product('357', repeat=i):
        if len(set(s)) < 3:
            continue
        num = int(''.join(s))
        if  num > n:
            break
        ans += 1
print(ans)
Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())

limit=int(m**(1/2))+1

divisors=[]
for d in range(limit):
    if m%d==0:
        divisors.append(d)
        divisors.append(m//d)
ans=1

for d in divisors:
    if n*d<=m:
        ans=d
print(ans)


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

limit=int(m**(1/2))+1

divisors=[]
for d in range(1,limit):
    if m%d==0:
        divisors.append(d)
        divisors.append(m//d)

divisors.sort()
ans=1

for d in divisors:
    if n*d<=m:
        ans=d
print(ans)
Buggy python submission (verdict: Runtime Error):
a,b=map(int,input())
if a%2==1 and b%2==1:
    print("Yes")
else:
    print("No")

Fixed submission:
a,b=map(int,input().split())
if a%2==1 and b%2==1:
    print("Yes")
else:
    print("No")
Buggy python submission (verdict: Runtime Error):
h,w=map(int,input().split())
a = [list(input()) for _ in range(w)]

row=[False]*h
col=[False]*w

for i in range(h):
    for j in range(w):
        if a[i][j]=="#":
            row[i]=True
            col[j]=True

for i in range(h):
    if row[i]:
        for j in range(w):
            if col[j]:
                print(a[i][j],end="")
        print()


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

row=[False]*h
col=[False]*w

for i in range(h):
    for j in range(w):
        if a[i][j]=="#":
            row[i]=True
            col[j]=True

for i in range(h):
    if row[i]:
        for j in range(w):
            if col[j]:
                print(a[i][j],end="")
        print()
Buggy python submission (verdict: Time Limit Exceeded):
n,m,q=map(int, input().split())

tab=[[0]*(n+1) for _ in range(n+1)]

for _ in range(m):
    l,r=map(int, input().split())
    tab[l][r]+=1

for i in range(1,n+1):
    for j in range(1,n):
        tab[i][j+1]+=tab[i][j]

for _ in range(q):
    p,q=map(int, input().split())
    ans=0
    for i in range(p,q+1):
        ans+=tab[i][q]-tab[i][p-1]
    print(ans)


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

tab=[[0]*(n+1) for _ in range(n+1)]

for _ in range(m):
    l,r=map(int, input().split())
    tab[l][r]+=1

for i in range(1,n+1):
    for j in range(1,n):
        tab[i][j+1]+=tab[i][j]

for _ in range(q):
    p,q=map(int, input().split())
    ans=0
    for i in range(p,q+1):
        ans+=tab[i][q]-tab[i][p-1]
    print(ans)
Buggy python submission (verdict: Runtime Error):
a,b=map(int,input().split())
if a+b<=16 and a<=8 and B<8:
    print("Yay!")
else:
    print(":(")

Fixed submission:
a,b=map(int,input().split())
if a+b<=16 and a<=8 and b<=8:
    print("Yay!")
else:
    print(":(")
Buggy python submission (verdict: Runtime Error):
A, B, C, X, Y = map(int, input().split())

answer = 10**18
for c in range(2*10**6+1):
  a = X-c
  b = Y-c
  cost = C*c*2
  cost += max(a, 0)*A
  cost += max(b, 0)*B
  if answer > cost:
    answer = cost
print(answer)


Fixed submission:
s=input()
print(700+int(s.count("o")*100))

Buggy python submission (verdict: Runtime Error):
A, B, C, X, Y = map(int, input().split())

answer = 10**18
for c in range(1.5*10**6+1):
  a = X-c
  b = Y-c
  cost = C*c*2
  cost += max(a, 0)*A
  cost += max(b, 0)*B
  if answer > cost:
    answer = cost
print(answer)


Fixed submission:
s=input()
print(700+int(s.count("o")*100))
Buggy python submission (verdict: Runtime Error):
x, y = map(int, input().split())

if yx% y == 0:
    print(-1)
else:
    print(x)


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

if x% y == 0:
    print(-1)
else:
    print(x)
Buggy python submission (verdict: Runtime Error):
s=input()
AKB={"AKIHABARA","KIHABARA","AKIHBARA","KIHBARA","AKIHABRA","KIHABRA","AKIHBRA","KIHBRA","AKIHABAR","KIHABAR","AKIHBAR","KIHBAR","AKIHABR","KIHABR","AKIHBR","KIHBR"}

print("YES" if s in pos else "NO")


Fixed submission:
s=input()
AKB={"AKIHABARA","KIHABARA","AKIHBARA","KIHBARA","AKIHABRA","KIHABRA","AKIHBRA","KIHBRA","AKIHABAR","KIHABAR","AKIHBAR","KIHBAR","AKIHABR","KIHABR","AKIHBR","KIHBR"}

print("YES" if s in AKB else "NO")

Buggy python submission (verdict: Runtime Error):
s=input()

flag=True
KIHBR="KIHBR"
s_new=s.replace("A", "")
if len(s_new)>5:
    flag=False
else:
    for i in range(5):
        if s_new[i]!=KIHBR[i]:
            flag=False
            break

if s.count("AA")>1 or s.count("KIH")!=1:
    flag=False

print("YES" if flag else "NO")


Fixed submission:
s=input()
AKB={"AKIHABARA","KIHABARA","AKIHBARA","KIHBARA","AKIHABRA","KIHABRA","AKIHBRA","KIHBRA","AKIHABAR","KIHABAR","AKIHBAR","KIHBAR","AKIHABR","KIHABR","AKIHBR","KIHBR"}

print("YES" if s in AKB else "NO")

Buggy python submission (verdict: Runtime Error):
s=input()

flag=True
KIHBR="KIHBR"
s_new=s.replace("A", "")
cnt=0
for i in range(len(s_new)):
    if s_new[i]!=KIHBR[cnt]:
        flag=False
        break
    cnt+=1

if len(s_new)>5:
    flag=False

if s.count("AA")>1:
    flag=False

print("YES" if flag else "NO")


Fixed submission:
s=input()
AKB={"AKIHABARA","KIHABARA","AKIHBARA","KIHBARA","AKIHABRA","KIHABRA","AKIHBRA","KIHBRA","AKIHABAR","KIHABAR","AKIHBAR","KIHBAR","AKIHABR","KIHABR","AKIHBR","KIHBR"}

print("YES" if s in AKB else "NO")

Buggy python submission (verdict: Runtime Error):
s=input()

flag=True
KIHBR="KIHBR"
s_new=s.replace("A", "")
cnt=0
for i in range(len(s_new)):
    if s_new[i]!=KIHBR[cnt]:
        flag=False
        break
    cnt+=1

if s.count("AA")>1:
    flag=False

print("YES" if flag else "NO")


Fixed submission:
s=input()
AKB={"AKIHABARA","KIHABARA","AKIHBARA","KIHBARA","AKIHABRA","KIHABRA","AKIHBRA","KIHBRA","AKIHABAR","KIHABAR","AKIHBAR","KIHBAR","AKIHABR","KIHABR","AKIHBR","KIHBR"}

print("YES" if s in AKB else "NO")
Buggy python submission (verdict: Runtime Error):
import itertools

h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]

for i in range(w):
    for j in range(h):
        cnt=0
        if s[i][j]!="#":
            for dx,dy in itertools.product([-1,0,1],repeat=2):
                x=i+dx
                y=j+dy
                if x<0 or x>=w:
                    continue
                if y<0 or y>=h:
                    continue
                if s[x][y]=="#":
                    cnt+=1
            s[i][j]=str(cnt)

for i in range(h):
    print(''.join(s[i]))


Fixed submission:
import itertools

h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]

for i in range(h):
    for j in range(w):
        cnt = 0
        if s[i][j] != "#":
            for dx, dy in itertools.product([-1, 0, 1], repeat=2):
                y = i+dy
                x = j+dx
                if y < 0 or y >= h:
                    continue
                if x < 0 or x >= w:
                    continue
                if s[y][x] == "#":
                    cnt += 1
            s[i][j] = str(cnt)

for i in range(h):
    print(''.join(s[i]))
Buggy python submission (verdict: Runtime Error):
from scipy.sparse.csgraph import dijkstra
n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]

graph = [[0]*(n+1) for _ in range(n+1)]
for a, b in ab:
    graph[a][b] = 1

ans = 0
inf = 10**9

for a, b in ab:
    graph[a][b] = inf
    dist = dijkstra(graph, indices = a, directed = False)
    if dist[b] >= inf:
        ans += 1
    graph[a][b] = 1

print(ans)


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

from collections import deque

def bfs(u):
    queue = deque([u])
    d = [None] * n # uからの距離の初期化
    d[u] = 0 # 自分との距離は0
    while queue:
        v = queue.popleft()
        for i in graph[v]:
            if d[i] is None:
                d[i] = d[v] + 1
                queue.append(i)
    flag=True
    for _i in range(n):
        if d[_i]==None:
            flag=False
    return flag

cnt=0
for i in range(m):
    graph = [[] for _ in range(n)]
    for j in range(m):
        if i!=j:
            graph[ab[j][0]-1].append(ab[j][1]-1)
            graph[ab[j][1]-1].append(ab[j][0]-1)
    if not bfs(0):
        cnt+=1
print(cnt)

Buggy python submission (verdict: Runtime Error):
from scipy.sparse.csgraph import dijkstra
n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]

graph = [[0]*(n+1) for _ in range(n+1)]
for a, b in ab:
    graph[a-1][b-1] = 1

ans = 0
inf = 10**9

for a, b in ab:
    graph[a][b] = inf
    dist = dijkstra(graph, indices = a, directed = False)
    if dist[b] >= inf:
        ans += 1
    graph[a][b] = 1

print(ans)


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

from collections import deque

def bfs(u):
    queue = deque([u])
    d = [None] * n # uからの距離の初期化
    d[u] = 0 # 自分との距離は0
    while queue:
        v = queue.popleft()
        for i in graph[v]:
            if d[i] is None:
                d[i] = d[v] + 1
                queue.append(i)
    flag=True
    for _i in range(n):
        if d[_i]==None:
            flag=False
    return flag

cnt=0
for i in range(m):
    graph = [[] for _ in range(n)]
    for j in range(m):
        if i!=j:
            graph[ab[j][0]-1].append(ab[j][1]-1)
            graph[ab[j][1]-1].append(ab[j][0]-1)
    if not bfs(0):
        cnt+=1
print(cnt)
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())

for h in range(1, 3501):
    for w in range(1, 3501):
        if 4*h*w-N*(h+w)!=0:
            n=N*h*w/(4*h*w-N*(h+w))
            if n.is_integer() and n>0:
                print(h,int(n),w)
                exit()


Fixed submission:
N=int(input())

for h in range(1, 3501):
    for w in range(1, 3501):
        if 4*h*w-N*(h+w)!=0:
            n=N*h*w/(4*h*w-N*(h+w))
            if n.is_integer() and n>0:
                print(h,int(n),w)
                exit()
Buggy python submission (verdict: Time Limit Exceeded):
n, m, r = map(int, input().split())  # n:頂点数　w:辺の数
r_list = list(map(int, input().split()))
graph = [[float('inf')]*n for i in range(n)]
for _ in range(m):
    u, v, w = map(int, input().split())
    graph[u-1][v-1] = w
    graph[v-1][u-1] = w
for i in range(n):
    graph[i][i] = 0

def warshall_floyd(d):
    # d[i][j] : i から j への最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j], d[i][k]+d[k][j])
    return d


from itertools import permutations

adj_m=warshall_floyd(graph)
ans=float('inf')
for path in permutations(r_list):
    tmp_dist=0
    for i in range(r-1):
        tmp_dist+=adj_m[path[i]-1][path[i+1]-1]
    ans=min(ans, tmp_dist)
print(ans)


Fixed submission:
n, m, r = map(int, input().split())  # n:頂点数　w:辺の数
r_list = list(map(int, input().split()))
graph = [[float('inf')]*n for i in range(n)]
for _ in range(m):
    u, v, w = map(int, input().split())
    graph[u-1][v-1] = w
    graph[v-1][u-1] = w
for i in range(n):
    graph[i][i] = 0

def warshall_floyd(d):
    # d[i][j] : i から j への最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j], d[i][k]+d[k][j])
    return d


from itertools import permutations

adj_m=warshall_floyd(graph)
ans=float('inf')
for path in permutations(r_list):
    tmp_dist=0
    for i in range(r-1):
        tmp_dist+=adj_m[path[i]-1][path[i+1]-1]
    ans=min(ans, tmp_dist)
print(ans)
Buggy python submission (verdict: Runtime Error):
import math
from sys import stdin
input = stdin.readline

n = int(input())
ans=[]
for i in range(1,round(math.sqrt(n))):
    if n%i==0:
        tmp=max(len(str(i)),len(str(n//i)))
        ans.append(tmp)
print(min(ans))

Fixed submission:
import math
from sys import stdin
input = stdin.readline

n = int(input())
ans=[1145141919]
for i in range(1,round(math.sqrt(n))+1):
    if n%i==0:
        tmp=max(len(str(i)), len(str(n//i)))
        ans.append(tmp)
print(min(ans))
Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
graph=[[]*n for _ in range(n)]
for _ in range(n):
    a,b=map(int,input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)

def dfs(v, seen):
    if all(seen):
        return 1
    ans=0
    for vv in graph[v]:
        if seen[vv]:
            continue
        seen[vv]=True
        ans+=dfs(vv, seen)
        seen[vv]=False
    return ans

seen=[False]*n
seen[0]=True
print(dfs(0, seen))


Fixed submission:
n,m=map(int,input().split())
graph = [[] for _ in range(n)]
for i in range(m):
    a, b=map(int, input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)

def dfs(v, seen):
    if all(seen):
        return 1
    ans=0
    for i in graph[v]:
        if seen[i]:
            continue
        seen[i]=True
        # print(seen)
        ans+=dfs(i, seen)
        seen[i]=False
    return ans

seen=[False]*n
seen[0]=True
print(dfs(0, seen))
Buggy python submission (verdict: Runtime Error):
s=input()
s＝s.replace("eraser","").replace("erase","").replace("dreamer","").replace("dream","")
if s:
    print("NO")
else:
    print("YES")

Fixed submission:
s=input()
s=s.replace("eraser","").replace("erase","").replace("dreamer","").replace("dream","")
if s:
    print("NO")
else:
    print("YES")
Buggy python submission (verdict: Runtime Error):
n=int(input())
a=[int(input()) for _ in range(n)]

if a.count(0)==0:
    print(sum(a//2))
else:
    tmp_sum=0
    ans=0
    for i in range(n):
        if a[i]==0:
            ans+=tmp_sum//2
            tmp_sum=0
        else:
            tmp_sum+=a[i]
    ans+=tmp_sum//2

    print(ans)



Fixed submission:
n=int(input())
a=[int(input()) for _ in range(n)]

if a.count(0)==0:
    print(sum(a)//2)
else:
    tmp_sum=0
    ans=0
    for i in range(n):
        if a[i]==0:
            ans+=tmp_sum//2
            tmp_sum=0
        else:
            tmp_sum+=a[i]
    ans+=tmp_sum//2

    print(ans)
Buggy python submission (verdict: Runtime Error):
def solve(n):
  ans = n * (n + 1) - 1  # 1とk 自信

  i = 2
  while i * i <= n:
      num = n // i
      min_i = i * i
      max_i = i * num
      ans += (num - i + 1) * (min_i + max_i)  # 自分自身
      ans -= min_i
      i += 1
  print(ans)
  return True

if __name__ = "__main__":
  n = int(input())
  solve(n)

Fixed submission:
#!/usr/bin/env python3
# input
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n = int(input())
ans = n * (n + 1) - 1  # 1とk 自信

i = 2
while i * i <= n:
    num = n // i
    min_i = i * i
    max_i = i * num
    ans += (num - i + 1) * (min_i + max_i)  # 自分自身
    ans -= min_i
    i += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
total = [0] * 31
for i in a:
    for j in range(31):
        if i >> j & 1:
            total[j] += 1
# print(total)
for i in a:
    ans = 0
    for j in range(31):
        if total[j] % 2 != (i >> j & 1):
            ans += 2 ** j
    print(ans, end=" ")
print()


Fixed submission:
n = int(input())
a = list(map(int, input().split()))
total = [0] * 31
for i in a:
    for j in range(31):
        if i >> j & 1:
            total[j] += 1
# print(total)
for i in a:
    ans = 0
    for j in range(31):
        if total[j] % 2 != (i >> j & 1):
            ans += 2 ** j
    print(ans, end=" ")
print()
Buggy python submission (verdict: Runtime Error):
import bisect

n = int(input())
a = list(map(int, input().split()))
a.sort()
k = len(a)
ok = [0] * k
for i in range(k):
    if i < k - 1 and a[i] == a[i + 1]:
        ok[i] = 1
        ok[i + 1] = 1
        continue
    """"if a[i] < 2 * 100:
        for j in range(i + 1, k):
            if a[j] % a[i] == 0:
                ok[j] = 1"""
    else:
        times = 2
        while a[i] * times < a[-1]:
            l = bisect.bisect_left(a, a[i] * times)
            r = bisect.bisect_right(a, a[i] * times)
            if r != l:
                for idx in range(l, r):
                    ok[idx] = 1
            times += 1
# print(ok)
print(len([i for i in ok if i == 0]))
# print(ok) WAはどこ？


Fixed submission:
import bisect

n = int(input())
a = list(map(int, input().split()))
a.sort()
k = len(a)
ok = [0] * k
for i in range(k):
    if i < k - 1 and a[i] == a[i + 1]:
        ok[i] = 1
        ok[i + 1] = 1
        continue
    if a[i] < 2 * 100:
        for j in range(i + 1, k):
            if a[j] % a[i] == 0:
                ok[j] = 1
    else:
        times = 2
        while a[i] * times < a[-1]:
            l = bisect.bisect_left(a, a[i] * times)
            r = bisect.bisect_right(a, a[i] * times)
            if r != l:
                for idx in range(l, r):
                    ok[idx] = 1
            times += 1
# print(ok)
print(len([i for i in ok if i == 0]))
# print(ok) WAはどこ？
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
ans = 1
ng = 0
for i in a:
    ans *= i
    if ans > 10 ** 18:
        ng = 1
    if i == 0:
        print(0)
        exit()
if ng:
    print(-1)
    exit()
print(ans)


Fixed submission:
n = int(input())
a = list(map(int, input().split()))
ans = 1
ng = 0
if 0 in a:
    print(0)
    exit()
for i in a:
    ans *= i
    if ans > 10 ** 18:
        print(-1)
        exit()
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
MOD = 998244353
N, S = map(int, input().split())
A = list(map(int, input().split()))
DP = [[0] * (1 + S) for _ in range(1 + N)]  # DP[i][j] はi番目までみた時に総和がjと成る組み合わせの総和
# 初期化
DP[0][0] = 1
# 漸化式　DP[i][j] = DP[i-1][j]*2 + DP[i-1][j-A[i]]
for i in range(1, N + 1):
    for j in range(S + 1):
        if j >= A[i - 1]:
            DP[i][j] = DP[i - 1][j] * 2 + DP[i - 1][j - A[i - 1]]
        else:
            DP[i][j] = DP[i - 1][j] * 2
        DP[i][j] %= MOD
print(DP[N][S])


Fixed submission:
MOD = 998244353
N, S = map(int, input().split())
A = list(map(int, input().split()))
DP = [[0] * (1 + S) for _ in range(1 + N)]  # DP[i][j] はi番目までみた時に総和がjと成る組み合わせの総和
# 初期化
DP[0][0] = 1
# 漸化式　DP[i][j] = DP[i-1][j]*2 + DP[i-1][j-A[i]]
for i in range(1, N + 1):
    for j in range(S + 1):
        if j >= A[i - 1]:
            DP[i][j] = DP[i - 1][j] * 2 + DP[i - 1][j - A[i - 1]]
        else:
            DP[i][j] = DP[i - 1][j] * 2
        DP[i][j] %= MOD
print(DP[N][S])
Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3
n, m = map(int, input().split())
h = list(map(int, input().split()))
not_good = []
for i in range(m):
    a, b = map(int, input().split())
    if h[a - 1] > h[b - 1]:  # bはダメ
        not_good.append(b)
    elif h[a - 1] == h[b - 1]:
        not_good.append(a)
        not_good.append(b)
    else:
        not_good.append(a)
ans = 0
not_good = list(set(not_good))
not_good.sort()
# print(not_good)
idx = 0
for i in range(1, n + 1):
    if i < not_good[idx]:
        ans += 1
        # print(i, end=" ")
    elif i == not_good[idx]:
        idx += 1
        if idx == len(not_good):
            ans += n - i
            break
    else:
        pass
print(ans)


Fixed submission:
#!/usr/bin/env python3
import bisect

n = int(input())
a = list(map(int, input().split()))
# b = a -1 とすると、bi+bj = -2*i or -2+j となるi,jを見つける
# sortして2分探索すると解ける
b = [a[i] - (1 + i) for i in range(0, n)]
b, idx = zip(*sorted(zip(b, range(1, n + 1))))
# print(b)
ans = 0
for i in range(n):
    # b[i]を固定して二分探索
    r = bisect.bisect_right(b, -b[i] - 2 * idx[i])
    l = bisect.bisect_left(b, -b[i] - 2 * idx[i])
    ans += r - l
    # if r - l != 0:
    # print(i, r, l, -b[i] - 2 * idx[i])
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import itertools

n, m, q = map(int, input().split())
abcd = [input() for _ in range(q)]
a, b, c, d = [0] * q, [0] * q, [0] * q, [0] * q
for i in range(q):
    a[i], b[i], c[i], d[i] = map(int, abcd[i].split())

comb = [1] * n + [0] * (m - 1)
combination_all = set(itertools.permutations(comb))  # 5!通り出力する
all_array = []
#print(combination_all)
for i in combination_all:
    tmp = 1
    lis = []
    for j in range(len(i)):
        if i[j]:
            lis.append(tmp)
        else:
            tmp += 1
    all_array.append(lis)
#print(all_array)
ans = 0  # maxをとる
for arr in all_array:
    val = 0
    for j in range(q):
        if arr[b[j] - 1] - arr[a[j] - 1] == c[j]:
            val += d[j]
    ans = max(ans, val)
print(ans)
# bit 全探索で i が成り立つと仮定する
# 連結する奴の中で最大と最小の差がN-1以下なら成立
"""ans = 0

for i in range(2**n):
    score = 0
    emerge = []
    lis = []
    for j in range(n):
        if i>>j & j:
            if a[i] in emerge:"""


Fixed submission:
#!/usr/bin/env python3
from itertools import combinations_with_replacement

n, m, q = map(int, input().split())
abcd = [0] * q
for i in range(q):
    abcd[i] = list(map(int, input().split()))
# print(0)
# comb = [1] * n + [0] * (m - 1)
# combination_all = set(itertools.permutations(comb))  # 5!通り出力する
cmb = combinations_with_replacement([i for i in range(1, m + 1)], n)
all_array = list(cmb)
"""for i in combination_all:
    tmp = 1
    lis = []
    for j in range(len(i)):
        if i[j]:
            lis.append(tmp)
        else:
            tmp += 1
    all_array.append(lis)"""
# print(all_array)
ans = 0  # maxをとる
for arr in all_array:
    val = 0
    for j in range(q):
        if arr[abcd[j][1] - 1] - arr[abcd[j][0] - 1] == abcd[j][2]:
            val += abcd[j][3]
    ans = max(ans, val)
print(ans)
# bit 全探索で i が成り立つと仮定する
# 連結する奴の中で最大と最小の差がN-1以下なら成立
"""ans = 0

for i in range(2**n):
    score = 0
    emerge = []
    lis = []
    for j in range(n):
        if i>>j & j:
            if a[i] in emerge:"""
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
n = int(input())
ans = 0


def gcd(n, m):
    a, b = n, m
    if a > b:
        a, b = b, a
    while a != 0:
        a, b = b % a, a
    return b


for i in range(1, n + 1):
    for j in range(1, n + 1):
        for k in range(1, n + 1):
            ans += gcd(k, gcd(i, j))
print(ans)


Fixed submission:
#!/usr/bin/env python3
n = int(input())
ans = 0
# gcd_grid = [[0] * 201 for _ in range(201)]


def gcd(n, m):
    """if n <= 200 and m <= 200:
        if gcd_grid[n][m] != 0:
            return gcd_grid[n][m]"""
    a, b = n, m
    if a > b:
        a, b = b, a
    while a != 0:
        a, b = b % a, a
    # gcd_grid[n][m] = b
    # gcd_grid[m][n] = b
    return b


for i in range(1, n + 1):
    for j in range(i + 1, n + 1):
        for k in range(j + 1, n + 1):
            ans += 6 * gcd(k, gcd(i, j))
for i in range(1, n + 1):
    for j in range(i + 1, n + 1):
        ans += 6 * gcd(i, j)
for i in range(n + 1):
    ans += i
print(ans)