fork(1) download
  1. /* paiza POH! Lite
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/kirishima/result/327fdfad3b495e0501dd024b36334c29
  4.  * author: Leonardone @ NEETSDKASU
  5.  */
  6. import java.io.*;
  7. import java.lang.*;
  8. import java.util.*;
  9.  
  10. class Main
  11. {
  12. static int parseInt(String str) {
  13. int len = str.length();
  14. int n = 0;
  15. for (int i = 0; i < len; i++) {
  16. n = n * 10 + (int)(str.charAt(i) - '0');
  17. }
  18. return n;
  19. }
  20.  
  21. static int[][] a = new int[51][500001];
  22. static int[] q = new int[51];
  23. static int[] r = new int[51];
  24.  
  25. public static void main (String[] args) throws java.lang.Exception
  26. {
  27.  
  28. int m;
  29. int n;
  30.  
  31. int w = 0, p = 0;
  32.  
  33. int i, j, x, y;
  34. int[] t0, t1;
  35.  
  36. m = parseInt(in.readLine());
  37. n = parseInt(in.readLine());
  38.  
  39. for (i = 0; i < n; i++) {
  40. String[] str = in.readLine().split(" ");
  41. q[i] = parseInt(str[0]);
  42. r[i] = parseInt(str[1]);
  43. w += q[i];
  44. p += r[i];
  45. }
  46. w -= m;
  47.  
  48. for (i = 0; i < n; i++) {
  49. t0 = a[i];
  50. t1 = a[i + 1];
  51. for (j = 0; j <= w; j++) {
  52. if (q[i] <= j) {
  53. x = t0[j];
  54. y = t0[j - q[i]] + r[i];
  55. t1[j] = (x > y) ? x : y;
  56. } else {
  57. t1[j] = t0[j];
  58. }
  59. }
  60. }
  61.  
  62. System.out.println(p - a[n][w]);
  63.  
  64. }
  65. }
Success #stdin #stdout 0.24s 380160KB
stdin
250
5
35 3640
33 2706
98 9810
57 5472
95 7790 
stdout
23072