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.
The first line contains a positive integer v (0 ≤ v ≤ 10^6). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 10^5).
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.
Input
The first line contains a positive integer v (0 ≤ v ≤ 10 ^6). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 10^5).
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.
Samples
5
5 4 3 2 1 2 3 4 5
55555
2
9 11 1 12 5 8 9 10 6
33
0
1 1 1 1 1 1 1 1 1
-1
思路用最小花费的数字确定位数,然后如果剩下的花费大于0,就从最大数字开始调整,如果满足v1+a[1].cost>=b[j].cost就直接调整,直到v1为0;
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 10;
class A{
public: int num;
int cost;
};
bool cmp(A a, A b) {//花费从小到大排序
if (a.cost != b.cost) {
return a.cost < b.cost;
}
else if (a.num != b.num) {//相同花费按数字大的排序
return a.num > b.num;
};
};
bool cmp1(A a, A b) {
return a.num > b.num;//数字从大到小排序
};
int res[1000010];//用于保存结果
int main() {
A* a = new A[10];
A* b = new A[10];//用于调整数字
long long v,v1;
cin >> v;
v1 = v;
for (int i = 1; i <= 9; i++) {
cin >> a[i].cost;
a[i].num = i;
b[i].cost = a[i].cost;
b[i].num = i;
};
sort(b + 1, b + 10, cmp1);//数字从小到大排序
sort(a + 1, a + 10, cmp);//按花费从小到大排
/*for (int i = 1; i <= 9; i++) {
cout << a[i].cost << " " <<a[i].num<<" ";
};*/
if (v1 < a[1].cost) {
cout << -1 << endl;
return 0;
};
int flag = 0;
while (v1 > 0) {//确定位数
if (v1 >= a[1].cost) {
v1 -= a[1].cost;
res[flag++] = a[1].num;
}
else {
break;
};
};
//调整颜料,换尽快可能大数
if (v1 > 0) {
for (int i = 0; i < flag; i++) {
if (v1 == 0) break;
for (int j = 1; j <= 9; j++) {
if ((a[1].cost + v1) >= b[j].cost && res[i] < b[j].num) {
res[i] = b[j].num;
v1 = v1 + a[1].cost - b[j].cost;
break;
};
};
};
};
for (int i = 0; i < flag; i++) cout << res[i];
cout << endl;
return 0;
};
ac了