fork(1) download
  1. /* paiza POH! Lite
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/kirishima/result/e0dc73382fe071c8016bf5fdd884277b
  4.  * author: Leonardone @ NEETSDKASU
  5.  */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. int a[51][500001];
  10. int q[51];
  11. int r[51];
  12.  
  13. int main(void) {
  14.  
  15. int m;
  16. int n;
  17.  
  18. int w = 0, p = 0;
  19.  
  20. int i, j, x, y;
  21.  
  22. scanf("%d", &m);
  23. scanf("%d", &n);
  24.  
  25. for (i = 0; i < n; i++) {
  26. scanf("%d %d", &q[i], &r[i]);
  27. w += q[i];
  28. p += r[i];
  29. }
  30. w -= m;
  31.  
  32. for (i = 0; i < n; i++) {
  33. for (j = 0; j <= w; j++) {
  34. if (q[i] <= j) {
  35. x = a[i][j];
  36. y = a[i][j - q[i]] + r[i];
  37. a[i + 1][j] = (x > y) ? x : y;
  38. } else {
  39. a[i + 1][j] = a[i][j];
  40. }
  41. }
  42. }
  43.  
  44. printf("%d\n", p - a[n][w]);
  45.  
  46. return 0;
  47. }
Success #stdin #stdout 0s 101888KB
stdin
250
5
35 3640
33 2706
98 9810
57 5472
95 7790 
stdout
23072