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. postman 获取登录成功后存在在header中cookies,并在下一个接口中使用。

    1.首先登录成功 2.在tests中添加脚本 3.添加环境变量 4.配置环境变量和引用值 5.请求第二个接口成功

  2. java检测是不是移动端访问

    request可以用别的代替 private static boolean isMobile(){ HttpServletRequest request = ThreadContextHolder.g ...

  3. 北大 ACM highways问题研究(最小生成树)

    #include<stdlib.h> #include<stdio.h> #include<queue> struct vertex//代表一个村庄 { int m ...

  4. JS基础_JS的HelloWorld

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. springboot(二十二)-sharding-jdbc-读写分离

    前面我们使用sharding-jdbc配置了分库分表.sharding-jdbc还有个用法,就是实现读写分离. 什么时候需要或者可以使用读写分离? 当我们的项目所使用的数据库查询的访问量,访问频率,及 ...

  6. 如何在万亿级别规模的数据量上使用Spark

    一.前言 Spark作为大数据计算引擎,凭借其快速.稳定.简易等特点,快速的占领了大数据计算的领域.本文主要为作者在搭建使用计算平台的过程中,对于Spark的理解,希望能给读者一些学习的思路.文章内容 ...

  7. GitFlow入门

    1-概述 2-GitFLow分支介绍 2.1-master 分支 2.2-develop 分支 2.3-feature 分支 2.4-release 分支 2.5-hotfix 分支 3-GitFlo ...

  8. 富文本编辑器--引入demo和简单使用

    wangEditor —— 轻量级 web 富文本编辑器,配置方便,使用简单.支持 IE10+ 浏览器. 官网:www.wangEditor.com 文档:www.kancloud.cn/wangfu ...

  9. pgsql sql字段拼接

    1.  一条记录数据字段拼接 语法:concat_ws('拼接符号',字段名,more fields) 例子:concat_ws(':',username,sex)2. 多条记录字段拼接 语法:con ...

  10. linux 软件安装目录详解

    我一般会在/opt目录下创建 一个software目录,用来存放我们从官网下载的软件格式是.tar.gz文件,或者通过 wget+地址下载的.tar.gz文件 执行解压缩命令,这里以nginx举例 t ...