Link to this code:
https://cses.fi/paste/bfa69186a1a66eb1897392/#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
const int INF = 1e9;
const ll LINF = 1e18;
const int N = 1e6 + 5;
template<typename T>
bool minimize(T& a, const T& b) {
if (a < b) return false;
a = b;
return true;
}
int memo[N];
int dp(int n) {
if (n == 0) return 0;
int& ans = memo[n];
if (ans != -1) return ans;
ans = INF;
for (int tmp = n; tmp > 0; tmp /= 10) {
int digit = tmp % 10;
minimize(ans, 1 + dp(n - digit));
}
return ans;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
memset(memo, -1, sizeof memo);
cout << dp(n) << '\n';
}