After the lessons n groups of schoolchildren went outside and decided to visit Polycarpus to celebrate his birthday. We know that the i-th group consists of si friends (1 ≤ si ≤ 4), and they want to go to Polycarpus together. They decided to get there by taxi. Each car can carry at most four passengers. What minimum number of cars will the children need if all members of each group should ride in the same taxi (but one taxi can take more than one group)?
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of groups of schoolchildren. The second line contains a sequence of integers s1, s2, ..., sn (1 ≤ si ≤ 4). The integers are separated by a space, si is the number of children in the i-th group.
Output
Print the single number — the minimum number of taxis necessary to drive all children to Polycarpus.
Examples
input
5
1 2 4 3 3
output
4
input
8
2 3 4 4 2 1 3 1
output
5
Note
In the first test we can sort the children into four cars like this:
- the third group (consisting of four children),
- the fourth group (consisting of three children),
- the fifth group (consisting of three children),
- the first and the second group (consisting of one and two children, correspondingly).
There are other ways to sort the groups into four cars.
题面解释:
由于英语差,都不懂题,会错了题意,后来想了一下午,明白了题目是什么意思:
有许多小组要结对去Polycarpus家,但是他们要坐出租车,他们一个组有1~4个人不等,而且每辆出租车只能最多坐4个人,现在问你至少要几辆车。
思路:
贪心+模拟
最多坐4个同一小组还不能分开,记住这个原则。所以我们先分4个的组,ans加4个人的组的组数,然后分3个分,3个组的人还能插1个的,然后分两个的,22组合,如果1有剩2有剩,继续组合,也就是。4人组的直接开车,2优先跟2组合,3优先跟1组合,最后1或者1和2组合就好。
代码:
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 int main() 5 { 6 int n; 7 scanf("%d",&n); 8 int have[4]={0}; 9 int m; 10 int ans=0; 11 for(int i=0;i<n;++i) 12 { 13 scanf("%d",&m); 14 ++have[m]; 15 if(m==4) 16 ++ans; 17 } 18 if(have[3]<have[1]) 19 { 20 ans+=have[3]; 21 have[1]-=have[3]; 22 have[3]=0; 23 } 24 else 25 { 26 ans+=have[3]; 27 have[1]=0; 28 have[3]=0; 29 } 30 ans+=have[2]/2; 31 if(have[2]%2) 32 { 33 ++ans; 34 have[1]-=2; 35 } 36 if(have[1]>0) 37 { 38 ans+=have[1]/4; 39 if(have[1]%4) 40 ++ans; 41 } 42 printf("%d\n",ans); 43 }