#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<queue>
#include<algorithm>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
using namespace std;
int f[6],ans;
void out()
{
printf("%d 1:%d 2:%d 3:%d 4:%d\n",ans,f[1],f[2],f[3],f[4]);
}
int main()
{
int n;
while(~scanf("%d",&n))
{
int x;clr(f,0);
FOR(i,1,n)
{
scanf("%d",&x);
++f[x];
}
ans=0;
///1->2;
int zmin=min(f[2],f[1]);
f[3]+=zmin;ans+=zmin;
f[2]-=zmin;f[1]-=zmin;
///2 self
f[3]+=2*(f[2]/3);ans+=2*(f[2]/3);
f[2]%=3;
// out();
// out();
///1->1 to 3
f[3]+=f[1]/3;ans+=2*(f[1]/3);
f[1]%=3;
if(f[1]<=f[3]){ans+=f[1];f[1]=0;}///1->3
else f[2]+=f[1]/2,ans+=f[1]/2,f[1]%=2; ///1->1 2
if(f[1])///left 1
{
if(f[1]<=f[3])ans+=f[1];///1->3
else if(f[4]>=f[1]*2)ans+=f[1]*2;///4->1
else ans=-1;///impossble
}
else if(f[2])
{
if(f[2]==2)ans+=2;///2->2 4
else if(f[2]==1)
{
if(f[4])ans++;///4->2 3,3
else if(f[3]>=2)ans+=2;///2->3 4
else ans=-1;
}
}
printf("%d\n",ans);
}
}
codeforce 365 C. Compartments(gready,4级)
最新推荐文章于 2020-07-01 17:51:18 发布