Java Knapsack O(2KM+NlogK), K = 20000
\n ```\n private int sln2(int[] bs, int[] ts, int t){\n        int n = bs.length;\n        int m = ts.length;\n        \n        boolean[] dp = new boolean[2000
\n ```\n private int sln2(int[] bs, int[] ts, int t){\n        int n = bs.length;\n        int m = ts.length;\n        \n        boolean[] dp = new boolean[20001];\n        dp[0] = true;\n        for(int i = 0;i<2*m;i++){\n            for(int j = dp.length-1; j>=ts[i%m];j--){\n                dp[j] =  dp[j] || dp[j-ts[i%m]];\n            }\n        }\n        List<Integer> list =new ArrayList<>();\n        for(int i=0;i< dp.length;i++) if(dp[i]) list.add(i);\n        \n        int res = Integer.MAX_VALUE;;\n        for(int i = 0;i< n;i++){\n            \n            int ceiling =Collections.binarySearch(list, t-bs[i]);\n            if(ceiling<0) ceiling = -ceiling-1;\n            if(ceiling<list.size()){\n                int sum = list.get(ceiling)+bs[i];\n                if(Math.abs(sum-t)<Math.abs(res-t)) res = sum;\n                else if(Math.abs(sum-t)==Math.abs(res-t) && sum<res) res = sum;\n            }\n            int floor = ceiling-1;\n            if(floor>=0){\n                int sum = list.get(floor)+bs[i];\n                if(Math.abs(sum-t)<Math.abs(res-t)) res = sum;\n                else if(Math.abs(sum-t)==Math.abs(res-t) && sum<res) res = sum;\n            }\n        }\n        return res;\n    }
[]