diff --git a/buggy.py b/buggy.py
--- a/buggy.py
+++ b/buggy.py
@@ -7,7 +7,7 @@
     d[s[i][0]] = s[i][1]
 dp = [[0] * (n + 1) for i in range(n + 1)]
 for i in range(n):
-    for j in range(1, n + 1):
+    for j in range(1, i + 2):
         dp[i + 1][j] = dp[i][j - 1] + x[i] + d[j]
     dp[i + 1][0] = max(dp[i])
 print(max(dp[-1]))
