import java.util.Arrays;
import java.util.Scanner;
public class Main {
public int count(int[] arr, int capacity) {
int cnt = 1;
int sum = 0;
for(int x : arr) {
if(sum+x > capacity) {
cnt++;
sum = x;
}
else {
sum += x;
}
}
return cnt;
}
public int solution(int n, int cd, int[] arr) {
int ans = 0;
int lt = Arrays.stream(arr).max().getAsInt();
int rt = Arrays.stream(arr).sum();
while(lt <= rt) {
int mid = (lt + rt) / 2;
if(count(arr, mid) <= cd) {
ans = mid;
rt = mid-1;
}
else {
lt = mid+1;
}
}
return ans;
}
public static void main(String[] args) {
Main T = new Main();
Scanner kb = new Scanner(System.in);
int n = kb.nextInt();
int cd = kb.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = kb.nextInt();
}
System.out.println(T.solution(n, cd, arr));
}
}
'Solved > 코딩테스트' 카테고리의 다른 글
동전교환 (냅색 알고리즘) (0) | 2023.04.06 |
---|---|
최대점수 구하기 (냅색 알고리즘) (0) | 2023.04.05 |
섬나라 아일랜드 (BFS, DFS) (0) | 2023.03.30 |
토마토(BFS 활용) (0) | 2023.03.29 |
후위연산식 문제 (Stack) (0) | 2023.03.18 |