Problem p02581 (python)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
N = len(A) // 3
A = A[:3*N]
#print(N, A)
A.append(0)
A.append(0)

ans = 0
def ind(a, b):
  t = sorted((a, b))
  return t[0] * (N+1) + t[1]
def inv(i):
  b = i % (N+1)
  a = i // (N + 1)
  return a, b

dp = [-1] * (N+1)**2
dp[ind(A[0],A[1])] = 0
current_max = 0
dp1 = [-1] * (N+1)
dp1[A[0]] = 0
dp1[A[1]] = 0

ans = 0
for i in range(0, N):
  a = A[2+3*i] 
  b = A[2+3*i+1]
  c = A[2+3*i+2]
  #print(a,b,c)
  if a == b == c:
    ans+=1
    continue
  dp_new = {}
  for x, y, z in [(a, b, c), (b, c, a), (c, a, b)]:
    # choose 1 (x), leave y, z
    i = ind(y, z)
    if i not in dp_new:
      dp_new[i] = 0
    dp_new[i] = max(dp_new[i], current_max)
    if dp[ind(x, x)] >= 0:
      dp_new[i] = max(dp_new[i], dp[ind(x, x)] + 1)
    
    # choose 2 (x, y), leave t, z
    for t in range(1, N+1):
      i = ind(t,z)
      # Not making three
      if dp1[t] >= 0:
        if i not in dp_new:
          dp_new[i]=0
        dp_new[i] = max(dp_new[i], dp1[t])
      # Making three (x==y)
      if x == y and dp[ind(t, x)] >= 0:
        if i not in dp_new:
          dp_new[i] = 0
        dp_new[i] = max(dp_new[i], dp[ind(t, x)] + 1)
        
  #print(a,b,c)
  #print({inv(i):v for i, v in dp_new.items() })
  for i, s in dp_new.items():
    dp[i] = max(dp[i], s)
    ta, tb = inv(i)
    dp1[ta] = max(dp1[ta], s)
    dp1[tb] = max(dp1[tb], s)
    current_max = max(current_max, s)

#print(doubles)
print(ans + max(dp1))

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
A.append(0)
A.append(0)

ans = 0
doubles = set()
singles = set()
if A[0] == A[1]:
  doubles.add(A[0])
  singles.add(A[0])
else:
  singles.add(A[0])
  singles.add(A[1])
  
for i in range(0, N):
  a = A[2+3*i] 
  b = A[2+3*i+1]
  c = A[2+3*i+2]
  #print(ans, doubles, singles, a, b, c)
  if a == b == c:
    ans += 1
    continue
  d = None
  if a == b or b == c:
    d = b
  if  c == a:
    d = a
  #print(d)
  if d == None:
    match = False
    for x in (a, b, c):
      if x in doubles:
        match = True
        break
    if match:
      doubles.clear()
      singles.remove(x)
      for x2 in (a, b, c):
        if x2 != x:
          singles.append(x2)
      
      ans += 1
      continue
    else:
      for x in (a, b, c):
        singles.append(x)
  else:
    for x in (a, b, c):
      if x != d:
        s = x
    if d in singles:
      if s in singles:
        doubles.add(s)
      singles.add(s)
      if d in doubles:
        doubles.clear()
        ans+=1
        continue
      else:
        doubles.clear()
        singles.remove(d)
        ans += 1
        continue
        
    if s in doubles:
        doubles.clear()
        singles.remove(s)
        singles.add(d)
        doubles.add(d)
        ans+=1
        continue
    if s in singles:
        doubles.add(s)
        singles.add(s)
    else:
        singles.add(s)
    singles.add(d)
    doubles.add(d)
#print(doubles)
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
A.append(0)
A.append(0)

ans = 0
doubles = set()
singles = set()
if A[0] == A[1]:
  doubles.add(A[0])
  singles.add(A[0])
else:
  singles.add(A[0])
  singles.add(A[1])
  
for i in range(0, N):
  a = A[2+3*i] 
  b = A[2+3*i+1]
  c = A[2+3*i+2]
  #print(ans, doubles, singles, a, b, c)
  if a == b == c:
    ans += 1
    continue
  d = None
  if a == b or b == c:
    d = b
  if  c == a:
    d = a
  #print(d)
  if d == None:
    match = False
    for x in (a, b, c):
      if x in doubles:
        match = True
        break
    if match:
      doubles.clear()
      singles.remove(x)
      for x2 in (a, b, c):
        if x2 != x:
          singles.append(x2)
      
      ans += 1
      continue
    else:
      for x in (a, b, c):
        singles.append(x)
  else:
    for x in (a, b, c):
      if x != d:
        s = x
    if d in singles:
      if s in singles:
        doubles.add(s)
      singles.add(s)
      if d in doubles:
        doubles.clear()
        ans+=1
        continue
      else:
        doubles.clear()
        singles.remove(d)
        ans += 1
        continue
        
    if s in doubles:
        doubles.clear()
        singles.remove(s)
        singles.add(d)
        doubles.add(d)
        ans+=1
        continue
    if s in singles:
        doubles.add(s)
    else:
        singles.add(s)
    singles.add(d)
    doubles.add(d)
#print(doubles)
print(ans)