fork(3) download
  1. // * * ببسم الله الرحمن الرحيم * *
  2. // ********************@Author**************************************
  3. // ____ _____ __ _ ____ __ _
  4. // / __ \ | ____| | \| | / __ \ | \| |
  5. // / /__\ \ | |__ | |\ | / /__\ \ | |\ |
  6. // /_/ \_\ |_| |_| \_| /_/ \_\ |_| \_|
  7.  
  8. #include<bits/stdc++.h>
  9. using namespace std;
  10. #define mod 1000000007
  11. #define lcm(a,b) a*b/__gcd(a,b)
  12. #define endl '\n'
  13. #define FAsT ios_base::sync_with_stdio(false);cin.tie(NULL);
  14. #define sz 50000
  15. typedef long long ll;
  16. /*--------------------------------------------------------------------*/
  17.  
  18. int main()
  19. {
  20. FAsT
  21. ll a, b, c, d, e, f, g, h, k, len, n, m, p, q, t, x, y, z;
  22. cin >> t;
  23. while(t--)
  24. {
  25. cin>>n;
  26. deque<int>q;
  27. set<int>st;
  28. for(int i=0; i<n; i++)
  29. {
  30. cin>>x;
  31. q.push_back(x);
  32. st.insert(x);
  33. }
  34. if(st.size()==1)
  35. {
  36. cout<<-1<<endl;
  37. continue;
  38. }
  39. vector<int>v;
  40. vector<int>::iterator it;
  41. v.push_back(x);
  42. q.pop_back();
  43. while(!q.empty())
  44. {
  45. p = q.back();
  46. q.pop_back();
  47. if(x>p)
  48. {
  49. v.push_back(p);
  50. x = p;
  51. break;
  52. }
  53. else
  54. {
  55. x = p;
  56. v.push_back(p);
  57. }
  58.  
  59. }
  60.  
  61. sort(v.begin(),v.end());
  62.  
  63.  
  64. it =v.begin();
  65.  
  66. for(; it!=v.end(); it++)
  67. {
  68. // cout<<*it<<" ";
  69. if(*it > x)
  70. {
  71. q.push_back(*it);
  72. // cout<<"erase "<<*it<<endl;
  73. v.erase(it);
  74. break;
  75. }
  76. }
  77. // cout<<"it "<<*it<<endl;
  78. // q.push_back(*it);
  79. for(it =v.begin(); it!=v.end(); it++)
  80. {
  81. q.push_back(*it);
  82. }
  83. while(!q.empty())
  84. {
  85. cout<<q.front();
  86. q.pop_front();
  87. }
  88. cout<<endl;
  89. }
  90.  
  91. exit(0);
  92. }
Success #stdin #stdout 0s 5496KB
stdin
5
5
1 5 4 8 3
5
1 5 3 8 4
5
1 3 5 8 4
6
5 3 4 9 7 6
10
1 4 7 4 5 8 4 1 2 6
stdout
15834
15438
13845
536479
1474584162