fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. const unsigned long long MOD = 1e9 + 7;
  5. const int INF = (int)2e9 + 7;
  6. const long long LINF = (long long)1e18;
  7. const unsigned long long mod1 = 183453789;
  8. const unsigned long long mod2 = 1234567891;
  9. const int P1 = 337, P2 = 263;
  10.  
  11. template<typename T>
  12. T input(){
  13. T ans = 0, m = 1; char c = ' ';
  14. while (!((c >= '0' && c <= '9') || c == '-')) c = getchar();
  15. if (c == '-') m = -1, c = getchar();
  16. while (c >= '0' && c <= '9') ans = ans * 10 + (c - '0'), c = getchar();
  17. return ans * m;
  18. }
  19.  
  20. string nextString(bool flag = false){
  21. char ch; string ans = "";
  22. do { ch = getchar(); } while(ch <= ' ');
  23. while(1) {
  24. ans += ch; ch = getchar();
  25. if ( (!flag && ch <= ' ') || (flag && ch < ' ') ) break;
  26. }
  27. return ans;
  28. }
  29. char nextChar(){
  30. char ch;
  31. do {ch = getchar(); } while(ch <= ' ');
  32. return ch;
  33. }
  34. void read(string& s){ s = nextString(); }
  35. void read(char& c){ c = nextChar(); }
  36. template<typename T> void read(T& a){ a = input<T>(); }
  37. template<typename T, typename... R> void read(T& a, R&... r){ read(a); read(r...); }
  38.  
  39. map < int, int > m;
  40.  
  41. void print(){
  42. for (int i = 9; i >= 0; -- i){
  43. while (m[i] > 0){
  44. m[i] --;
  45. cout << i;
  46. }
  47. }
  48. }
  49.  
  50. bool erase(int x){
  51. if (m[x] > 0){
  52. m[x] --;
  53. return true;
  54. } else return false;
  55. }
  56.  
  57. int32_t main(){
  58. ios_base::sync_with_stdio(0); cin.tie(0);
  59. #ifdef LOCAL
  60. freopen("in.txt", "r", stdin);
  61. freopen("out.txt", "w", stdout);
  62. #else
  63. freopen("divisible.in", "r", stdin);
  64. freopen("divisible.out", "w", stdout);
  65. #endif
  66. string temp; read(temp);
  67. int sum = 0;
  68. for (auto i: temp){
  69. (sum += int(i - '0')) %= 3;
  70. m[int(i - '0')] ++;
  71. }
  72. if (sum == 1){
  73. bool ok = false;
  74. for (int i = 1; !ok && i < 10; i += 3){
  75. ok |= erase(i);
  76. }
  77. if (!ok){
  78. int cnt = 0;
  79. for (int i = 2; cnt < 2 && i < 10; i += 3){
  80. while (m[i] > 0 && cnt < 2){
  81. m[i] --;
  82. cnt ++;
  83. }
  84. }
  85. }
  86. } else
  87. if (sum == 2){
  88. bool ok = false;
  89. for (int i = 2; !ok && i < 10; i += 3){
  90. ok |= erase(i);
  91. }
  92. if (!ok){
  93. int cnt = 0;
  94. for (int i = 1; cnt < 2 && i < 10; i += 3){
  95. while (m[i] > 0 && cnt < 2){
  96. m[i] --;
  97. cnt ++;
  98. }
  99. }
  100. }
  101. }
  102.  
  103. print();
  104. }
Time limit exceeded #stdin #stdout 5s 3460KB
stdin
Standard input is empty
stdout
Standard output is empty