fork download
  1. #include<bits/stdc++.h>
  2.  
  3. #pragma GCC optimize("O2")
  4. #pragma GCC optimize("O3")
  5. #pragma GCC optimize("Ofast")
  6.  
  7. #define int long long
  8.  
  9. #define pii pair<int,int>
  10. #define ii int,int
  11. #define fi first
  12. #define se second
  13. #define pb push_back
  14. #define lb lower_bound
  15. #define ub upper_bound
  16. #define umap unordered_map
  17. #define BIT(mask, i) ((mask >> i)&1)
  18. #define ALL(c) c.begin(), c.end()
  19.  
  20. using namespace std;
  21.  
  22. mt19937 rd(chrono::high_resolution_clock::now().time_since_epoch().count());
  23. int rand(int l, int r){
  24. return l + rd()*rd()%(r-l+1);
  25. }
  26.  
  27. const int N = 1e6+7;
  28. const int M = 1e3+3;
  29. const int MOD = 1e9+7;
  30. const int inf = 3e18;
  31.  
  32. int n, a[N], c[5];
  33.  
  34. signed main(){
  35. ios_base::sync_with_stdio(0);
  36. cin.tie(0);
  37. cout.tie(0);
  38. cin >> n;
  39. for(int i = 1; i <= n; i++){
  40. cin >> a[i];
  41. c[a[i]]++;
  42. }
  43. int ans = c[4];
  44. ans += c[3];
  45. c[1] -= c[3];
  46. if(c[1] < 0) c[1] = 0;
  47. ans += c[2]/2;
  48. if(c[2]%2 == 1){
  49. ans++;
  50. if(c[1] > 0){
  51. c[1] -= 2;
  52. }
  53. }
  54. if(c[1] > 0){
  55. ans += (c[1]/4);
  56. if(c[1] % 4 != 0){
  57. ans++;
  58. }
  59. }
  60. cout << ans;
  61. }
  62.  
  63.  
Success #stdin #stdout 0s 5348KB
stdin
Standard input is empty
stdout
Standard output is empty