아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Historical TV Remote Control

면접 대비

시간 제한1초메모리 제한1024 MB

요약
고장 난 숫자 버튼이 있는 리모컨에서 1부터 999까지의 목표 채널에 도달하기 위해 필요한 채널 업/다운 최소 횟수를 구한다. 시작 채널은 작동하는 숫자로 직접 입력한다.
난이도

보통10점 중 4점

유형
완전 탐색, 구현, 수학, 그리디
정답자
아직 제출이 없습니다

문제

As Dr. Orooji is getting older, he is becoming more attached to older items and has difficulty letting go of them (he claims they have historical value). For example, he still has the first table he got for the programming team! The situation is the same at home, e.g., there is a broken TV remote control but Dr. O still uses it, because he considers it an old item with historical value!

The old remote control has 12 buttons: digits 0-9, channel down, and channel up. There are no other buttons on the remote control. Some digits on the remote don’t work but channel up/down always works. So, to get to a particular channel, Dr. O sometimes has to use the channel up/down. For example, let’s assume digits 0 and 5 on the remote don’t work:

  • If Dr. O wants to watch channel 102, he would select 99 and then “channel up” 3 times.
  • If he wants to watch channel 597, he would select 611 and then “channel down” 14 times.

Given the digits that do not work and a target channel, determine how many times Dr. O needs to hit channel up or down. Dr. O, of course, wants to exert the least energy, hence he wants to hit the channel up/down the minimum number of times. Assume that Dr. O will enter a channel between 0 and 999 (inclusive) to start and that channel down has no effect at 0 and channel up has no effect at 999.

입력

The first input line contains an integer, n (1 ≤ n ≤ 9), indicating how many digits on the remote do not work. These broken digits are listed (in increasing order) on the same input line. The second input line provides the target channel (an integer between 1 and 999, inclusive).

출력

The output consists of a single integer, indicating how many times Dr. O needs to hit channel up/down. Note that, since one or more digits work, it is always possible to reach the target channel.

예제2

  1. 예제 1

    입력
    3 0 8 9
    35
    
    예상 출력
    0
    
  2. 예제 2

    입력
    4 1 2 5 9
    250
    
    예상 출력
    50