fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long int;
  4. #define inf 1000000000000000LL
  5. #define mod 1000000007LL
  6. #define FOR(i,a,b) for(int i = a; i < b; i++)
  7.  
  8. void solve() {
  9. ll n;
  10. cin >> n;
  11. vector <ll> l1(n);
  12. for (int i = 0; i < n; i++) cin >> l1[i];
  13. auto w = [&](ll x){
  14. ll cost = 0;
  15. while (x >= 1){
  16. cost++;
  17. if (x % 2 == 0) x/=2;
  18. else x--;
  19. }
  20. return cost;
  21. };
  22. auto flag = [&](ll x){
  23. ll cost = 0;
  24. for (int i= 0; i < n; i++){
  25. for (int j = 0; j < x; j++){
  26. l1[i] /= 2;
  27. }
  28. ll z = inf;
  29. for (int j = l1[i]; j < l1[i] + 20; j++){
  30. z = min(z, w(j) + ((1LL << x)*(j - l1[i])));
  31. }
  32. cost += z;
  33. }
  34. // cout << cost << "\n";
  35. return cost;
  36. };
  37. ll answ = inf;
  38. for (ll i = 0; i <= 25; i++){
  39. vector <ll> cur = l1; ll cost = i;
  40. for (ll j =0; j < n; j++){
  41. ll v = (1LL << (i));
  42. ll dif = l1[j] % v;
  43. if (dif >= 1){
  44. cost += v - dif;
  45. l1[j] += v - dif;
  46. }
  47. }
  48. // cout << cost << " ";
  49. cost += flag(i);
  50. // cout << cost << "\n";
  51. answ = min(answ, cost);
  52. l1 = cur;
  53. }
  54. cout << answ << "\n";
  55. }
  56.  
  57. int main() {
  58. ios::sync_with_stdio(false);
  59. cin.tie(nullptr);
  60.  
  61. ll t = 1;
  62. cin >> t;
  63. while (t--) {
  64. solve();
  65. }
  66.  
  67. return 0;
  68. }
Time limit exceeded #stdin #stdout 5s 69076KB
stdin
Standard input is empty
stdout
Standard output is empty