Pagini recente » Cod sursa (job #448479) | Cod sursa (job #2804055) | Cod sursa (job #2981659) | Cod sursa (job #1610526) | Cod sursa (job #2724116)
import java.io.FileReader;
import java.util.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
public class Main {
// 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;
// }
// 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;
// }
// 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)));
// }
// public static int solution(int[] nums, int n) {
//// int s, step, pos, count = 0;
// Arrays.sort(nums);
//// 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 = 0;
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, step;
for (pos = 0, 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("foo.in"));
PrintStream out = new PrintStream("foo.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();
}
}