fork download
  1. /* paiza POH! Lite
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/kirishima/result/050e7c0556cbbf5791a5018fd30b558f
  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. int *t0, *t1;
  22.  
  23.  
  24. scanf("%d", &m);
  25. scanf("%d", &n);
  26.  
  27. for (i = 0; i < n; i++) {
  28. scanf("%d %d", &q[i], &r[i]);
  29. w += q[i];
  30. p += r[i];
  31. }
  32. w -= m;
  33.  
  34. for (i = 0; i < n; i++) {
  35. t0 = a[i];
  36. t1 = a[i + 1];
  37. for (j = 0; j <= w; j++) {
  38. if (q[i] <= j) {
  39. x = t0[j];
  40. y = t0[j - q[i]] + r[i];
  41. t1[j] = (x > y) ? x : y;
  42. } else {
  43. t1[j] = t0[j];
  44. }
  45. }
  46. }
  47.  
  48. printf("%d\n", p - a[n][w]);
  49.  
  50. return 0;
  51. }
Success #stdin #stdout 0s 101888KB
stdin
250
5
35 3640
33 2706
98 9810
57 5472
95 7790 
stdout
23072