Codeforces Round #202 (Div. 2)B. Color the Fence(贪心,模拟)

本文介绍了一个编程问题:如何利用有限的油漆资源,在围栏上写出尽可能大的数字以吸引对面房子里的女孩注意。文章提供了完整的代码实现,并解释了核心思路:先确定能够书写的数字数量,再尝试替换数字以形成最大的可能数值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
题意:给你n元,构成数字1-9的花费为ai,问能组成的最大数字是多少。
题解:首先数字数越多越好,当数字数确定后,看能不能把将数字换成更大的。有点模拟的意思。
代码:

#include <bits/stdc++.h>
#define ll long long
#define bababba printf("!!!!!!!!!\n")
using namespace std;
int cnt;
map<int,int>mp;
const int N=1e7+10;
int ans[N];
struct node
{
    int id,pi;
    bool operator <(const node &t)const{
       return pi<t.pi||(pi==t.pi&&id>t.id);
    }
}a[100];
void save()
{
    for(int i=1;i<=cnt;i++)
    {
        ans[i]=a[1].id;
    }
}
void print()
{
    for(int i=1;i<=cnt;i++)
    {
        printf("%d",ans[i]);
    }
    printf("\n");
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=9;i++)
    {
        int x;
        scanf("%d",&x);
        a[i].id=i;
        a[i].pi=x;
    }
    sort(a+1,a+10);
    for(int i=1;i<=9;i++)
    {
        mp[a[i].id]=i;
    }
     cnt=n/a[1].pi;
     if(cnt==0)
     {
         printf("-1\n");
         return 0;
     }
    save();
    if(n%a[1].pi==0)
    {
        print();
    }
    else
    {
        int y=n%a[1].pi;
        for(int i=1;i<=cnt;i++)
        {
            for(int j=9;j>=1;j--)
            {
                if(y+a[mp[ans[i]]].pi>=a[mp[j]].pi&&j>ans[i])//j>ans[i]不加也能过,但我感觉是错的。。
                {
                    int t=ans[i];
                    ans[i]=j;
                    y=y+a[mp[t]].pi-a[mp[j]].pi;
                    break;
                }
            }
        }
        print();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值