fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace CSharp
  8. {
  9. class MAXNUM3
  10. {
  11. static void Main(string[] args)
  12. {
  13. char z = '0';
  14. int t = int.Parse(Console.ReadLine());
  15. while (t-- != 0)
  16. {
  17. string str = Console.ReadLine();
  18. List<char> removed = new List<char>(str.ToCharArray());
  19. int sum = 0;
  20. foreach (char c in str)
  21. sum += (c - z);
  22. int mod = sum % 3;
  23.  
  24. if (((removed.Last() - z) & 1) == 1)
  25. {
  26. removed.RemoveAt(str.Length - 1);
  27. sum = sum - (str[removed.Count()] - z);
  28. if (sum%3!=0 || (((removed.Last() - z) & 1) == 1))
  29. {
  30. Console.WriteLine(-1);
  31. continue;
  32. }
  33. }
  34. //else if (((removed.Last() - z) % 3) == mod && ((removed[str.Length-2] - z) % 2) == 0)
  35. //{
  36. // if (((removed[str.Length - 2] - z) % 3) == mod && (removed[str.Length - 2] < (removed[str.Length - 1])))
  37. // {
  38. // removed.RemoveAt(str.Length - 2);
  39. // }
  40. // else
  41. // {
  42. // removed.RemoveAt(str.Length - 1);
  43. // }
  44. //}
  45. else
  46. {
  47. int last = -1;
  48. for (int i = 0; i < str.Length-1; i++)
  49. {
  50. if (((removed[i] - z) % 3) == mod)
  51. {
  52. last = i;
  53. if (removed[i] <= removed[i + 1])
  54. {
  55. removed.RemoveAt(i);
  56. break;
  57. }
  58. }
  59. }
  60. if (((removed[removed.Count()-1] - z) % 3) == mod && (removed[removed.Count()-2] - z) % 2 == 0)
  61. last = str.Length - 1;
  62. if (str.Length == removed.Count() && last >= 0)
  63. removed.RemoveAt(last);
  64. }
  65. if (str.Length == removed.Count())
  66. Console.WriteLine(-1);
  67. else
  68. {
  69. Console.WriteLine(new string(removed.ToArray()));
  70. }
  71. }
  72. }
  73. }
  74. }
Success #stdin #stdout 0.02s 132160KB
stdin
1
9663000
stdout
963000