Andy works at the puzzles review magazine, and of course he got interest in this Japanese news. He realized that the information known so far is enough to find the number of equal first rows in a puzzle winning position. Now Andy wants to write a computer program for calculating this number for any given starting configuration.
For example, if you are given a puzzle which looks this way:
≡ | ≡ | + |
≡ | ∗ | ∗ |
∆ | ∗ | ∆ |
one of the optimal rearrangements could look like
≡ | ∗ | ∆ |
≡ | ∗ | ∆ |
+ | ≡ | ∗ |
The first line of the input file contains two integers n (1 ≤ n ≤ 40 000 ) and k (1 ≤ k ≤ 50 000 ). Each of the next k lines contains the number of blocks with the corresponding type of picture li (li > 0, sum of all li is exactly n2).
Output the maximal possible number of equal first rows at the first line of the output file. The following n lines must contain contents of the row which gives the maximum. Each line shows a single number of picture, in order they must appear. If there are many optimal solutions, any is acceptable.
3 4 3 3 2 1Sample Output
2 1 2 3
using namespace std;
int n,k;
int ai[ 100050];
int judge(int midl){
int sum= 0;
for( int i= 1;i<=k;i++)
{
sum+=ai[i]/midl;
if(sum>=n)
return 1;
}
return 0;
}
int main(){
while( scanf( "%d %d",&n,&k)== 2)
{
for( int i= 1;i<=k;i++)
scanf( "%d",&ai[i]);
int l= 1,r=n,midl;
while(l<=r)
{
midl=(l+r)/ 2;
if(judge(midl))
l=midl+ 1;
else
r=midl -1;
}
printf( "%d\n",l -1);
int m=l -1,sum= 0;
for( int i= 1;i<=k;i++)
{
while(ai[i]>=m&&sum<n)
{
printf( "%d\n",i);
sum++;
ai[i]-=m;
}
}
}
return 0;
}