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

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

Grinding Gravel

면접 대비

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

요약
돌의 무게들과 같은 용량의 격자 칸들이 주어질 때, 조각들을 칸에 정확히 채우기 위해 돌을 최소 몇 번 쪼개야 하는지 구한다.
난이도

보통10점 중 7점

유형
동적 계획법, 그리디, 수학, 비트 연산
정답자
아직 제출이 없습니다

문제

During the renovation of your garden, you decide that you want a gravel path running from the street to your front door. Being a member of the Boulders And Pebbles Community, you want this path to look perfect. You already have a regular grid to put the gravel in, as well as a large container of gravel containing exactly as much as the total capacity of the grid.

There is one problem: the gravel does not yet fit perfectly into the grid. Each grid cell has the same (fixed) capacity and every piece of gravel has a certain weight. You have a grindstone that can be used to split the stones into multiple pieces, but doing so takes time, so you want to do a minimal number of splits such that the gravel can be exactly distributed over the grid.

As an example, consider the first sample case. There are three grid cells of size 88, which can be filled as follows. Put the stones of weight 22 and 66 in the first cell. Now grind the stone of weight 77 into two pieces of weight 33 and 44. Then the other two grid cells get filled by weights 3,53, 5 and 4,44, 4 respectively.

입력

The input consists of:

  • One line with two integers nn and kk (1≤n≤1001 \leq n \leq 100, 1≤k≤81 \leq k \leq 8), the number of pieces of gravel and the capacity per grid cell.
  • One line with nn integers w_1,…,w_nw\_1, \dots, w\_n (1≤w_i≤1061 \leq w\_i \leq 10^6 for all ii), the weight of each piece of gravel.

It is guaranteed that w_1+w_2+⋯+w_nw\_1 + w\_2 + \dots + w\_n is a multiple of kk.

출력

Output the minimal number of times a stone needs to be split into two, such that all the pieces of gravel can be used to fill all the grid cells perfectly.

예제2

  1. 예제 1

    입력
    5 8
    2 4 5 6 7
    
    예상 출력
    1
    
  2. 예제 2

    입력
    2 5
    12 13
    
    예상 출력
    4