Color the Fence(贪心)

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

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
out put
55555
input
2
9 11 1 12 5 8 9 10 6
output
33
intput
0
1 1 1 1 1 1 1 1 1
output
-1

这道题WA了好几发。
有V升油漆,数字1~9分别消耗不同的油漆(不写0),求能写出的最大数字。
这道题首先想到的就是先找到消耗油漆最少的数字,然后所有的油漆都用来写这个数,位数越多数字越大嘛。但是,有可能写完了后还有剩余的油漆,这时候就从高位到低位去替换数字,把小的数字替换成所能替换的数字中的最大的。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
typedef long long ll;
using namespace std;
int main()
{
    int v,i,j,cnt,minx=9999999;
    int n,r,t;
    int a[11];
    int ans[10001];
    cin>>v;
    for(i=1; i<=9; i++)
        cin>>a[i];
    for(i=1; i<=9; i++)
        if(a[i]<=v)
            break;
    if(i>9)
    {
        cout<<-1;
        return 0;
    }
    for(i=1; i<=9; i++)
        if(a[i]<=minx)
        {
            minx=a[i];
            cnt=i;
        }
    n=v/minx;
    r=v%minx;
    while(r>0&&n>0)
    {
        for(i=9; i>cnt; i--)
        {
            if(a[i]-a[cnt]<=r)
            {
                cout<<i;
                n--;
                r=r-a[i]+a[cnt];
                break;
            }
        }
        if(i==cnt)
            break;
    }
    for(i=0; i<n; i++)
        cout<<cnt;
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值