fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Codechef
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. // your code goes here
  10. Scanner sc=new Scanner(System.in);
  11. int t=sc.nextInt();
  12. while(t-->0){
  13. int n=sc.nextInt();
  14. int k=sc.nextInt();
  15. int [] A=new int[n];
  16. int [] B=new int[n];
  17. for(int i=0;i<n;i++){
  18. A[i]=sc.nextInt();
  19. }
  20. for(int i=0;i<n;i++){
  21. B[i]=sc.nextInt();
  22. }
  23. int ok=Math.min(solve(A,B,k),solve(B,A,k));
  24. System.out.println(ok);
  25. }
  26. sc.close();
  27. }
  28. public static int solve(int []A,int[]B,int k){
  29. int diff=0, n=A.length;
  30. for(int i=0;i<n;i++){
  31. diff=Math.max(diff,A[i]-B[i]);
  32. }
  33. int [][][] dp=new int[n+1][k+1][diff+1];
  34. dp[0][0][0]=1;
  35. int maxA=Integer.MIN_VALUE;
  36. for(int i=1;i<=n;i++){
  37. for(int j=0;j<=k;j++){
  38. for(int l=0;l<=diff;l++){
  39. dp[i][j][l]=dp[i-1][j][l];
  40. if(j-1 >= 0){
  41. int id=l-(A[i-1]-B[i-1]);
  42. if (id>=0 && id<=diff){
  43. dp[i][j][l]=Math.max(dp[i][j][l],dp[i-1][j-1][id]+B[i-1]);
  44. }
  45. }
  46. if(j==k){
  47. maxA=Math.max(maxA,dp[i][j][l]);
  48. }
  49. }
  50. }
  51. }
  52. return maxA;
  53. }
  54. }
  55.  
Success #stdin #stdout 0.12s 56644KB
stdin
1
5 3
6 3 6 5 1
1 4 5 9 2
stdout
15