fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4.  
  5. int main()
  6. {
  7. ll t;
  8. // cin>>t;
  9. t=1;
  10. while (t--)
  11. {
  12. ll n=5;
  13. vector <ll> v(n);
  14. for (ll i=0;i<n;i++)
  15. {
  16. v[i]=i+1;
  17. }
  18. ll ans=LLONG_MIN;
  19. map <vector <ll>,ll> m;
  20. do
  21. {
  22. ll sum=0;
  23. for (ll i=0;i<n;i++)
  24. {
  25. for (ll j=i;j<n;j++)
  26. {
  27. ll mini=LLONG_MAX;
  28. for (ll k=i;k<=j;k++)
  29. {
  30. mini=min(mini,v[k]);
  31. }
  32. sum+=mini;
  33. // cout<<sum<<"\n";
  34. }
  35. }
  36. m[v]=sum;
  37. if (sum>=ans)
  38. {
  39. ans=sum;
  40. }
  41. } while (next_permutation(v.begin(),v.end()));
  42. // cout<<"\n";
  43. ll counter=1;
  44. cout<<ans<<"\n";
  45. for (auto elem:m)
  46. {
  47. if (elem.second==ans)
  48. {
  49. cout<<counter<<"-> ";
  50. for (auto entry:elem.first)
  51. {
  52. cout<<entry<<" ";
  53. }
  54. counter++;
  55. cout<<"\n";
  56. }
  57. }
  58. }
  59. return 0;
  60. }
Success #stdin #stdout 0.01s 5272KB
stdin
Standard input is empty
stdout
35
1-> 1 2 3 4 5 
2-> 1 2 3 5 4 
3-> 1 2 4 5 3 
4-> 1 2 5 4 3 
5-> 1 3 4 5 2 
6-> 1 3 5 4 2 
7-> 1 4 5 3 2 
8-> 1 5 4 3 2 
9-> 2 3 4 5 1 
10-> 2 3 5 4 1 
11-> 2 4 5 3 1 
12-> 2 5 4 3 1 
13-> 3 4 5 2 1 
14-> 3 5 4 2 1 
15-> 4 5 3 2 1 
16-> 5 4 3 2 1