fork(86) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int mask(int n) {
  8. int mask = 0;
  9. for (int i = 1; n/i; i*= 10) {
  10. int digit = n/i % 10;
  11. mask |= 1 << digit;
  12. }
  13. return mask;
  14. }
  15.  
  16. int dp(int set, int memo[1<<10], const vector<int>& a) {
  17. if (set == 0) {
  18. return memo[set] = 0;
  19. }
  20. if (memo[set] != -1)
  21. return memo[set];
  22.  
  23. int res = 0;
  24. for (auto num: a) {
  25. int bitmask = mask(num);
  26. if ((set | bitmask) == set)
  27. res = std::max(dp(set ^ mask(num), memo, a) + num, res);
  28. }
  29. return memo[set] = res;
  30. }
  31.  
  32. int main()
  33. {
  34. vector<int> a = {121,23,3,333,4};
  35. int memo[1<<10];
  36. for (int i = 0; i < 1 << 10; i++) memo[i] = -1;
  37.  
  38. int res = 0;
  39. for (int i = 0; i < 1 << 10; i++) {
  40. res = std::max(res, dp(i, memo, a));
  41. }
  42. cout << res << std::endl;
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
458