int cash, n; int money[10]; int bills[10]; int dp[100001];
voidcomplete_pack(int w){ for (int i = w; i <= cash; i++) { dp[i] = max(dp[i], dp[i - w] + w); } }
voidzero_one_pack(int w){ for (int i = cash; i >= w; i--) { dp[i] = max(dp[i], dp[i - w] + w); } }
voidmultiple_pack(int w, int n){ if (w * n >= cash) { complete_pack(w); } else { int k = 1; while (n > 0) { zero_one_pack(k*w); n -= k; if (k * 2 <= n) k *= 2; else k = n; } } }
intmain(){ while (cin >> cash) { cin >> n; memset(dp, 0, sizeof(dp)); for (int i = 0; i < n; i++) { cin >> bills[i] >> money[i]; multiple_pack(money[i], bills[i]); } for (int i = cash; i >= 0; i--) { if (dp[i] == i) { printf("%d\n", i); break; } } } }
voidcomplete_pack(int w){ for (int i = w; i <= cash; i++) { dp[i] = dp[i] || dp[i - w]; } }
voidzero_one_pack(int w){ for (int i = cash; i >= w; i--) { dp[i] = dp[i] || dp[i - w]; } }
voidmultiple_pack(int w, int n){ if (w * n >= cash) { complete_pack(w); } else { int k = 1; while (n > 0) { zero_one_pack(k*w); n -= k; if (k * 2 <= n) k *= 2; else k = n; } } }
intmain(){ while (cin >> cash) { cin >> n; memset(dp, 0, sizeof(dp)); dp[0] = true; for (int i = 0; i < n; i++) { cin >> bills >> money; multiple_pack(money, bills); } for (int i = cash; i >= 0; i--) { if (dp[i]) { printf("%d\n", i); break; } } } }