fork(1) download
  1. # include <iostream>
  2. # include <vector>
  3. # include <algorithm>
  4. using namespace std;
  5.  
  6. int main() {
  7. int n,x;
  8. cin >> n;
  9.  
  10. int cnt = 1;
  11. vector <int> v;
  12. for (int i = 0; i < n; i++) {
  13. cin >> x;
  14. cnt += x == 4;
  15. v.push_back(x);
  16. }
  17.  
  18. sort(v.rbegin(), v.rend());
  19. int sz = 4;
  20. bool f = false;
  21. for (int i = 0; i < n; i++) {
  22. if (v[i] != 4) {
  23. if (sz - v[i] < 0) {
  24. sz = 4 - v[i];
  25. cnt++;
  26. } else sz -= v[i];
  27. }
  28. }
  29.  
  30. cout << cnt;
  31. return 0;
  32. }
Success #stdin #stdout 0s 3480KB
stdin
8
2 3 4 4 2 1 3 1
stdout
6