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