题目描述:

D. 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

思路:

题目意思是给9个数字的价钱,和现在拥有的钱,看能不能买,能的话把能买到数字凑成最大的数是多少。

刚开始,想得简单了(只想法),就知道位数越多越好,那我就选最便宜的买咯,全买最便宜的,也想复杂了,还创建结构体,在sort写了个cmp结构体排序,输入的时候就直接忽略钱不够的数字,从钱最少的且数值最大的开始。(该简单的时候不简单,该复杂的时候又想简单了ε=(´ο`*))))

结果这个策略不行,为啥,有没有可能我的钱花不完,然后把剩下的钱买够得到的,尽可能多的情况下数值又尽量大的颜料。emmm,有道理,但是既然剩下的钱都可以买比现在的更便宜的颜料,那我直接全部买这种便宜的颜料数量上岂不更多?所以是不可能的啦。(一开始就这个思路还写了个递归,最后就像写bug一样把自己绕进去了QAQ)

那么,我要是不用全部买最便宜的颜料后剩下的钱呢?

什么意思,就是我假设可以买n个最便宜的颜料,嗯,那我买n-1个试试,剩下的钱看能不能买个9?买两个9?两个不行,那我能不能再买个8,两个8?,...,这样从大到小一直下去,就保证了数字位数最大,而且数值最大。代码对我来说有一点tricky。

知识点:贪心

代码:

 #include <iostream>
#include <algorithm>
#include <memory.h>
#define INF 0x3f3f3f3f
using namespace std;
int n;
int pri[];
int minm;
int main()
{
cin >> n;
int flag = ;
minm = INF;
for(int i = ;i<;i++)
{
cin >> pri[i];
if(n>pri[i])
{
flag = ;
}
if(pri[i]<minm)
{
minm = pri[i];
}
}
if(flag==)
{
cout << - << endl;
}
else
{
int sum = n/minm;
for(int i = sum;i>;i--)
{
for(int j = ;j>;j--)
{
if(n>=pri[j]&&(n-pri[j])/minm==i-)//可以买j,而且剩下的钱还可以买i-1个数
{
n -= pri[j];
cout << j;
}
}
}
cout << endl;
}
return ;
}

最新文章

  1. 利用DetachedCriteria构建HQL参数动态匹配
  2. .NET invoke NetSuite Restlet
  3. UML聚合与组合
  4. redis基础使用
  5. Delphi 泛型对象类
  6. HTML5-36d嗨起^_^
  7. 把struts2-convention-plugin丢进太平洋
  8. CF #Manthan, Codefest 16 C. Spy Syndrome 2 Trie
  9. properties文件作用以及在哪些地方用
  10. input表单的type属性详解,不同type不同属性之间区别
  11. [Swift]LeetCode410. 分割数组的最大值 | Split Array Largest Sum
  12. jquery扩展实现input框字符长度限制中文2个字符,英文1个字符
  13. css BFC布局及用处
  14. dns server 配置
  15. pro2
  16. Dijkstra算法(转)
  17. 【bzoj2006】NOI2010超级钢琴
  18. 统计iOS产品不同渠道的下载量
  19. Qt5.4.1移植到arm——Linuxfb篇
  20. Java笔记 —— 初始化

热门文章

  1. sqlyog 社区版
  2. 【视频开发】【Live555】摄像头采集,264编码,live555直播
  3. php_mvc实现步骤十
  4. MySql 、Oracle 获取表结构和字段信息
  5. 1.JVM前奏篇(看官网怎么说)
  6. python with方法
  7. ASP.NET MVC请求参数字符串之区分空与NULL
  8. 5. JDBC/ODBC服务器
  9. Linux进程自保护攻防对抗技术研究(Process Kill Technology &amp;&amp; Process Protection Against In Linux)
  10. android使用http3