fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4. ios::sync_with_stdio(false);
  5. cin.tie(nullptr);
  6. freopen("MATMA.INP" , "r" , stdin);
  7. freopen("MATMA.OUT" , "w" , stdout);
  8. string s;
  9. cin >> s;
  10. vector<int> d;
  11. int sum = 0;
  12. bool hasZero = false;
  13. for (char c : s) {
  14. int x = c - '0';
  15. d.push_back(x);
  16. sum += x;
  17. if (x == 0) hasZero = true;
  18. }
  19. if (!hasZero || sum % 3 != 0) {
  20. cout << -1;
  21. return 0;
  22. }
  23. sort(d.begin(), d.end(), greater<int>());
  24. bool zeroUsed = false;
  25. for (int x : d) {
  26. if (x == 0 && !zeroUsed) {
  27. zeroUsed = true;
  28. continue;
  29. }
  30. cout << x;
  31. }
  32. cout << 0;
  33. return 0;
  34. }
Success #stdin #stdout 0.01s 5324KB
stdin
123456789
stdout
-1