fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. #define int long long
  5. #define yes cout << "YES\n";
  6. #define no cout << "NO\n";
  7.  
  8.  
  9. void FastIO(){
  10. ios_base::sync_with_stdio(false);
  11. cin.tie(nullptr);
  12. cout.tie(nullptr);
  13. }
  14.  
  15. void solve(){
  16. int n;
  17. cin >> n;
  18. vector<int> v(n);
  19. for (int i = 0; i < n; i++){
  20. cin >> v[i];
  21. }
  22.  
  23. int ops = 0;
  24.  
  25. for (int i = n - 2; i >= 0; i--) {
  26. while (v[i] >= v[i+1] && v[i] > 0) {
  27. v[i] /= 2;
  28. ops++;
  29. }
  30. if (v[i] == 0 && v[i] >= v[i+1]) {
  31. cout << -1 << "\n";
  32. return;
  33. }
  34. }
  35.  
  36. set<int> st(v.begin(), v.end());
  37. if ((int)st.size() < n) {
  38. cout << -1 << "\n";
  39. return;
  40. }
  41.  
  42. cout << ops << "\n";
  43. }
  44.  
  45.  
  46. signed main(){
  47. FastIO();
  48.  
  49. int t = 1;
  50. cin >> t;
  51.  
  52. while (t--){
  53. solve();
  54. }
  55. return 0;
  56. }
  57.  
Success #stdin #stdout 0s 5324KB
stdin
7
3
3 6 5
4
5 3 2 1
5
1 2 3 4 5
1
1000000000
4
2 8 7 5
5
8 26 5 21 10
2
5 14
stdout
2
-1
0
0
4
11
0