fork(13) download
  1. #include<bits/stdc++.h>
  2. #define INF 1e9
  3.  
  4. using namespace std;
  5.  
  6. int n, m, htB[100] = {10,10,12,13,16}, htG[100] = {6,7,9,10,11,12,17}, dp[100][100];
  7.  
  8. int solve(int idx1, int idx2){
  9. if(idx1 == n) return 0;
  10. if(idx2 == m) return INF;
  11.  
  12. if(dp[idx1][idx2] != -1) return dp[idx1][idx2];
  13.  
  14. int v1, v2;
  15.  
  16. //include current
  17. v1 = solve(idx1 + 1, idx2 + 1) + abs(htB[idx1] - htG[idx2]);
  18.  
  19. //do not include current
  20. v2 = solve(idx1, idx2 + 1);
  21.  
  22. return dp[idx1][idx2] = min(v1, v2);
  23. }
  24.  
  25.  
  26. int main(){
  27.  
  28. n = 5, m = 7;
  29. sort(htB, htB+n);sort(htG, htG+m);
  30. for(int i = 0;i < 100;i++) for(int j = 0;j < 100;j++) dp[i][j] = -1;
  31. cout << solve(0, 0) << endl;
  32. return 0;
  33. }
Success #stdin #stdout 0s 3452KB
stdin
Standard input is empty
stdout
4