Advertisement
Ahmed_Negm

Untitled

Mar 29th, 2023
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 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 std;
  5. using namespace __gnu_pbds;
  6. #define ll long long
  7. #define OO 2'000'000'000
  8. #define ull unsigned long long
  9. #define nl '\n'
  10. #define sz(x) (ll)(x.size())
  11. #define all(x) x.begin(),x.end()
  12. #define rall(s)  s.rbegin(), s.rend()
  13. #define getline(s) getline(cin>>ws,s)
  14. #define ceill(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
  15. #define pi  3.141592653589793
  16. #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
  17. #define multi_ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
  18.  
  19.  
  20. void Fast_IO(){
  21. ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  22. // freopen("filename.in", "r", stdin);
  23. // freopen("filename.txt", "w", stdout);
  24. #ifndef ONLINE_JUDGE
  25. freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  26. #endif
  27. }
  28.  
  29.  
  30.  
  31.  
  32. int dx[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
  33. int dy[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
  34.  
  35. set<string>st;
  36. bool ok ;
  37. ll n,k;
  38. void cnt_subseq_of_lenk(ll i,string &s, ll sz,string ans=""){
  39.   if(sz(ans)==sz){
  40.     st.emplace(ans);
  41.     return;
  42.   }
  43.     if(i==sz(s)) return;
  44.   if(sz(st)>=k){
  45.     ok = 1;
  46.     return;
  47.   }
  48.   cnt_subseq_of_lenk(i+1,s,sz,ans+s[i]);
  49.   if(sz(st)>=k){
  50.     ok = 1;
  51.     return;
  52.   }
  53.   cnt_subseq_of_lenk(i+1,s,sz,ans);
  54. }
  55.  
  56. bool comp(string &a,string &b){
  57.     return sz(a) > sz(b);
  58. }
  59.  
  60.  
  61. void solve(){
  62.  cin>>n>>k;
  63. ll maxi = max(1LL,n-2);
  64. long double l = 1LL<<maxi;
  65. long double r = 1LL<<n;
  66. if(r-l<k){
  67.     cout<<-1<<nl;
  68.     return;
  69. }
  70.  
  71. string s; cin>>s;
  72. for(int i=sz(s);i>=max(1LL,n-2);i--){
  73.     if(ok) break;
  74.     cnt_subseq_of_lenk(0,s,i);
  75. }
  76. if(sz(st)<k){
  77.     cout<<-1<<nl;
  78.     return;
  79. }
  80. ll res = 0;
  81. vector<string>ans(all(st));
  82. sort(all(ans),comp);
  83. for(int i=0;i<sz(ans);i++){
  84.     res += sz(s)-sz(ans[i]);
  85. }
  86. cout<<res<<nl;
  87.  
  88.  
  89. }
  90.  
  91. int main(){
  92.     Fast_IO();
  93. int t =1;
  94. //cin>>t;
  95. while(t--){
  96. solve();
  97. }
  98. return 0;
  99. }  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement