您的位置:首页 > Web前端

CodeForces 349B Color the Fence

2016-07-19 10:54 351 查看
B. Color the Fence

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

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


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


题目大意:用一定量的油漆绘出一个最大的数字,第一行给出数V代表油漆的总量,第二行九个数分          别代表1-9这9个数字所需的所耗费的油漆量,问能绘出的最大数字是多少。
思路:我们用一个变量MIN表示1-9这9个数字所耗油漆量的最小值,那么V/MIN就表示V升油漆可绘       制的数字的最大长度。这样最大长度就有了,接下来我们开始找最大的数。从最高位开           始找,从大到小暴力9个数字,若发现某个数字可以绘制并且绘制后不会改变最大长度(例       如V = 5, 9个数分别为2、2、3、4、5、6、7、8、9,那么最大长度为2,当选择数字           1,2,3时不会改变最大长度,但若选择4时,便无法再继续选择,此时最大长度为1)时,
      就选用这个数,然后退出。不断循环,直到无法选择为止。
#include<cstdio>
#include<cstring>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
typedef pair<int,int> P;
int v;

vector<P> Num;

int main()
{
scanf("%d",&v);
int MIN = 1e9;
for(int i = 1; i <= 9; i++){
int x;
scanf("%d",&x);
Num.push_back(P(i,x));
MIN = min(MIN,x);
}
string s;
int ans = -1;
while(v >= MIN){
int len = v/MIN;
for(int i = 8; i >= 0; i--){
if(v >= Num[i].second && (v-Num[i].second)/MIN+1 == len){
s.push_back(Num[i].first+48);
v -= Num[i].second;
break;
}
}
}
if(!s.empty()){
cout << s << endl;
}
else cout << "-1" << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: