Buggy python submission (verdict: Time Limit Exceeded):
N,M,Q=map(int,input().split())
List = []

for i in range(Q):
  a = list(map(int,input().split()))
  List.append(a)

arr = []
for i in range(2 ** Q):
    ans = [1 for i in range(N)]
    cnt = 0
    ck = 0
    
    for j in range(Q):  # このループが一番のポイント
      if ((i >> j) & 1):  # 順に右にシフトさせ最下位bitのチェックを行う
        a = List[j][0]
        b = List[j][1]
        c = List[j][2]
        d = List[j][3]
        cnt += d
        ans[b-1] = ans[a-1] + c

    for j in range(Q):  # このループが一番のポイント
      if ((i >> j) & 1):  # 順に右にシフトさせ最下位bitのチェックを行う
        a = List[j][0]
        b = List[j][1]
        c = List[j][2]
        d = List[j][3]
        if ans[b-1] - ans[a-1] != c:
          ck = 1
        if ans[b-1] > M:
          ck = 1
      
    if ck == 0:
      arr.append(cnt)

print(max(arr))

Fixed submission:
import itertools

N,M,Q=map(int,input().split())
List = []

for i in range(Q):
  a = list(map(int,input().split()))
  List.append(a)
  
ans = 0

for team in itertools.combinations_with_replacement(range(1,M+1), N):
  cnt = 0
  for i in range(Q):
    a = List[i][0]
    b = List[i][1]
    c = List[i][2]
    d = List[i][3]
    
    if team[b-1] - team[a-1] == c:
      cnt += d
  ans = max(ans ,cnt)  
  
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,M,Q=map(int,input().split())
List = []

for i in range(Q):
  a = list(map(int,input().split()))
  List.append(a)

arr = []
for i in range(2 ** Q):
    ans = [1 for i in range(N)]
    cnt = 0
    ck = 0
    
    for j in range(Q):  # このループが一番のポイント
      if ((i >> j) & 1):  # 順に右にシフトさせ最下位bitのチェックを行う
        a = List[j][0]
        b = List[j][1]
        c = List[j][2]
        d = List[j][3]
        cnt += d
        ans[b-1] = ans[a-1] + c

    for j in range(Q):  # このループが一番のポイント
      if ((i >> j) & 1):  # 順に右にシフトさせ最下位bitのチェックを行う
        a = List[j][0]
        b = List[j][1]
        c = List[j][2]
        d = List[j][3]
        if ans[b-1] - ans[a-1] != c:
          ck = 1
        if ans[b-1] > M:
          ck = 1
      
    if ck == 0:
      arr.append(cnt)

print(max(arr))

Fixed submission:
import itertools

N,M,Q=map(int,input().split())
List = []

for i in range(Q):
  a = list(map(int,input().split()))
  List.append(a)
  
ans = 0

for team in itertools.combinations_with_replacement(range(1,M+1), N):
  cnt = 0
  for i in range(Q):
    a = List[i][0]
    b = List[i][1]
    c = List[i][2]
    d = List[i][3]
    
    if team[b-1] - team[a-1] == c:
      cnt += d
  ans = max(ans ,cnt)  
  
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,M,Q=map(int,input().split())
List = []

for i in range(Q):
  a = list(map(int,input().split()))
  List.append(a)

arr = []
for i in range(2 ** Q):
    ans = [1 for i in range(N)]
    cnt = 0
    ck = 0
    
    for j in range(Q):  # このループが一番のポイント
      if ((i >> j) & 1):  # 順に右にシフトさせ最下位bitのチェックを行う
        a = List[j][0]
        b = List[j][1]
        c = List[j][2]
        d = List[j][3]
        cnt += d
        ans[b-1] = ans[a-1] + c

    for j in range(Q):  # このループが一番のポイント
      if ((i >> j) & 1):  # 順に右にシフトさせ最下位bitのチェックを行う
        a = List[j][0]
        b = List[j][1]
        c = List[j][2]
        d = List[j][3]
        if ans[b-1] - ans[a-1] != c:
          ck = 1
        if ans[b-1] > M:
          ck = 1
      
    if ck == 0:
      arr.append(cnt)

print(max(arr))

Fixed submission:
import itertools

N,M,Q=map(int,input().split())
List = []

for i in range(Q):
  a = list(map(int,input().split()))
  List.append(a)
  
ans = 0

for team in itertools.combinations_with_replacement(range(1,M+1), N):
  cnt = 0
  for i in range(Q):
    a = List[i][0]
    b = List[i][1]
    c = List[i][2]
    d = List[i][3]
    
    if team[b-1] - team[a-1] == c:
      cnt += d
  ans = max(ans ,cnt)  
  
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools

N,M,Q=map(int,input().split())
List = []

for i in range(Q):
  a = list(map(int,input().split()))
  List.append(a)
  

arr = []
for i in range(2 ** Q):
    ans = [1 for i in range(N)]
    cnt = 0
    ck = 0
    
    for j in range(Q):  # このループが一番のポイント
      if ((i >> j) & 1):  # 順に右にシフトさせ最下位bitのチェックを行う
        a = List[j][0]
        b = List[j][1]
        c = List[j][2]
        d = List[j][3]
        cnt += d
        ans[b-1] = ans[a-1] + c

    for j in range(Q):  # このループが一番のポイント
      if ((i >> j) & 1):  # 順に右にシフトさせ最下位bitのチェックを行う
        a = List[j][0]
        b = List[j][1]
        c = List[j][2]
        d = List[j][3]
        if ans[b-1] - ans[a-1] != c:
          ck = 1
        
    for k in range(N-1):
      if ans[k] > ans[k+1]:
        ck = 1
      elif ans[k] > M:
        ck = 1
    if ans[-1] > M:
      ck = 1
      
      
    if ck == 0:
      arr.append(cnt)

print(max(arr))




Fixed submission:
import itertools

N,M,Q=map(int,input().split())
List = []

for i in range(Q):
  a = list(map(int,input().split()))
  List.append(a)
  
ans = 0

for team in itertools.combinations_with_replacement(range(1,M+1), N):
  cnt = 0
  for i in range(Q):
    a = List[i][0]
    b = List[i][1]
    c = List[i][2]
    d = List[i][3]
    
    if team[b-1] - team[a-1] == c:
      cnt += d
  ans = max(ans ,cnt)  
  
print(ans)