CSES - Shared codeLink to this code:
https://cses.fi/paste/c7b47cd556431c833488ee/
#include <bits/stdc++.h>
using namespace std;
#define int long long int
void solve() {
int n;
cin >> n;
vector<int> dp(n + 1, INT_MAX);
auto rec = [&](int num, const auto &self) -> int {
if (num == 0)return 0;
if (dp[num] != INT_MAX)return dp[num];
vector<int> digits;
int m = num;
while (m) {
if (m % 10 != 0)digits.template emplace_back(m % 10);
m /= 10;
}
for (int i: digits) {
dp[num] = min(dp[num], 1 + self(num - i, self));
}
return dp[num];
};
cout << rec(n, rec);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tc = 1;
//cin >> tc;
for (int i = 1; i <= tc; i++) {
solve();
}
return 0;
}