for test case 12 , expected out put is 51 and it is giving 49 , wasted a lot of time during the contest !
ll n = 0 , flag = 0 , ans = 0 ;
ll memo[20][2]; // Memoization table
ll dp( string &b , int ind , bool flag_high , string curr){
if(ind >= n ){
return 0;
}
if(memo[ind][flag_high] != -1) { // If result is already computed, return it
return memo[ind][flag_high];
}
ll low = 0 , high = 9 ;
if(flag_high){
high = b[ind] - '0' ;
}
ll ans = 0 ;
f(i , 0 , high+1){
bool temp_high = flag_high ;
if(i < b[ind] - '0'){
temp_high = false ;
}
ans += i + dp(b , ind + 1 , temp_high , curr ) ;
}
return ans; // Save the computed result in the memoization table
}
void solve(){
ll r ;
cin >> r;
string b = to_string(r);
ans = 0 ;
n = b.size() ;
cout << b<<endl;
memset(memo, -1, sizeof(memo)); // Initialize memoization table with -1
cout << dp(b , 0 , true ,"" ) <<endl;
}
int main(){
int t ;
cin >> t;
while(t--){
solve();
}
}