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. boolean ok=solve(A,B,k);
  24. System.out.println(ok);
  25. }
  26. sc.close();
  27. }
  28.  
  29. public static boolean solve(int []A,int[]B,int k){
  30. int diff=0, n=A.length;
  31. for(int i=0;i<n;i++){
  32. diff=Math.max(diff,A[i]-B[i]);
  33. }
  34. boolean [][][] dp=new boolean[n+1][k+1][diff+1];
  35. dp[0][0][0]=true;
  36.  
  37. for(int i=1;i<=n;i++){
  38. for(int j=0;j<=k;j++){
  39. for(int l=0;l<=diff;l++){
  40. if(dp[i-1][j][l]==true){
  41. dp[i][j][l]=true;
  42. }else if (j - 1 >= 0) {
  43. int id = l - (A[i - 1] - B[i - 1]);
  44. if (id >= 0 && id <= diff && dp[i - 1][j - 1][id]) {
  45. dp[i][j][l] = true;
  46. }
  47. }
  48. }
  49. }
  50. }
  51.  
  52. return dp[n][k][diff];
  53.  
  54. }
  55. }
  56.  
Success #stdin #stdout 0.14s 54544KB
stdin
1
5 3
6 3 6 5 1
1 4 5 9 2
stdout
true