Advertisement
Zeinab_Hamdy

Untitled

Aug 11th, 2023
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. using namespace __gnu_pbds;
  5. using namespace std;
  6. #define pb push_back
  7. #define sz(x) int(x.size())
  8. #define all(vec) vec.begin(), vec.end()
  9. #define rall(vec) vec.rbegin(), vec.rend()
  10. #define cin(v) \
  11. for (auto &cn : v) \
  12. cin >> cn;
  13. #define cout(v) \
  14. for (auto &cn : v) \
  15. cout << cn << " ";
  16. #define MOD 1000000007
  17. // #define pi 3.142857
  18. #define numofdigits(n) ((int)log10(n) + 1)
  19. #define endl '\n'
  20. typedef long long ll;
  21. typedef unsigned long long ull;
  22. typedef long double ld;
  23. typedef pair<int, int> pii;
  24. typedef pair<ll, ll> pll;
  25. typedef vector<int> vi;
  26. typedef vector<ll> vl;
  27. void Haidy()
  28. {
  29. ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  30. // freopen("input.txt","r",stdin),freopen("output.txt","w",stdout);
  31. }
  32. template <typename K, typename V, typename Comp = std::less<K>>
  33. using ordered_map = tree<K, V, Comp, rb_tree_tag, tree_order_statistics_node_update>;
  34. template <typename K, typename Comp = std::less<K>>
  35. using ordered_set = ordered_map<K, null_type, Comp>;
  36. template <typename K, typename V, typename Comp = std::less_equal<K>>
  37. using ordered_multimap = tree<K, V, Comp, rb_tree_tag, tree_order_statistics_node_update>;
  38. template <typename K, typename Comp = std::less_equal<K>>
  39. using ordered_multiset = ordered_multimap<K, null_type, Comp>;
  40.  
  41. int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
  42. int dy[] = {-1, 1, 0, 0, -1, 1, -1, 1};
  43. // int dx2[]= {-2,-2,2,2,1,1,-1,-1};
  44. // int dy2[]= {-1,1,1,-1,2,-2,2,-2};
  45.  
  46. void solve();
  47.  
  48. int main()
  49. {
  50. Haidy();
  51. int t = 1;
  52. cin >> t;
  53. while(t--)
  54. solve();
  55. return 0;
  56. }
  57. int n;
  58.  
  59. vector<ll> mem , a , freq;
  60.  
  61. ll dp(int i){
  62. if(*max_element(all(freq)) > 2)
  63. return -1e18;
  64. if(i == n){
  65. return 0;
  66. }
  67. ll & ret = mem[i];
  68. if(~ret)
  69. return ret;
  70. ret = 0;
  71.  
  72. // add me
  73. ll tmp = a[i];
  74. while(tmp){
  75. freq[tmp % 10]++;
  76. tmp /= 10;
  77. }
  78.  
  79. ret = a[i] + dp(i + 1);
  80. //backtracking
  81. tmp = a[i];
  82. while(tmp){
  83. freq[tmp % 10]--;
  84. tmp /= 10;
  85. }
  86. ret = max(ret, dp(i + 1));
  87. return ret;
  88. }
  89. void solve(){
  90. cin >> n;
  91. // mem(105) , a(105) , freq(10);
  92.  
  93. mem.assign(105 , -1);
  94. a.assign(105 ,0);
  95. freq.assign(10,0);
  96.  
  97. for(int i = 0; i < n; i++){
  98. cin >> a[i];
  99. }
  100. cout << dp(0) << '\n';
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement