Igor has fallen in love with Tanya. Now Igor wants to show his feelingsand write a number on the fence opposite to Tanya's house. Igor thinks that thelarger the number is, the more chance to win Tanya's heart he has.
Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters ofpaint. Besides, Igor heard that Tanya doesn't like zeroes. That's why Igorwon't use them in his number.
Help Igor find the maximum number he can write on the fence.
Input
The first line contains a positive integer v (0 ≤ v ≤ 106). Thesecond line contains nine positive integers a1, a2, ..., a9(1 ≤ ai ≤ 105).
Output
Print the maximum number Igor can write on the fence. If he has toolittle paint for any digit (so, he cannot write anything), print -1.
Sample test(s)
input
5
5 4 3 2 1 2 3 4 5
output
55555
input
2
9 11 1 12 5 8 9 10 6
output
33
input
0
1 1 1 1 1 1 1 1 1
output
-1
思路:
贪心
将数字1~9按ai从小到大排序,ai一样的大的排在前面
然后用v除以最小的ai,确定输出数字的长度
然后从最高位到最低位判断能否替换成尽量大的数
程序:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
using namespace std;
#define L(u) (u<<1)
#define R(u) (u<<1|1)
#define lowbit(x) (x&-x)
#define rep(i,x,y) for (i=x;i<=y;i++)
#define ll __int64
#define max(x,y) ((x>y)?(x),(y))
#define min(x,y) ((x<y)?(x),(y))
#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define slld(x) scanf("%lld",&x)
const int N = 1000005;
struct node
{
int v, id;
}a[12];
bool cmp(node a, node b)
{
if (a.v == b.v)
return a.id > b.id;
else
return a.v<b.v;
}
bool cmp2(node a, node b)
{
return a.id<b.id;
}
int main()
{
int v;
int l = 0, i;
while (sd(v) != EOF)
{
rep(i, 1, 9)
{
sd(a[i].v);
a[i].id = i;
}
sort(a + 1, a + 10, cmp);
int temp = a[1].id;
l = v / a[1].v;
if (l == 0)
{
printf("-1\n");
return 0;
}
v -= a[1].v*l;
sort(a + 1, a + 10, cmp2);
rep(i, 1, l)
{
int j, ans = temp;
for (j = 9; j > temp; j--)
{
if (a[j].v - a[temp].v <= v)
{
ans = j;
v -= a[j].v - a[temp].v;
break;
}
}
printf("%d", ans);
}
printf("\n");
}
return 0;
}