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
思路:
位数越多当然数越大,首先要考虑的是每一位都一样位数最大的情况。
但若每一位都是一样,可能会有剩余的油漆。
所以我们要找到在此长度的情况下,数值的最大值
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int main()
{
int a[10], sum, minn, k, i, j, cnt, r;
while (~scanf("%d", &sum))
{
minn = 1000000005;
k = 0;
for (i = 1; i <= 9; i++)
{
scanf("%d", &a[i]);
if (a[i] < minn) //找出花费最小的颜料数
{
minn = a[i];
k = i;
}
else if (a[i] == minn && k < i) //花费与最少的相等,自然取数字大的
k = i;
}
if (sum < minn) //最少的花费都大于总颜料,自然不行
{
printf("-1\n");
continue;
}
cnt = sum / minn; //最小花费能得到的数字长度
r = sum % minn;
if (!r) //若总颜料能整出最小花费,全部输出这个数一定是最大
{
for (i = 1; i <= cnt; i++)
printf("%d", k);
printf("\n");
continue;
}
while (sum > 0) //总颜料还没用完,找出与最小花费长度相等的最大数
{
int x = 0;
for (i = 1; i <= 9; i++)
{
int s = sum - a[i]; //减去这个数字的花费
if (s < 0) //这个数字会使颜料用完,换下一个颜料
continue;
if (s / minn == cnt - 1 && x < i) //减去该数字的花费之后,剩下的除以最小的长度是原来长度-1,必定是最符合的,在所有最符合的状况中找到最大的
x = i;
}
if (x)
{
sum -= a[x]; //放了一个数字,总花费减少
cnt--;//长度减一
printf("%d", x); //输出这个数字
}
}
printf("\n");
} return 0;
}

CodeForces 349B--Color the Fence(贪心)的更多相关文章

  1. codeforces 349B Color the Fence 贪心,思维

    1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...

  2. Codeforces 349B - Color the Fence

    349B - Color the Fence 贪心 代码: #include<iostream> #include<algorithm> #include<cstdio& ...

  3. 【贪心】Codeforces 349B.Color the Fence题解

    题目链接:http://codeforces.com/problemset/problem/349/B 题目大意 小明要从9个数字(1,2,--,9)去除一些数字拼接成一个数字,是的这个数字最大. 但 ...

  4. CodeForces 349B Color the Fence (DP)

    题意:给出1~9数字对应的费用以及一定的费用,让你输出所选的数字所能组合出的最大的数值. 析:DP,和01背包差不多的,dp[i] 表示费用最大为 i 时,最多多少位,然后再用两个数组,一个记录路径, ...

  5. Codeforces D. Color the Fence(贪心)

    题目描述: D. Color the Fence time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  6. 349B - Color the Fence

    Color the Fence Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Su ...

  7. codeforces B. Color the Fence 解题报告

    题目链接:http://codeforces.com/problemset/problem/349/B 题目意思:给定v升的颜料和9个需要花费ad 升的颜料,花费ad 升的颜料意味着得到第d个数字,现 ...

  8. NYOJ-791 Color the fence (贪心)

    Color the fence 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Tom has fallen in love with Mary. Now Tom w ...

  9. nyoj 791——Color the fence——————【贪心】

    Color the fence 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Tom has fallen in love with Mary. Now Tom w ...

  10. ACM Color the fence

    Color the fence 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Tom has fallen in love with Mary. Now Tom w ...

随机推荐

  1. Spring基于SchedulingConfigurer实现定时任务

    Spring 基于 SchedulingConfigurer 实现定时任务,代码如下: import org.springframework.scheduling.annotation.Schedul ...

  2. Hive 教程(六)-Hive Cli

    hive 有两种启动方式,一种是 bin/hive,一种是 hiveserver2, bin/hive 是 hive 的 shell 模式,所有任务在 shell 中完成,shell 就相当于 hiv ...

  3. 最小生成树之Prim Kruskal算法(转)

    最小生成树 首先,生成树是建立在无向图中的,对于有向图,则没有生成树的概念,所以接下来讨论的图均默认为无向图.对于一个有n个点的图,最少需要n-1条边使得这n个点联通,由这n-1条边组成的子图则称为原 ...

  4. 基于IdentityServer4的声明的授权

    ## 概述 基于Asp.net Core 1.1 ,使用IdentityServer4认证与授权. ## 参考资料 [微软教程](https://docs.microsoft.com/zh-cn/as ...

  5. sql--select into,create database,create table,Constraints

    SQL SELECT INTO 语句可用于创建表的备份复件.SELECT INTO 语句SELECT INTO 语句从一个表中选取数据,然后把数据插入另一个表中.SELECT INTO 语句常用于创建 ...

  6. Yii2 常用代码集合

    Yii2.0 对数据库查询的一些简单的操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ...

  7. 常用CSS代码大全(工作必备)

    用html+css可以很方便的进行网页的排版布局,但不是每一种属性或者代码我们都铭记于心,最近我把CSS中的常用代码进行了归纳总结,方便自己以后查看,同时也分享给大家,希望对你们有用. 一.文本设置 ...

  8. wex5 页面跳转

    页面交互: 3种方法: 1.使用Shell提供的方法 打开另一个页面不需要等待页面返回 功能树上打开 2. 用windowDialog组件 需要等待页面返回 3.内嵌页 windowContainer ...

  9. 部署Dashboard,监控应用

    部署web UI(dashboard)用于监控node资源 参见文档:https://blog.csdn.net/networken/article/details/85607593 官网:https ...

  10. 转换byte(十进制)为图形(大小写字母,符号)

    /** * 转换byte(十进制)为字母 */ public static String ByteToLetter (byte[] chars) { String Letter = "&qu ...