HELP IN UBER ONLINE QUESTION

Revision en1, by loverBoUy, 2022-07-30 10:08:58

Can someone pls tell me , why i'm getting 40 instead of 42 in given tc int n; int m; int d;

int cord[501]; int cost[501]; int dp[501][501][501]; int findans(int i, int prev, int m) {

if (i <= 0)
          return 0;

   if (m <= 0)
          return INT_MAX;


   if (dp[i][prev][m] != -1)
          return dp[i][prev][m];


   int ans = INT_MAX;

   for (int j = i; j >= 1; j--)
   {
          if (j == 1)
          {
                 ans = min(findans(j - 1, j, m) + (cord[prev] - cord[j]) * (cost[j]), ans);
          }
          else
          {
                 if (m > 0)
                 {
                        int res = min(findans(j - 1, prev, m - 1), findans(j - 1, j, m) + (cord[prev] - cord[j]) * (cost[j]));
                        ans = min(ans, res);
                 }
                 else
                        ans = min(ans, findans(j - 1, j, m) + (cord[prev] - cord[j]) * (cost[j]));
          }
   }

   return dp[i][prev][m] = ans;

} void solve() { cin >> n >> d >> m; for (int i = 1; i <= n; i++) { cin >> cord[i]; } for (int i = 1; i <= n; i++) { cin >> cost[i]; } cord[n + 1] = d; memset(dp, -1, sizeof(dp));

int ans = findans(n, n + 1, m);
   cout << ans << endl;

}

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English loverBoUy 2022-07-30 10:13:10 1394
en1 English loverBoUy 2022-07-30 10:08:58 1580 Initial revision (published)