fork(7) download
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5. private Scanner scanner;
  6.  
  7. private Main(Scanner scanner) {
  8. this.scanner = scanner;
  9. }
  10.  
  11. public static void main(String[] args) {
  12. Main main = new Main(new Scanner(System.in));
  13. main.solve();
  14. }
  15.  
  16. private void solve() {
  17. int n = scanner.nextInt();
  18. int m = scanner.nextInt();
  19. int[] a = new int[n];
  20. long s = 0;
  21. for (int i = 0; i < n; i++) s += a[i] = scanner.nextInt();
  22. long l = 2, r = s + n;
  23. while (l < r) {
  24. long z = l + r >> 1;
  25. int[] b = a.clone();
  26. int p = n - 1;
  27. for (int i = 0; i < m; i++) {
  28. while (p >= 0 && b[p] == 0) p--;
  29. long t = z - p - 1;
  30. if (t <= 0) break;
  31. while (p >= 0 && b[p] <= t) t -= b[p--];
  32. if (p >= 0) b[p] -= t;
  33. }
  34. if (p < 0) r = z;
  35. else
  36. l = z + 1;
  37. }
  38. System.out.println(r);
  39. }
  40. }
  41.  
Runtime error #stdin #stdout #stderr 0.15s 321088KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.util.NoSuchElementException
	at java.util.Scanner.throwFor(Scanner.java:862)
	at java.util.Scanner.next(Scanner.java:1485)
	at java.util.Scanner.nextInt(Scanner.java:2117)
	at java.util.Scanner.nextInt(Scanner.java:2076)
	at Main.solve(Main.java:17)
	at Main.main(Main.java:13)