游客 Signup | Login
中文 | En

2114 - CF挑战!

虽然fuzhongqing是个弱菜。但是他会常常到codeforce上受虐。因为他坚信“被大神虐多了才有可能成为大神”!!!


于是他做出了第一道codeforce水题!

<br />

<span style="color:#494949;font-family:Arial, Helvetica, sans-serif;font-size:14px;line-height:22px;background-color:#C7CBBD;"><span style="color:#333333;font-family:Verdana, Arial, Tahoma;font-size:14px;line-height:25px;background-color:#FFFFFF;">有n个数,这些数都是由5和0组成,现选取其中的数组成一个数a,使得a%90==0,若存在,输出可以组成的最大的a,否则输出-1。</span></span> 

Input

第一行包含整数n(1≤N≤103)。下一行包含n个整数A1,A2,...,一(AI=0或AI=5)。数字AI表示被写入第i个卡上的数字。

Output

输出最大的a,若不存在a则输出-1。

Examples

Input

4
5 0 5 0
11
5 5 5 5 5 5 5 5 0 5 5

Output

0
5555555550

Solution C

#include <stdio.h>
#include <malloc.h>
int main()
{ int N;
	while(scanf("%d",&N)!=EOF)
	{int x,count1=0,count2=0,t,i,m,*p;
     while(N--)
	 {
		 scanf("%d",&x);
		 if(!x) count1++;
		 else count2++;
	 }
      t=(count2/9)*9+count1;
	  m=t-count1;
	  p=(int *)malloc(t*sizeof(int));
	  i=t;
	  if(!m&&count1)       printf("0");
	  else if(!count1) printf("-1");
	  else
	  {
	  while(m--)
	  p[--i]=5;	  
	  while(count1--)
      p[--i]=0;
	  for(;t>0;t--)
	  printf("%d",p[t-1]);
	  }
	  printf("\n");
	}
return 0;
}

Solution C++

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    while(cin>>n)
    {
        int i,temp,count5=0,count0=0;
        while(n--)
        {
            cin>>temp;
            temp?count5++:count0++;
        }
        if(count5<9)
        {
            if(count0>0)
            {
                cout<<"0"<<endl;
                continue;
            }
            cout<<"-1"<<endl;
            continue;
        }
        if(count0==0)
        {
            cout<<"-1"<<endl;
            continue;
        }
        for(i=1;i<=count5/9;i++)
            cout<<"555555555";
        for(i=1;i<=count0;i++)
            cout<<"0";
        cout<<endl;
    }
    return 0;
}

Time Limit 1 second
Memory Limit 128 MB
Discuss Stats
上一题 下一题