-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBooks.java
More file actions
38 lines (37 loc) · 918 Bytes
/
Books.java
File metadata and controls
38 lines (37 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//https://codeforces.com/contest/279/problem/B
import java.util.*;
public class Books {
public static void main(String[] str) {
var in = new Scanner(System.in);
int n = in.nextInt();
int fund = in.nextInt();
var arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = in.nextInt();
in.close();
var prefix = new long[n + 1];
prefix[0] = 0;
for (int i = 1; i < n + 1; i++)
prefix[i] = prefix[i - 1] + arr[i - 1];
int best = 0;
for (int from = 0; from < n; from++) {
int left = from;
int right = n - 1;
while (left <= right) {
int mid = left + (right - left + 1) / 2;
long cost = prefix[mid + 1] - prefix[from];
if (fund >= cost) {
left = mid;
best = Math.max(mid - from + 1, best);
} else
right = mid - 1;
if (left == right) {
if (fund >= arr[left])
best = Math.max(1, best);
break;
}
}
}
System.out.println(best);
}
}