fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6. // your code goes here
  7. int t,n,m,k,count;
  8. int b[30];
  9. scanf("%d",&t);
  10. set<int> s;
  11. set<int> :: iterator it,jt;
  12. while(t--)
  13. {
  14. int max=-1;
  15. scanf("%d",&n);
  16. scanf("%d",&m);
  17. for(int i=0;i<n;i++)
  18. {
  19. b[i]=i+1;
  20. }
  21. while(next_permutation(b,b+n))
  22. {count=0;
  23. for(int i=0;i<n;i++)
  24. {
  25.  
  26. s.insert(b[i]);
  27. it=s.find(b[i]);
  28. jt=it;
  29. if(it==s.begin())
  30. {
  31. count+=(*it);
  32. }
  33. else
  34. {
  35. --it;
  36. count+=(*jt)-(*it);
  37. }
  38. it=jt;
  39. it++;
  40. if(it==s.end())
  41. {
  42. count+=(n+1)-(*jt);
  43. }
  44. else
  45. {
  46. count+=(*it)-(*jt);
  47. }
  48. }
  49. if(count>max&&count<m)
  50. {
  51. max=count;
  52. }
  53. }
  54. cout<<max<<endl;
  55.  
  56. }
  57. return 0;
  58. }
Success #stdin #stdout 0s 3480KB
stdin
4
3 8
3 9
2 4
5 25
stdout
6
6
-1
13