fork(3) download
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. long long int n;
  6. int x=0,count=0,c4=0,c3=0,c2=0,c1=0;
  7. cin>>n;
  8. while(n--)
  9. {
  10. cin>>x;
  11. if(x==4)
  12. c4++;
  13. else if(x==3)
  14. c3++;
  15. else if(x==2)
  16. c2++;
  17. else if(x==1)
  18. c1++;
  19. }
  20.  
  21. // cout<<c4<<" "<<c3<<" "<<c2<<" "<<c1<<endl;
  22. count+=c4;
  23. c4=0;
  24. if(c3>=c1)
  25. {
  26. count+=c3;
  27. c3=c1=0;
  28. }
  29. else
  30. {
  31. count+=c3;
  32. c1-=c3;
  33. c3=0;
  34. }
  35. if(c2>=c1)
  36. {
  37. if(c1%2==0)
  38. {
  39. c1/=2;
  40. count+=c1;
  41. c2-=c1;
  42. }
  43. else
  44. {
  45. c1+=1;
  46. c1/=2;
  47. count+=c1;
  48. c2-=c1;
  49. }
  50. if(c2>=2)c2/=2;
  51. count+=c2;
  52. }
  53. else
  54. {
  55. if(c1%2==0)
  56. {
  57. c1/=2;
  58. count+=c2;
  59. c1-=c2;
  60. }
  61. else
  62. {
  63. c1+=1;
  64. c1/=2;
  65. count+=c2;
  66. c1-=c2;
  67. }
  68. if(c1>=2)c1/=2;
  69. count+=c1;
  70. }
  71. cout<<count<<endl;
  72. return 0;
  73. }
  74.  
Success #stdin #stdout 0s 3472KB
stdin
5
4 3 2 1 4
stdout
4