Pagini recente » Cod sursa (job #271094) | Cod sursa (job #1110037) | Cod sursa (job #606802) | Cod sursa (job #1915839) | Cod sursa (job #2724130)
import java.io.FileReader;
import java.util.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
public class Main {
// // Xor
/* public static int pow(int c) {
int p = -1;
while (c > 0) {
++p;
c >>= 1;
}
return p;
}
public static int solution(int c) {
int a = 0, b = 0, d = pow(c), p;
while (d >= 0) {
p = 1 << d;
if (((c >> d) & 1) == 0) {
a += p;
b += p;
} else {
if (a > b) b += p;
else a += p;
}
--d;
}
return a * b;
}
*/
// // Aparitii
/* public static int solution(int n, int k, int[] nums) {
int[][] digits = new int[10][10];
int no = 0;
for (int i = 0, digit = 0; i < n; ++i, digit = 0) {
while (nums[i] > 0) {
++digits[digit][nums[i] % 10];
nums[i] /= 10;
++digit;
}
}
for (int i = 9; i >= 0; --i, System.out.println())
for (int j = 0; j <= 9; ++j) {
if (digits[i][j] % k != 0)
no = no * 10 + j;
}
return no;
}
*/
// // Numere8
// public static int pow(int n, int p) {
// if (p == 0) {
// return 1;
// } else {
// int pp = pow(p / 2, n);
// return (p & 1) == 0 ? pp * pp : n * pp * pp;
// }
// }
//
// public static void solution(int[] nums) {
// System.out.println((pow(2,3)));
// }
// // NrTri
/* public static int solution(int[] nums, int n) {
Arrays.sort(nums);
// int s, step, pos, count = 0;
// for (int i = 0; i < n - 1; ++i)
// for (int j = i + 1; j < n; ++j) {
// s = nums[i] + nums[j];
// for (pos = j, step = 1 << 9; step > 0; step >>= 1)
// if (pos + step < n && s >= nums[step + pos]) {
// pos += step;
// }
//
// count += pos > j ? pos + 1 - j : 0;
// }
int s, pos = 2, count = 0;
if (n >= 3) {
for (int i = 0; i < n - 2 && pos <= n; ++i)
for (int j = i + 1; j < n - 1 && pos <= n; ++j, count += pos + 1 - j) {
s = nums[i] + nums[j];
while (pos + 1 < n && s >= nums[pos + 1]) ++pos;
}
}
System.out.println(count);
return count;
}
*/
public static int compute(int[] nums, int n, int c) {
int count = 0, kk = 1;
for (int i = 0; i < n; ++i)
if (count + nums[i] <= c) {
count += nums[i];
} else {
++kk;
count = nums[i];
}
return kk;
}
public static int solution(int[] nums, int n, int k) {
int pos = 0, step;
for (int i = 0; i < n; ++i)
if (pos < nums[i])
pos = nums[i];
for (--pos, step = 1 << 20; step > 0; step >>= 1) {
if (compute(nums, n, pos + step) > k) {
pos += step;
}
}
return pos + 1;
}
public static void main(String[] args) throws FileNotFoundException, IOException {
Scanner in = new Scanner(new FileReader("transport.in"));
PrintStream out = new PrintStream("transport.out");
// int t = in.nextInt(), c;
// while (t-- > 0) {
// out.print(solution(in.nextInt()));
// }
// int n = in.nextInt(), k = in.nextInt();
// int[] nums = new int[n];
// for (int i = 0; i < n; ++i)
// nums[i] = in.nextInt();
// out.print(solution(n, k, nums));
// int[] nums = new int[10];
// for (int i = 0; i < 10; nums[i] = in.nextInt(), ++i);
// solution(nums);
// int n = in.nextInt();
// int[] nums = new int[n];
// for (int i = 0; i < n; ++i)
// nums[i] = in.nextInt();
// solution(nums, n);
// Trasport
int n = in.nextInt(), k = in.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; ++i)
nums[i] = in.nextInt();
out.print(solution(nums, n, k));
in.close();
out.close();
}
}