Bridging the Gap

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

요약
다리 정원 c와 각자의 이동 시간이 주어질 때, 모든 사람이 건너는 데 필요한 최소 총 시간을 구한다.
난이도

보통10점 중 7점

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

문제

A group of walkers arrives at a river in the night. They want to cross a bridge, which can hold a limited number of walkers at a time. The walkers have just one torch, which needs to be used when crossing the bridge. Each walker takes a certain time to cross; a group crossing together must walk at the slowest walker’s pace. What is the shortest time it takes for all walkers to cross the bridge?

For example, Sample Input 1 assumes the bridge can hold 22 walkers at a time and there are 44 walkers with crossing times 11 minute, 22 minutes, 55 minutes and 1010 minutes, respectively. The shortest time of 1717 minutes can be achieved by the following sequence of crossings. First, the two fastest walkers cross in 22 minutes. Second, the fastest walker crosses back in 11 minute. Third, the two slowest walkers cross in 1010 minutes. Fourth, the second-fastest walker crosses back in 22 minutes. Fifth, the two fastest walkers cross in 22 minutes.

입력

The first line of input contains two integers nn and cc, where nn (2≤n≤1042≤n≤10^4) is the number of walkers, and cc (2≤c≤1042≤c≤10^4) is the number of walkers the bridge can hold at a time.

Then follows a line containing nn integers t_1,…,t_nt\_1,\dots ,t\_n (1≤t_i≤1091≤t\_i≤10^9 for all ii). The iith walker takes time t_it\_i to cross.

출력

Output the minimum total time it takes for the entire group to cross the bridge.

예제2

  1. 예제 1

    입력
    4 2
    1 2 10 5
    
    예상 출력
    17
    
  2. 예제 2

    입력
    4 6
    1 2 10 5
    
    예상 출력
    10