fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main(){
  6. int t;cin>>t;
  7.  
  8. while (t--)
  9. {
  10. int n;
  11. cin>>n;
  12. int powsCount =0;
  13.  
  14. for (int i = 1; i <= n; i++)
  15. {
  16. int ii=i;
  17. while (!(ii%2) and ii/2!=1)
  18. {
  19. ii/=2;
  20. }
  21. if (ii==2)
  22. {
  23. powsCount++;
  24.  
  25. }
  26.  
  27. }
  28.  
  29.  
  30. int arrpowsWehave[powsCount];
  31.  
  32. for (int i = 1; i <= powsCount; i++)
  33. {
  34. arrpowsWehave[i-1]=i;
  35.  
  36. }
  37.  
  38. int twosReminder=0;
  39.  
  40. int steps=0;
  41.  
  42. for (int i = 0; i < n; i++)
  43. {
  44. int a;
  45. cin>>a;
  46. twosReminder+=1;
  47.  
  48. while (!(a%2))
  49. {
  50. a/=2;
  51. twosReminder--;
  52.  
  53.  
  54. }
  55.  
  56.  
  57.  
  58. }
  59.  
  60. for (int i=powsCount-1;i>=0;i--)
  61. {
  62. if(twosReminder>=arrpowsWehave[i]){
  63. twosReminder-=arrpowsWehave[i];
  64. steps++;
  65. }
  66. if (twosReminder==0)
  67. {
  68. break;
  69. }
  70.  
  71.  
  72. }
  73. if(twosReminder!=0 and twosReminder>0){
  74. steps=-1;
  75. }
  76. cout<<steps<<'\n';
  77.  
  78.  
  79.  
  80.  
  81. }
  82. }
Success #stdin #stdout 0s 5344KB
stdin
Standard input is empty
stdout
Standard output is empty