Problem Description
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya’s house.
Igor thinks that the larger 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 of paint. Besides, Igor
heard that Tanya doesn’t like zeroes. That’s why Igor won’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). The second 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 too little paint for any digit (so, he cannot write anything), print -1.
Examples
input
5
5 4 3 2 1 2 3 4 5
out put
55555
input
2
9 11 1 12 5 8 9 10 6
output
33
intput
0
1 1 1 1 1 1 1 1 1
output
-1
这道题WA了好几发。
有V升油漆,数字1~9分别消耗不同的油漆(不写0),求能写出的最大数字。
这道题首先想到的就是先找到消耗油漆最少的数字,然后所有的油漆都用来写这个数,位数越多数字越大嘛。但是,有可能写完了后还有剩余的油漆,这时候就从高位到低位去替换数字,把小的数字替换成所能替换的数字中的最大的。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
typedef long long ll;
using namespace std;
int main()
{
int v,i,j,cnt,minx=9999999;
int n,r,t;
int a[11];
int ans[10001];
cin>>v;
for(i=1; i<=9; i++)
cin>>a[i];
for(i=1; i<=9; i++)
if(a[i]<=v)
break;
if(i>9)
{
cout<<-1;
return 0;
}
for(i=1; i<=9; i++)
if(a[i]<=minx)
{
minx=a[i];
cnt=i;
}
n=v/minx;
r=v%minx;
while(r>0&&n>0)
{
for(i=9; i>cnt; i--)
{
if(a[i]-a[cnt]<=r)
{
cout<<i;
n--;
r=r-a[i]+a[cnt];
break;
}
}
if(i==cnt)
break;
}
for(i=0; i<n; i++)
cout<<cnt;
return 0;
}