1 #include <cstdio> 2 #include <cstring> 3 4 const int MAXN = 1e5+10; 5 6 bool dp[MAXN]; 7 int num[MAXN], val[MAXN]; 8 int l, n; 9 10 int main() 11 { 12 while (~scanf("%d%d", &l, &n)) { 13 int sum = 0; 14 for (int i = 1; i <= n; ++i) { 15 scanf("%d%d", num+i, val+i); 16 sum += num[i] * val[i]; 17 } 18 if (sum <= l) { 19 printf("%d\n", sum); 20 continue; 21 } 22 memset(dp, false, sizeof dp); 23 dp[0] = true; 24 for (int i = 1; i <= n; ++i) { 25 for (int j = l; j >= 0; --j) { 26 if (true == dp[j]) { 27 for (int k = 1; k <= num[i]; ++k) { 28 if (j + k * val[i] > l) 29 continue; 30 dp[j + k * val[i]] = true; 31 } 32 } 33 } 34 } 35 int i; 36 for (i = l; i >= 1; --i) 37 if (true == dp[i]) 38 break; 39 printf("%d\n", i); 40 } 41 return 0; 42 }