fork(2) download
  1. #include<bits/stdc++.h>
  2. /*
  3. @author Trijeet Ganguly
  4. */
  5. #define F(i,j,k,l) for((i)=(j);(i)<(k);(i)+=(l))
  6. #define f0(i,j) F(i,0,j,1)
  7. #define f1(i,j) F(i,1,(j+1),1)
  8. #define RF(i,j,k,l) for((i)=(j);(i)>(k);(i)-=(l))
  9. #define rf0(i,j) RF(i,(j-1),-1,1)
  10. #define rf1(i,j) RF(i,j,0,1)
  11. #define pb push_back
  12. #define NL <<"\n"
  13. #define SP <<" "
  14. #define nline cout<<"\n"
  15. #define all(o) o.begin(), o.end()
  16. #define FAST_IO ios_base::sync_with_stdio(false),cin.tie(NULL)
  17. #define elif else if
  18. #define tests int T; cin>>T; while(T--)
  19. #define arr_inp(i,N,x,arr) f0(i,N){ cin>>x; arr.pb(x); }
  20. #define PAUSE system("pause")
  21.  
  22. using namespace std;
  23.  
  24. typedef long long ll;
  25. typedef unsigned long long ull;
  26. typedef vector<int> vi;
  27. typedef vector<ll> vll;
  28.  
  29. const int MOD = 1e9+7;
  30. const int iINF = 1e9+9;
  31. const double PI = 3.1415926535;
  32.  
  33.  
  34. int main()
  35. {
  36. FAST_IO;
  37.  
  38. /*freopen("in.txt","r",stdin);
  39. freopen("out.txt","w",stdout);//*/
  40.  
  41. ll i,j,k,x,y,z;
  42. tests
  43. {
  44. string a;
  45. cin>>a;
  46. ll sum=0, n=a.length();
  47. f0(i,n)
  48. sum+= (a[i]-'0');
  49. ll need= sum%3, last=a[n-1]-'0', l2=a[n-2]-'0';
  50.  
  51. if(last%2==1 and l2%2==1)
  52. {
  53. cout<<-1 NL;
  54. continue;
  55. }
  56. if(last%2==1 && l2%2==0 && (sum-last)%3==0)
  57. {
  58. f0(i,n-1)
  59. cout<<a[i];
  60. nline;
  61. continue;
  62. }
  63. if(last%2==1 && l2%2==0 && (sum-last)%3!=0)
  64. {
  65. cout<<-1 NL;
  66. continue;
  67. }
  68.  
  69. ll mark=-1, low=12;
  70. for(i=0;i<n-1;i++)
  71. {
  72. x=a[i]-'0';
  73. if((sum-x)%3==0 && x< a[i+1]-'0')
  74. {
  75. mark=i;
  76. break;
  77. }
  78. if((sum-x)%3==0 && low>=x)
  79. {
  80. low=x;
  81. mark=i;
  82. }
  83. }
  84. if(mark== -1)
  85. {
  86. cout<<-1 NL;
  87. }
  88. else
  89. {
  90. f0(i,n)
  91. if(i != mark)
  92. cout<<a[i];
  93. nline;
  94. }
  95. }
  96.  
  97. /*fclose(stdin);
  98. fclose(stdout);//*/
  99.  
  100. return 0;
  101. }
Success #stdin #stdout 0s 16056KB
stdin
3
123
1000222
10003
stdout
12
000222
-1