diff --git a/buggy.py b/buggy.py
--- a/buggy.py
+++ b/buggy.py
@@ -4,7 +4,7 @@
 DP[0] = 1
 for i in range(N):
     a, b = map(int, input().split())
-    for n in range(X + 1):
+    for n in reversed(range(X + 1)):
         for c in range(1, b + 1):
             if 0 <= n - c * a and DP[n - c * a]:
                 DP[n] = 1
