Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- using namespace __gnu_pbds;
- using namespace std;
- #define pb push_back
- #define sz(x) int(x.size())
- #define all(vec) vec.begin(), vec.end()
- #define rall(vec) vec.rbegin(), vec.rend()
- #define cin(v) \
- for (auto &cn : v) \
- cin >> cn;
- #define cout(v) \
- for (auto &cn : v) \
- cout << cn << " ";
- #define MOD 1000000007
- // #define pi 3.142857
- #define numofdigits(n) ((int)log10(n) + 1)
- #define endl '\n'
- typedef long long ll;
- typedef unsigned long long ull;
- typedef long double ld;
- typedef pair<int, int> pii;
- typedef pair<ll, ll> pll;
- typedef vector<int> vi;
- typedef vector<ll> vl;
- void Haidy()
- {
- ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
- // freopen("input.txt","r",stdin),freopen("output.txt","w",stdout);
- }
- template <typename K, typename V, typename Comp = std::less<K>>
- using ordered_map = tree<K, V, Comp, rb_tree_tag, tree_order_statistics_node_update>;
- template <typename K, typename Comp = std::less<K>>
- using ordered_set = ordered_map<K, null_type, Comp>;
- template <typename K, typename V, typename Comp = std::less_equal<K>>
- using ordered_multimap = tree<K, V, Comp, rb_tree_tag, tree_order_statistics_node_update>;
- template <typename K, typename Comp = std::less_equal<K>>
- using ordered_multiset = ordered_multimap<K, null_type, Comp>;
- int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
- int dy[] = {-1, 1, 0, 0, -1, 1, -1, 1};
- // int dx2[]= {-2,-2,2,2,1,1,-1,-1};
- // int dy2[]= {-1,1,1,-1,2,-2,2,-2};
- void solve();
- int main()
- {
- Haidy();
- int t = 1;
- cin >> t;
- while(t--)
- solve();
- return 0;
- }
- int n;
- vector<ll> mem , a , freq;
- ll dp(int i){
- if(*max_element(all(freq)) > 2)
- return -1e18;
- if(i == n){
- return 0;
- }
- ll & ret = mem[i];
- if(~ret)
- return ret;
- ret = 0;
- // add me
- ll tmp = a[i];
- while(tmp){
- freq[tmp % 10]++;
- tmp /= 10;
- }
- ret = a[i] + dp(i + 1);
- //backtracking
- tmp = a[i];
- while(tmp){
- freq[tmp % 10]--;
- tmp /= 10;
- }
- ret = max(ret, dp(i + 1));
- return ret;
- }
- void solve(){
- cin >> n;
- // mem(105) , a(105) , freq(10);
- mem.assign(105 , -1);
- a.assign(105 ,0);
- freq.assign(10,0);
- for(int i = 0; i < n; i++){
- cin >> a[i];
- }
- cout << dp(0) << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement