fork download
  1. // Brankonymous //
  2.  
  3. #include <bits/stdc++.h>
  4.  
  5. #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  6. #define rep(i,l,r) for (int i=l;i<=r;i++)
  7. #define fi first
  8. #define se second
  9. #define pb push_back
  10. #define all(x) (x).begin(), (x).end()
  11. #define rall(x) (x).rbegin(), (x).rend()
  12. #define ll long long
  13. #define mod 1000000007
  14. #define random_jbt mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  15.  
  16. using namespace std;
  17.  
  18. int main(){
  19. ios;
  20.  
  21. string s,a,b,ans;
  22. cin>>s;
  23. a=b=s;
  24. sort(all(b));
  25. sort(rall(a));
  26.  
  27. for (int i=s.size()-1;i>=0;i--){
  28. if (a[i]>=b[i])ans+=(a[i]-b[i])+'0';
  29. else{
  30. int x=a[i]-'0';
  31. int y=b[i]-'0';
  32. while(x<y){
  33. x+=10;
  34. a[i-1]--;
  35. }
  36. ans+=(x-y)+'0';
  37. }
  38. }
  39. while(ans.size()>1 and ans.back()=='0')ans.pop_back();
  40. reverse(all(ans));
  41. cout<<ans;
  42. return 0;
  43. }
  44.  
Success #stdin #stdout 0s 15240KB
stdin
310141617
stdout
753197643