Advertisement
AlejandroGY

Sumas Rapidas

Apr 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.56 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. constexpr int INF = 1000000;
  4.  
  5. int s, n;
  6. std::string a;
  7.  
  8. int solve(int i, int num, int acum) {
  9.     if (acum > s || num >= 100) return INF;
  10.     if (i >= n) {
  11.         if (num + acum == s) return 0;
  12.         return INF;
  13.     }
  14.  
  15.     int temp = a[i] - '0';
  16.     int a = solve(i + 1, 10 * num + temp, acum);
  17.     int b = solve(i + 1, temp, num + acum) + 1;
  18.     return std::min(a, b);
  19. }
  20.  
  21. int main( ) {
  22.     std::ios_base::sync_with_stdio(0);
  23.     std::cin.tie(0);
  24.  
  25.     std::cin >> a >> s;
  26.     n = a.size( );
  27.     int res = solve(0, 0, 0);
  28.     std::cout << (res >= 51 ? -1 : res) << "\n";
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement