C++ Using 2 sets
Instead of using a dp array ,I have used 2 sets , one used to store all possible sums till i-1 using combination of all 0 ,1 or 2 elements and other to make new
Instead of using a dp array ,I have used 2 sets , one used to store all possible sums till i-1 using combination of all 0 ,1 or 2 elements and other to make new combinations using those elements with the 0,1,2 multiplier taken with the current elemet ,then all values of a can be checked in order to get the closest possible value wih the target element ,the approach is discussed below\n```\nclass Solution {\npublic: \n\n    int closestCost( vector<int>& a , vector<int>& b , int target ) \n    {\n        set <int > s , s1;\n        s.insert(0);\n        s.insert(b[0]);\n        s.insert(2*b[0]);\n        int n = b.size();\n        for( int i = 1 ; i < n ; i++ )\n        {\n            for( auto u : s )\n            {\n                s1.insert( u + b[i] );\n                s1.insert( u + 2 * b[i] );\n            }\n            for(auto u : s1)\n            {\n                s.insert(u);\n            }\n            s1.clear();\n           \n        }\n       \n        int min_diff = INT_MAX , ans = INT_MAX; \n        for( auto u : a )\n        {\n            for( auto v : s )\n            {\n                if( llabs( u + v - target )  < min_diff )\n                {\n                    ans = u +v;\n                    min_diff = llabs(u+v-target);\n                   /// cout << min_diff <<  " " << ans << endl;\n                }\n                else if( llabs( u + v - target )  ==  min_diff)\n                {\n                    ans = min(ans ,u + v);\n                }\n            }\n        }\n        \n        return ans;\n    }\n};\n```
[]