Advertisement
i_am_wiz

solution1

Nov 24th, 2022
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. //---------------------------Hello there----------------------------
  6.  
  7. int n;
  8. int a[20];
  9. int dp[10][1 << 20];
  10.  
  11. int bestPairing(int board, int mask) {
  12. if(board == n + 1) return 0; // base case
  13. if(dp[board][mask] != -1) {
  14. return dp[board][mask]; // if ans already found in dp arr the return
  15. }
  16.  
  17. // if ans not already computed, then compute
  18. int ans = 0;
  19. for (int i = 0; i < 2 * n; ++i) {
  20. for (int j = 0; j < 2 * n; ++j) {
  21. if( (mask & ((1 << i) | (1 << j))) == 0 )
  22. ans = max(ans, board * abs(a[i] - a[j]) * gcd(a[i], a[j]) + bestPairing(board + 1, mask | (1 << i) | (1 << j)));
  23. }
  24. }
  25. dp[board][mask] = ans; // store ans and then return
  26. return dp[board][mask];
  27. }
  28.  
  29. int main() {
  30.  
  31. cin >> n;
  32. for (int i = 0; i < 2 * n; ++i) {
  33. cin >> a[i];
  34. }
  35. memset(dp, -1, sizeof dp);
  36. cout << bestPairing(1, 0) << endl;
  37.  
  38. return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement