CF#202DIV2:B. Color the Fence

來源:互聯網
上載者:User

http://codeforces.com/contest/349/problem/B

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 digitd requiresad 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 integersa1, 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.

Sample test(s)Input
55 4 3 2 1 2 3 4 5
Output
55555
Input
29 11 1 12 5 8 9 10 6
Output
33
Input
01 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;}


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.